diff --git a/docs/changelog.rst b/docs/changelog.rst index 9330c1c03..3c174a7f5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -265,6 +265,10 @@ Detailed list of changes status bars, file trees, etc. Create them with :code:`launch --type=window-dock` or :code:`launch --type=tab-dock`, see :ref:`docks` +- Vertical tab bars: When :opt:`tab_title_max_length` is set, size the tab bar + to fit the decorations of the current :opt:`tab_bar_style` rather than always + adding eight cells, allowing much narrower tab bars (:iss:`10680`) + - quick-access-terminal kitten: A new :option:`kitty +kitten quick_access_terminal --if-running` option to run a program in a new tab or window of an already running quick diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 4010fd16a..17c720315 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -2364,7 +2364,10 @@ opt( The maximum number of cells that can be used to render the text in a tab. A value of zero means that no limit is applied. For vertical tab bars, kitty uses a default sidebar width sized for about twenty title cells when this is -left unset. +left unset. When it is set, the sidebar is made just wide enough to fit titles +of this length along with the decorations drawn by :opt:`tab_bar_style`. With +the :code:`fade` style, use fewer values in :opt:`tab_fade` for a narrower +sidebar. """, ) diff --git a/kitty/state.c b/kitty/state.c index b8e7c76a1..a829785d5 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -763,8 +763,11 @@ vertical_tab_bar_cols(const OSWindow *os_window, long margin_outer, long margin_ long available_width = (long)os_window->viewport_width - margin_outer - margin_inner; if (available_width <= 0) return 0; unsigned available_cols = MAX(1u, (unsigned)available_width / cell_width); - unsigned title_cols = OPT(tab_title_max_length) > 0 ? (unsigned)OPT(tab_title_max_length) : 20u; - unsigned desired_cols = title_cols + 8u; + // Without an explicit title length use a default sidebar wide enough for + // about twenty title cells, otherwise fit the title and whatever + // decorations the tab bar style draws around it. + unsigned desired_cols = 28u; + if (OPT(tab_title_max_length) > 0) desired_cols = MAX((unsigned)OPT(tab_title_max_length) + OPT(vertical_tab_bar.padding), OPT(vertical_tab_bar.min_cols)); unsigned soft_max = available_cols / 3u; if (soft_max < 6u) soft_max = available_cols; return MAX(1u, MIN(available_cols, MIN(desired_cols, MAX(1u, soft_max)))); @@ -1088,6 +1091,45 @@ PYWRAP0(get_options) { return global_state.options_object; } +static bool +set_vertical_tab_bar_padding(PyObject *py_opts, Options *opts) { + // The number of cells each tab bar style draws around the title in a + // vertical tab bar, see the draw_tab_with_* functions in tab_bar.py. + // Custom styles can draw anything, so give them generous room. + opts->vertical_tab_bar.padding = 8; + opts->vertical_tab_bar.min_cols = 1; + RAII_PyObject(style, PyObject_GetAttrString(py_opts, "tab_bar_style")); + if (!style) return false; + if (PyUnicode_CompareWithASCIIString(style, "fade") == 0) { + RAII_PyObject(fade, PyObject_GetAttrString(py_opts, "tab_fade")); + if (!fade) return false; + Py_ssize_t n = PyObject_Length(fade); + if (n < 0) return false; + // Fade cells on both sides of the title, the trailing ones are only + // drawn when they end before the last column + opts->vertical_tab_bar.padding = 2 * (unsigned)n + (n < 2 ? 1 : 0); + } else if (PyUnicode_CompareWithASCIIString(style, "powerline") == 0) { + // Leading space, trailing space and the separator + opts->vertical_tab_bar.padding = 3; + } else if (PyUnicode_CompareWithASCIIString(style, "slant") == 0) { + // Separators and spaces on both sides of the title, tabs narrower + // than six cells show only an ellipsis + opts->vertical_tab_bar.padding = 4; + opts->vertical_tab_bar.min_cols = 6; + } else if (PyUnicode_CompareWithASCIIString(style, "separator") == 0) { + // The spaces in tab_separator, plus the column reserved at the end + RAII_PyObject(sep, PyObject_GetAttrString(py_opts, "tab_separator")); + if (!sep) return false; + Py_ssize_t len = PyUnicode_GetLength(sep); + if (len < 0) return false; + unsigned spaces = 0; + for (Py_ssize_t i = 0; i < len && PyUnicode_READ_CHAR(sep, i) == ' '; i++) spaces++; + for (Py_ssize_t i = len - 1; i >= (Py_ssize_t)spaces && PyUnicode_READ_CHAR(sep, i) == ' '; i--) spaces++; + opts->vertical_tab_bar.padding = spaces + 1; + } + return true; +} + PYWRAP1(set_options) { PyObject *opts; int is_wayland = 0, debug_rendering = 0, debug_font_fallback = 0; @@ -1106,6 +1148,7 @@ PYWRAP1(set_options) { global_state.debug_font_fallback = debug_font_fallback ? true : false; if (!convert_opts_from_python_opts(opts, &global_state.opts)) return NULL; if (!set_wheel_mappings(opts, &global_state.opts)) return NULL; + if (!set_vertical_tab_bar_padding(opts, &global_state.opts)) return NULL; push_modifier_remap_to_glfw(); Py_XDECREF(global_state.options_object); global_state.options_object = Py_NewRef(opts); diff --git a/kitty/state.h b/kitty/state.h index 6cf5b3c70..a945bfe09 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -155,6 +155,9 @@ typedef struct Options { size_t count; } detect_url_regex; bool tab_bar_hidden; + struct { + unsigned padding, min_cols; + } vertical_tab_bar; double font_size; struct { double outer, inner; diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index 9934aee2a..214c8b209 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -655,9 +655,11 @@ def draw_tab_with_powerline( needs_soft_separator = False separator_symbol, soft_separator_symbol = powerline_symbols.get(draw_data.powerline_style, ('', '')) - min_title_length = 1 + 2 - start_draw = 2 is_vertical = draw_data.tab_bar_edge in ('left', 'right') + # Vertical tabs draw their separator in a column of its own, so they need + # room only for the leading space and a single character of the title. + min_title_length = 1 if is_vertical else 1 + 2 + start_draw = 2 first_row = screen.cursor.y if screen.cursor.x == 0: diff --git a/kitty_tests/tab_bar.py b/kitty_tests/tab_bar.py index 6da3731cd..d02725cda 100644 --- a/kitty_tests/tab_bar.py +++ b/kitty_tests/tab_bar.py @@ -195,7 +195,7 @@ class TestTabBar(BaseTest): self.ae(tb.tab_id_at(5, 110), 1) self.ae(tb.tab_id_at(5, 150), 2) - def vertical_tab_bar(self, num_tabs: int = 2, height: int = 160, **opts: object) -> TabBar: + def vertical_tab_bar(self, num_tabs: int = 2, height: int = 160, width: int = 120, **opts: object) -> TabBar: self.set_options( { 'tab_bar_edge': LEFT_EDGE, @@ -204,8 +204,8 @@ class TestTabBar(BaseTest): **opts, } ) - central = region(120, 0, 400, height) - tab_bar = region(0, 0, 120, height) + central = region(width, 0, 400, height) + tab_bar = region(0, 0, width, height) with ( patch('kitty.tab_bar.cell_size_for_window', return_value=(10, 20)), patch('kitty.tab_bar.viewport_for_window', return_value=(central, tab_bar, 400, height, 10, 20)), @@ -221,6 +221,14 @@ class TestTabBar(BaseTest): s = tb.screen return [str(s.line(i)).rstrip() for i in range(s.lines)] + def test_vertical_tab_bar_narrow_powerline(self) -> None: + # A short title fits in a narrow vertical tab bar, since the separator + # is drawn in a column of its own + sep = powerline_symbols['round'][0] + tb = self.vertical_tab_bar(width=40, tab_bar_style='powerline', tab_powerline_style='round', tab_title_template='{index}', tab_title_max_length=1) + self.ae(tb.screen.columns, 4) + self.ae(self.screen_lines(tb)[:3], [f' 1 {sep}', '', f' 2 {sep}']) + def test_vertical_tab_bar_multiline_titles(self) -> None: # newlines are ignored unless tab_title_max_lines allows them, and the # dropped lines are marked with an ellipsis