mirror of
https://github.com/prdlk/dotfiles.git
synced 2026-11-02 19:32:43 +00:00
239 lines
8.2 KiB
Python
239 lines
8.2 KiB
Python
# ── tab_bar.py · kitty tab titles: program icon + repo dir (or nvim's file) ──
|
|
#
|
|
# kitty loads this file from the config dir and exposes `draw_title` as the
|
|
# `{custom}` field of tab_title_template (kitty/tab_bar.py:
|
|
# load_custom_draw_title). kitty still draws the powerline chrome; only the text
|
|
# inside a tab comes from here.
|
|
#
|
|
# A title is `<index> <icon> <label>`:
|
|
# 2 chezmoi repo the tab's cwd belongs to (dir name if no repo)
|
|
# 3 astrocore.lua an editor: the file it is editing
|
|
# 4 chezmoi a known program: its icon, repo as the label
|
|
#
|
|
# No subprocesses, no timers. The repo root is found by walking up for `.git`;
|
|
# the icon comes from the foreground process' cmdline; the edited file comes from
|
|
# the title nvim already sets (~/.config/nvim/lua/plugins/astrocore.lua,
|
|
# custom_title autocmd: "<file> | <org/repo>" / "<file> | Nvim", and
|
|
# "<org/repo>" / "Nvim" with no file). kitty repaints the tab bar when a window
|
|
# title changes, so buffer switches and program changes show up on their own.
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
# Nerd Font glyphs; VictorMono Nerd Font Mono covers every codepoint used here
|
|
# (checked with `fc-list ":charset=<hex>:family=VictorMono Nerd Font Mono"`).
|
|
FOLDER = '\uf07b' # nf-fa-folder — the fallback: "a shell somewhere"
|
|
EDITOR = '\ue62b' # nf-custom-neovim
|
|
|
|
ICONS: dict[str, str] = {
|
|
# editors (label becomes the edited file, see EDITORS below)
|
|
'nvim': EDITOR,
|
|
'vim': EDITOR,
|
|
'nvim-qt': EDITOR,
|
|
'hx': EDITOR,
|
|
'helix': EDITOR,
|
|
# agents / harnesses
|
|
'claude': '\U000f06a9', # nf-md-robot
|
|
'omp': '\uf2db', # nf-fa-microchip
|
|
'opencode': '\uf121', # nf-fa-code
|
|
'crush': '\uf0e7', # nf-fa-bolt
|
|
'codex': '\uf120', # nf-fa-terminal
|
|
'gemini': '\U000f0674', # nf-md-creation
|
|
'aider': '\U000f0068', # nf-md-auto_fix
|
|
# git
|
|
'lazygit': '\uf1d3', # nf-fa-git
|
|
'git': '\uf1d3',
|
|
'gitui': '\uf1d3',
|
|
'tig': '\uf1d3',
|
|
'gh': '\uf113', # nf-fa-github_alt
|
|
# containers
|
|
'lazydocker': '\uf308', # nf-linux-docker
|
|
'docker': '\uf308',
|
|
'docker-compose': '\uf308',
|
|
'podman': '\uf308',
|
|
'kubectl': '\U000f10fe', # nf-md-kubernetes
|
|
# multiplexers / terminals-in-terminals
|
|
'herdr': '\uf009', # nf-fa-th_large — panes
|
|
'tmux': '\uf00a', # nf-fa-th
|
|
# files, monitors, history
|
|
'yazi': '\uf07c', # nf-fa-folder_open
|
|
'ranger': '\uf07c',
|
|
'lf': '\uf07c',
|
|
'btop': '\uf0e4', # nf-fa-dashboard
|
|
'htop': '\uf0e4',
|
|
'top': '\uf0e4',
|
|
'atuin': '\uf1da', # nf-fa-history
|
|
'atuin-pick': '\uf1da',
|
|
'ssh': '\uf0ac', # nf-fa-globe
|
|
'chezmoi': '\uf013', # nf-fa-cog
|
|
# runtimes / build / data
|
|
'python': '\ue235', # nf-dev-python
|
|
'python3': '\ue235',
|
|
'ipython': '\ue235',
|
|
'node': '\ue718', # nf-dev-nodejs_small
|
|
'bun': '\ue718',
|
|
'deno': '\ue718',
|
|
'cargo': '\ue7a8', # nf-dev-rust
|
|
'rustc': '\ue7a8',
|
|
'make': '\uf085', # nf-fa-gears
|
|
'just': '\uf085',
|
|
'psql': '\uf1c0', # nf-fa-database
|
|
'sqlite3': '\uf1c0',
|
|
'duckdb': '\uf1c0',
|
|
'man': '\uf02d', # nf-fa-book
|
|
'less': '\uf02d',
|
|
'bat': '\uf02d',
|
|
'glow': '\uf02d',
|
|
}
|
|
|
|
# These get the edited file as their label instead of the repo.
|
|
EDITORS = frozenset({'nvim', 'vim', 'nvim-qt', 'hx', 'helix'})
|
|
|
|
# Skipped when reading a cmdline, so `node .../claude` is claude and
|
|
# `uv run python x.py` is python — unless nothing else is left, in which case the
|
|
# interpreter itself is the program (a bare `python` REPL).
|
|
INTERPRETERS = frozenset({
|
|
'sh', 'bash', 'zsh', 'fish', 'env', 'sudo', 'doas', 'nohup', 'setsid',
|
|
'node', 'bun', 'deno', 'npx', 'uv', 'uvx', 'python', 'python3',
|
|
})
|
|
|
|
# Subcommands of the runners above ("uv run python x.py"), skipped only after a
|
|
# runner has been skipped so a program genuinely called `run` still resolves.
|
|
RUNNER_WORDS = frozenset({'run', 'exec', 'x', 'tool', '--'})
|
|
|
|
MAX_LEN = 28
|
|
|
|
# wd -> repo root (or ''), memoized: a working dir keeps its answer for the life
|
|
# of the kitty instance, so `git init` in an already-open dir needs a config
|
|
# reload (ctrl+shift+f5) to show up.
|
|
_repo: dict[str, str] = {}
|
|
|
|
|
|
def draw_title(data: dict[str, Any]) -> str:
|
|
tab = data['tab']
|
|
program = _program(_cmdlines(data['tab_id']), tab.active_exe)
|
|
index = data['index']
|
|
if program in EDITORS:
|
|
edited = _nvim_file(data['title'])
|
|
if edited:
|
|
return f'{index} {ICONS[program]} {_elide(edited)}'
|
|
icon = ICONS.get(program, FOLDER)
|
|
wd = tab.active_wd
|
|
label = ''
|
|
if wd:
|
|
label = '~' if wd == os.path.expanduser('~') else os.path.basename(_repo_root(wd) or wd.rstrip('/'))
|
|
return f'{index} {icon} {_elide(label or data["title"])}'
|
|
|
|
|
|
def _cmdlines(tab_id: int) -> list[list[str]]:
|
|
"""argv of every foreground process of the tab's active window.
|
|
|
|
All of them, not just `foreground_cmdline`: an editor commonly has LSP/agent
|
|
children in the same process group, and kitty's single "process for cwd" can
|
|
be either one of them.
|
|
"""
|
|
from kitty.boss import get_boss
|
|
|
|
try:
|
|
tab = get_boss().tab_for_id(tab_id)
|
|
window = tab.active_window if tab is not None else None
|
|
if window is None:
|
|
return []
|
|
return [list(p['cmdline'] or ()) for p in window.child.foreground_processes]
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def _program(cmdlines: list[list[str]], fallback_exe: str) -> str:
|
|
"""Program a tab should be iconified by, across its foreground processes.
|
|
|
|
An editor wins over anything else (nvim with a node LSP child is nvim), then
|
|
the first process with an icon, then whatever the first process is.
|
|
"""
|
|
names = [name for cmdline in cmdlines if (name := _resolve(cmdline))]
|
|
for name in names:
|
|
if name in EDITORS:
|
|
return name
|
|
for name in names:
|
|
if name in ICONS:
|
|
return name
|
|
return names[0] if names else _canonical(os.path.basename(fallback_exe or '').lower())
|
|
|
|
|
|
def _resolve(cmdline: list[str]) -> str:
|
|
"""Program name of one cmdline.
|
|
|
|
Walks past runners and flags: `node …/claude` is claude, an unknown script
|
|
keeps its runtime's icon (`uv run python train.py` is python, `bun run dev`
|
|
is bun), and a bare `python` stays python.
|
|
"""
|
|
runtime = ''
|
|
skipped = False
|
|
for arg in cmdline:
|
|
if arg.startswith('-'):
|
|
continue
|
|
name = os.path.basename(arg).lower()
|
|
if name.endswith(('.js', '.mjs', '.cjs', '.py', '.ts')):
|
|
name = name.rsplit('.', 1)[0]
|
|
if not name:
|
|
continue
|
|
name = _canonical(name)
|
|
if name in INTERPRETERS or (skipped and name in RUNNER_WORDS):
|
|
skipped = True
|
|
runtime = runtime or (name if name in ICONS else '')
|
|
continue
|
|
return name if name in ICONS or not runtime else runtime
|
|
return runtime
|
|
|
|
|
|
def _canonical(name: str) -> str:
|
|
"""`omp-linux-x64` -> `omp`, `docker-compose` stays itself."""
|
|
if name in ICONS:
|
|
return name
|
|
for known in ICONS:
|
|
if name.startswith(known + '-'):
|
|
return known
|
|
return name
|
|
|
|
|
|
def _repo_root(wd: str) -> str:
|
|
root = _repo.get(wd)
|
|
if root is None:
|
|
root = ''
|
|
path = wd
|
|
while True:
|
|
if os.path.exists(os.path.join(path, '.git')):
|
|
root = path
|
|
break
|
|
parent = os.path.dirname(path)
|
|
if parent == path:
|
|
break
|
|
path = parent
|
|
_repo[wd] = root
|
|
return root
|
|
|
|
|
|
def _nvim_file(title: str) -> str:
|
|
"""The file name out of an editor window title, '' when it is editing none.
|
|
|
|
Handles this config's "<file> | <org/repo>" / "<file> | Nvim" and nvim's own
|
|
default "%t - Nvim" / "<file> (<dir>) - NVIM".
|
|
"""
|
|
text = title.strip()
|
|
for suffix in (' - NVIM', ' - Nvim', ' - nvim'):
|
|
if text.endswith(suffix):
|
|
text = text[: -len(suffix)]
|
|
text = text.split(' | ', 1)[0].strip()
|
|
if text.endswith(')') and ' (' in text:
|
|
text = text.split(' (', 1)[0].strip()
|
|
# Left with a bare repo slug ("org/repo") or the placeholder: no file open.
|
|
if not text or '/' in text or text.lower() == 'nvim':
|
|
return ''
|
|
return text
|
|
|
|
|
|
def _elide(label: str) -> str:
|
|
return label if len(label) <= MAX_LEN else label[: MAX_LEN - 1] + '…'
|