Repository: qutebrowser/qutebrowser
Pull request: Added protocol key to field [WIP]
The tabwidget.py correctly formats fields['protocol'], but I'm not sure that the key is accessible. #3230

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/qutebrowser/qutebrowser/3370)
<!-- Reviewable:end -->

edit by @the-compiler: Fixes #3230 (so the issue gets auto-closed)

Problem statement:
window.title_format protocol field
Couldn't find anything about this, so I'm trying an issue. Would it be possible/easy to implement a {protocol} field or something like it for window.title_format so that you could include "http(s)://" (and other protocols)?

The reason I'm asking is that I'm using KeepassXC and the autotype works perfectly with the URL field if I set the title_format to {host} and match it in the URL field for an entry. I did something similar with plugins for other browsers, but there I could also include "https" in the URL fiels to ensure no passwords would be provided unless the page is loaded using encryption. Hope that was more or less understandable...

I could try to take a look at this myself, although I have a bad habit of saying that and never getting around to actually doing something about it.... 😄

Hints from issue discussion:
Shouldn't be hard, it's just a matter of putting a `protocol` key with the correct value in [field](https://github.com/qutebrowser/qutebrowser/blob/master/qutebrowser/mainwindow/tabwidget.py#L152-L194).
See ffab9e263f6903315ef76261c006c39f37a9a84d for a similar example.

In the long run, this should be fixed by #1578 though, which would allow accessing any part of an URL.
Hi! I am new to open source projects and contributing, can I take this project?
@evanlee123: Just open a (WIP) PR.
I have not gotten around to start looking into this (as previously predicted), so please do 😄 
@evanlee123 Welcome! Yeah, please do - and if you get stuck somewhere, drop by in the [IRC channel](https://github.com/qutebrowser/qutebrowser#getting-help)! You might also want to take a look at the [contribution docs](https://github.com/qutebrowser/qutebrowser/blob/master/doc/contributing.asciidoc) for some helpful hints if you haven't yet :wink:
Will do! @marza91 Just to be clear , are you trying to implement a command that shows the protocol in the title bar (of the main window) or inside the tab titles?
I think the idea wasn't providing a command, but a field for the `window.title_format` and `tabs.title.format` settings.

Commit message: added the scheme field to FakeURL

Patch to review:
diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml
index 08c854ed3f4..efcc07b3558 100644
--- a/qutebrowser/config/configdata.yml
+++ b/qutebrowser/config/configdata.yml
@@ -1292,6 +1292,7 @@ tabs.title.format:
       - host
       - private
       - current_url
+      - protocol
     none_ok: true
   desc: |
     Format to use for the tab title.
@@ -1308,6 +1309,7 @@ tabs.title.format:
     * `{backend}`: Either ''webkit'' or ''webengine''
     * `{private}` : Indicates when private mode is enabled.
     * `{current_url}` : URL of the current web page.
+    * `{protocol}` : Internet Protocol of the current web page.
 
 tabs.title.format_pinned:
   default: '{index}'
@@ -1324,6 +1326,7 @@ tabs.title.format_pinned:
       - host
       - private
       - current_url
+      - protocol
     none_ok: true
   desc: Format to use for the tab title for pinned tabs. The same placeholders
     like for `tabs.title.format` are defined.
@@ -1467,6 +1470,7 @@ window.title_format:
       - backend
       - private
       - current_url
+      - protocol
   default: '{perc}{title}{title_sep}qutebrowser'
   desc: |
     Format to use for the window title. The same placeholders like for
diff --git a/qutebrowser/mainwindow/tabwidget.py b/qutebrowser/mainwindow/tabwidget.py
index b144663377c..01b9f0728d1 100644
--- a/qutebrowser/mainwindow/tabwidget.py
+++ b/qutebrowser/mainwindow/tabwidget.py
@@ -181,6 +181,11 @@ def get_tab_fields(self, idx):
         except qtutils.QtValueError:
             fields['current_url'] = ''
 
+        try:
+            fields['protocol'] = self.tab_url(idx).scheme()
+        except qtutils.QtValueError:
+            fields['protocol'] = ''
+
         y = tab.scroller.pos_perc()[1]
         if y is None:
             scroll_pos = '???'
diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py
index 878c9e1668b..cb3c8589633 100644
--- a/tests/helpers/stubs.py
+++ b/tests/helpers/stubs.py
@@ -131,11 +131,12 @@ class FakeUrl:
 
     """QUrl stub which provides .path(), isValid() and host()."""
 
-    def __init__(self, path=None, valid=True, host=None, url=None):
+    def __init__(self, path=None, valid=True, host=None, url=None, scheme = None):
         self.path = mock.Mock(return_value=path)
         self.isValid = mock.Mock(returl_value=valid)
         self.host = mock.Mock(returl_value=host)
         self.url = mock.Mock(return_value=url)
+        self.scheme = mock.Mock(return_value=scheme)
 
 
 class FakeNetworkReply:


Code review comments:
diff_hunk: @@ -131,11 +131,12 @@ class FakeUrl:
 
     """QUrl stub which provides .path(), isValid() and host()."""
 
-    def __init__(self, path=None, valid=True, host=None, url=None):
+    def __init__(self, path=None, valid=True, host=None, url=None, scheme = None):
path: tests/helpers/stubs.py
text: @user1:
There typically are no spaces around `=` for default arguments in Python, so that should be `scheme=None`.

@author:
Will do!

diff_hunk: @@ -1308,6 +1309,7 @@ tabs.title.format:
     * `{backend}`: Either ''webkit'' or ''webengine''
     * `{private}` : Indicates when private mode is enabled.
     * `{current_url}` : URL of the current web page.
+    * `{protocol}` : Internet Protocol of the current web page.
path: qutebrowser/config/configdata.yml
text: @user1:
Please remove the space before the `:` - though that's probably my fault, as the two lines above have that space as well. While you're at it, can you please remove it in all three lines?

Also, I'd prefer something like "Protocol (http/https/...) of the current web page." or so, as "Internet Protocol" is often associated with "IP" (the protocol, as in e.g. TCP/IP).

@author:
Qt calls the application protocols (http/ftp/etc) scheme, would that work? I just think the title is a little too long.

EDIT: nevermind I misunderstood... will do!

diff_hunk: @@ -181,6 +181,11 @@ def get_tab_fields(self, idx):
         except qtutils.QtValueError:
             fields['current_url'] = ''
 
+        try:
+            fields['protocol'] = self.tab_url(idx).scheme()
path: qutebrowser/mainwindow/tabwidget.py
text: @user1:
Since we're calling `self.tab_url(idx)` a third time here already, can you please add a `url = self.tab_url(idx)` somewhere above (e.g. after all the `fields[...] = ...` assignments) and change the calls here to `url.host()` etc.?

@author:
Will do!