From 4b5b4ee129933d91ef6f53cb0fa81ec591fb9985 Mon Sep 17 00:00:00 2001 From: Leo S <13348080+leosolid@users.noreply.github.com> Date: Sun, 25 Jul 2021 21:28:09 -0700 Subject: add relative_index for tab title format field (#6616) --- doc/help/settings.asciidoc | 1 + qutebrowser/config/configdata.yml | 3 +++ qutebrowser/mainwindow/tabwidget.py | 22 ++++++++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/doc/help/settings.asciidoc b/doc/help/settings.asciidoc index d993ddddd..70ea2ab6e 100644 --- a/doc/help/settings.asciidoc +++ b/doc/help/settings.asciidoc @@ -4360,6 +4360,7 @@ The following placeholders are defined: * `{index}`: Index of this tab. * `{aligned_index}`: Index of this tab padded with spaces to have the same width. +* `{relative_index}`: Index of this tab relative to the current tab. * `{id}`: Internal tab ID of this tab. * `{scroll_pos}`: Page scroll position. * `{host}`: Host of the current web page. diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index 5f5a71b06..147ec0b52 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -2140,6 +2140,7 @@ tabs.title.format: - title_sep - index - aligned_index + - relative_index - id - scroll_pos - host @@ -2159,6 +2160,7 @@ tabs.title.format: * `{index}`: Index of this tab. * `{aligned_index}`: Index of this tab padded with spaces to have the same width. + * `{relative_index}`: Index of this tab relative to the current tab. * `{id}`: Internal tab ID of this tab. * `{scroll_pos}`: Page scroll position. * `{host}`: Host of the current web page. @@ -2179,6 +2181,7 @@ tabs.title.format_pinned: - title_sep - index - aligned_index + - relative_index - id - scroll_pos - host diff --git a/qutebrowser/mainwindow/tabwidget.py b/qutebrowser/mainwindow/tabwidget.py index 7983127d5..7b22b5c0a 100644 --- a/qutebrowser/mainwindow/tabwidget.py +++ b/qutebrowser/mainwindow/tabwidget.py @@ -136,18 +136,31 @@ class TabWidget(QTabWidget): (fmt is None or ('{' + field + '}') not in fmt)): return + def right_align(text): + return str(text).rjust(len(str(self.count()))) + + def left_align(text): + return str(text).ljust(len(str(self.count()))) + + bar = self.tabBar() + cur_idx = bar.currentIndex() + if idx == cur_idx: + rel_idx = left_align(idx + 1) + " " + else: + rel_idx = " " + right_align(abs(idx - cur_idx)) + fields = self.get_tab_fields(idx) fields['current_title'] = fields['current_title'].replace('&', '&&') fields['index'] = idx + 1 - fields['aligned_index'] = str(idx + 1).rjust(len(str(self.count()))) + fields['aligned_index'] = right_align(idx + 1) + fields['relative_index'] = rel_idx title = '' if fmt is None else fmt.format(**fields) - tabbar = self.tabBar() # Only change the tab title if it changes, setting the tab title causes # a size recalculation which is slow. - if tabbar.tabText(idx) != title: - tabbar.setTabText(idx, title) + if bar.tabText(idx) != title: + bar.setTabText(idx, title) def get_tab_fields(self, idx): """Get the tab field data.""" @@ -305,6 +318,7 @@ class TabWidget(QTabWidget): def _on_current_changed(self, index): """Emit the tab_index_changed signal if the current tab changed.""" self.tabBar().on_current_changed() + self.update_tab_titles() self.tab_index_changed.emit(index, self.count()) @pyqtSlot() -- cgit v1.2.3-54-g00ecf