Skip to content

Commit

Permalink
feat(layout): add ability to generate custom tab titles
Browse files Browse the repository at this point in the history
  • Loading branch information
aravinda0 committed Jun 11, 2024
1 parent cc967a5 commit 0c8e097
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ Click on the image to open a full-size web view.
|`tab_bar.tab.font_size` | 15 | Font size to use for tab titles |
|`tab_bar.tab.active.bg_color` | Gruvbox.bg4 | Background color of active tabs |
|`tab_bar.tab.active.fg_color` | Gruvbox.fg1 | Foreground text color of the active tab |
|`tab_bar.tab.title_provider` | None | A callback that generates the title for a tab. The callback accepts 3 parameters<br>and returns the final title string. The params are:<br>1. `index`:<br>&nbsp;&nbsp;&nbsp;&nbsp;The index of the current tab in the list of tabs.<br>2. `active_pane`:<br>&nbsp;&nbsp;&nbsp;&nbsp;The active `Pane` instance under the current tab. A `Pane` is just a<br>&nbsp;&nbsp;&nbsp;&nbsp;container for a window and can be accessed via `pane.window`.<br>3. `tab`:<br>&nbsp;&nbsp;&nbsp;&nbsp;The current `Tab` instance.<br><br>For example, here's a callback that returns the active window's title:<br>def my_title_provider(index, active_pane, tab):<br>&nbsp;&nbsp;&nbsp;&nbsp;return active_pane.window.name |
|`auto_cwd_for_terminals` | True | (Experimental)<br><br>If `True`, when spawning new windows by specifying a `program` that happens to<br>be a well-known terminal emulator, will try to open the new terminal window in<br>same working directory as the last focused window. |
|`restore.threshold_seconds` | 4 | You likely don't need to tweak this.<br>Controls the time within which a persisted state file is considered to be from a<br>recent qtile config-reload/restart event. If the persisted file is this many<br>seconds old, we restore our window tree from it. |

Expand Down
19 changes: 19 additions & 0 deletions src/qtile_bonsai/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ class AddClientMode(enum.Enum):
"Foreground text color of the active tab",
default_value_label="Gruvbox.fg1",
),
LayoutOption(
"tab_bar.tab.title_provider",
None,
"""
A callback that generates the title for a tab. The callback accepts 3
parameters and returns the final title string. The params are:
1. `index`:
The index of the current tab in the list of tabs.
2. `active_pane`:
The active `Pane` instance under the current tab. A `Pane` is just a
container for a window and can be accessed via `pane.window`.
3. `tab`:
The current `Tab` instance.
For example, here's a callback that returns the active window's title:
def my_title_provider(index, active_pane, tab):
return active_pane.window.name
""",
),
LayoutOption(
"auto_cwd_for_terminals",
True,
Expand Down
11 changes: 10 additions & 1 deletion src/qtile_bonsai/tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2023-present Aravinda Rao <maniacalace@gmail.com>
# SPDX-License-Identifier: MIT

from collections.abc import Callable

from libqtile.backend.base.drawer import Drawer, TextLayout
from libqtile.backend.base.window import Internal, Window
from libqtile.config import ScreenRect
Expand Down Expand Up @@ -67,6 +69,9 @@ def render(self, screen_rect: ScreenRect, tree: "BonsaiTree"):
tab_font_size: float = tree.get_config("tab_bar.tab.font_size", level=level)
tab_bg_color: str = tree.get_config("tab_bar.tab.bg_color", level=level)
tab_fg_color: str = tree.get_config("tab_bar.tab.fg_color", level=level)
tab_title_provider: Callable[[int, BonsaiPane, BonsaiTab], str] = (
tree.get_config("tab_bar.tab.title_provider", level=level)
)

tab_active_bg_color: str = tree.get_config(
"tab_bar.tab.active.bg_color", level=level
Expand Down Expand Up @@ -127,7 +132,11 @@ def render(self, screen_rect: ScreenRect, tree: "BonsaiTree"):
padding=tab_padding,
)

tab_title = f"{i + 1}: {tab.title}" if tab.title else f"{i + 1}"
if tab_title_provider is not None:
active_pane = tree.find_mru_pane(start_node=tab)
tab_title = tab_title_provider(i, active_pane, tab)
else:
tab_title = f"{i + 1}: {tab.title}" if tab.title else f"{i + 1}"
if len(tab_title) > per_tab_max_chars:
tab_title = f"{tab_title[:per_tab_max_chars - 1]}…"

Expand Down

0 comments on commit 0c8e097

Please sign in to comment.