1
0
Fork 0
dotfiles/.config/kitty/tab_bar.py

129 lines
3.1 KiB
Python
Raw Normal View History

# pyright: reportMissingImports=false
2022-07-19 10:37:00 +02:00
from datetime import datetime
import subprocess
import os
from kitty.boss import get_boss
from kitty.fast_data_types import Screen, add_timer
from kitty.tab_bar import (
DrawData,
ExtraData,
Formatter,
TabBarData,
as_rgb,
draw_attributed_string,
draw_title,
)
from kitty.utils import color_as_int
2022-07-19 10:37:00 +02:00
RIGHT_MARGIN = 1
def _draw_left_status(
draw_data: DrawData,
screen: Screen,
tab: TabBarData,
before: int,
max_title_length: int,
index: int,
is_last: bool,
extra_data: ExtraData,
) -> int:
print(extra_data)
if draw_data.leading_spaces:
screen.draw(" " * draw_data.leading_spaces)
draw_title(draw_data, screen, tab, index)
trailing_spaces = min(max_title_length - 1, draw_data.trailing_spaces)
max_title_length -= trailing_spaces
extra = screen.cursor.x - before - max_title_length
if extra > 0:
screen.cursor.x -= extra + 1
screen.draw("")
if trailing_spaces:
screen.draw(" " * trailing_spaces)
end = screen.cursor.x
screen.cursor.bold = screen.cursor.italic = False
screen.cursor.fg = 0
if not is_last:
screen.cursor.bg = as_rgb(color_as_int(draw_data.inactive_bg))
screen.draw(draw_data.sep)
screen.cursor.bg = 0
return end
def _draw_right_status(draw_data: DrawData, screen: Screen, is_last: bool) -> int:
if not is_last:
return 0
draw_attributed_string(Formatter.reset, screen)
2022-07-19 10:37:00 +02:00
time = datetime.now().strftime(" %H:%M")
date = datetime.now().strftime(" %d.%m.%Y")
# TODO: Figure out how to import psutils so I don't have to call a separate script
bat = subprocess.getoutput(os.path.expandvars("~/.local/bin/battery"))
cells = [
(draw_data.active_fg, bat),
(draw_data.active_fg, time),
(draw_data.inactive_fg, date),
]
2022-07-19 10:37:00 +02:00
right_status_length = RIGHT_MARGIN
for i in cells:
right_status_length += (len(str(i[1])))
draw_spaces = screen.columns - screen.cursor.x - right_status_length
if draw_spaces > 0:
screen.draw(" " * draw_spaces)
screen.cursor.fg = 0
for color, status in cells:
screen.cursor.fg = as_rgb(color_as_int(color))
screen.draw(status)
screen.cursor.bg = 0
if screen.columns - screen.cursor.x > right_status_length:
screen.cursor.x = screen.columns - right_status_length
return screen.cursor.x
def _redraw_tab_bar():
tm = get_boss().active_tab_manager
if tm is not None:
tm.mark_tab_bar_dirty()
timer_id = None
def draw_tab(
draw_data: DrawData,
screen: Screen,
tab: TabBarData,
before: int,
max_title_length: int,
index: int,
is_last: bool,
extra_data: ExtraData,
) -> int:
global timer_id
if timer_id is None:
2022-07-19 10:37:00 +02:00
timer_id = add_timer(_redraw_tab_bar, 15.0, True)
_draw_left_status(
draw_data,
screen,
tab,
before,
max_title_length,
index,
is_last,
extra_data,
)
_draw_right_status(
draw_data,
screen,
is_last,
)
return screen.cursor.x