Add pyenv indicator in tab bar
This commit is contained in:
parent
e1fd61e0fa
commit
504d52b78d
4 changed files with 237 additions and 85 deletions
|
@ -1,6 +1,6 @@
|
|||
# pyright: reportMissingImports=false
|
||||
# from pprint import pprint
|
||||
from datetime import datetime
|
||||
# from kitty.window import CwdRequest
|
||||
from kitty.boss import get_boss
|
||||
from kitty.fast_data_types import Screen, add_timer, get_options
|
||||
from kitty.utils import color_as_int
|
||||
|
@ -15,40 +15,13 @@ from kitty.tab_bar import (
|
|||
)
|
||||
|
||||
opts = get_options()
|
||||
text_fg = as_rgb(color_as_int(opts.color15))
|
||||
icon_fg = as_rgb(color_as_int(opts.color16))
|
||||
icon_bg = as_rgb(color_as_int(opts.color8))
|
||||
bat_text_color = as_rgb(color_as_int(opts.color15))
|
||||
clock_color = as_rgb(color_as_int(opts.color15))
|
||||
date_color = as_rgb(color_as_int(opts.color8))
|
||||
SEPARATOR_SYMBOL, SOFT_SEPARATOR_SYMBOL = ("", "")
|
||||
RIGHT_MARGIN = 0
|
||||
REFRESH_TIME = 1
|
||||
ICON = " "
|
||||
UNPLUGGED_ICONS = {
|
||||
10: "",
|
||||
20: "",
|
||||
30: "",
|
||||
40: "",
|
||||
50: "",
|
||||
60: "",
|
||||
70: "",
|
||||
80: "",
|
||||
90: "",
|
||||
100: "",
|
||||
}
|
||||
PLUGGED_ICONS = {
|
||||
1: "",
|
||||
}
|
||||
UNPLUGGED_COLORS = {
|
||||
15: as_rgb(color_as_int(opts.color1)),
|
||||
16: as_rgb(color_as_int(opts.color15)),
|
||||
}
|
||||
PLUGGED_COLORS = {
|
||||
15: as_rgb(color_as_int(opts.color1)),
|
||||
16: as_rgb(color_as_int(opts.color6)),
|
||||
99: as_rgb(color_as_int(opts.color6)),
|
||||
100: as_rgb(color_as_int(opts.color2)),
|
||||
}
|
||||
|
||||
|
||||
def _draw_icon(screen: Screen, index: int) -> int:
|
||||
|
@ -130,44 +103,10 @@ def _redraw_tab_bar(_):
|
|||
tm.mark_tab_bar_dirty()
|
||||
|
||||
|
||||
def get_battery_cells() -> list:
|
||||
try:
|
||||
with open("/sys/class/power_supply/BAT0/status", "r") as f:
|
||||
status = f.read()
|
||||
with open("/sys/class/power_supply/BAT0/capacity", "r") as f:
|
||||
percent = int(f.read())
|
||||
if status == "Discharging\n":
|
||||
# TODO: declare the lambda once and don't repeat the code
|
||||
icon_color = UNPLUGGED_COLORS[
|
||||
min(UNPLUGGED_COLORS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
icon = UNPLUGGED_ICONS[
|
||||
min(UNPLUGGED_ICONS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
elif status == "Not charging\n":
|
||||
icon_color = UNPLUGGED_COLORS[
|
||||
min(UNPLUGGED_COLORS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
icon = PLUGGED_ICONS[
|
||||
min(PLUGGED_ICONS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
else:
|
||||
icon_color = PLUGGED_COLORS[
|
||||
min(PLUGGED_COLORS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
icon = PLUGGED_ICONS[
|
||||
min(PLUGGED_ICONS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
percent_cell = (bat_text_color, str(percent) + "% ")
|
||||
icon_cell = (icon_color, icon)
|
||||
return [percent_cell, icon_cell]
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
|
||||
|
||||
timer_id = None
|
||||
right_status_length = -1
|
||||
|
||||
|
||||
def draw_tab(
|
||||
draw_data: DrawData,
|
||||
screen: Screen,
|
||||
|
@ -182,11 +121,15 @@ def draw_tab(
|
|||
global right_status_length
|
||||
if timer_id is None:
|
||||
timer_id = add_timer(_redraw_tab_bar, REFRESH_TIME, True)
|
||||
clock = datetime.now().strftime(" %H:%M")
|
||||
date = datetime.now().strftime(" %d.%m.%Y")
|
||||
cells = get_battery_cells()
|
||||
cells.append((clock_color, clock))
|
||||
cells.append((date_color, date))
|
||||
active_window = get_boss().active_window_for_cwd
|
||||
# active_dir = CwdRequest(active_window)
|
||||
active_pid = active_window.child.pid
|
||||
try:
|
||||
with open(f"/tmp/current_venv-{active_pid}", "r") as f:
|
||||
env = f.read()
|
||||
except FileNotFoundError:
|
||||
env = ""
|
||||
cells = [(text_fg, env)]
|
||||
right_status_length = RIGHT_MARGIN
|
||||
for cell in cells:
|
||||
right_status_length += len(str(cell[1]))
|
||||
|
|
213
.config/kitty/tab_barOLD.py
Executable file
213
.config/kitty/tab_barOLD.py
Executable file
|
@ -0,0 +1,213 @@
|
|||
# pyright: reportMissingImports=false
|
||||
from pprint import pprint
|
||||
from datetime import datetime
|
||||
from kitty.window import CwdRequest
|
||||
from kitty.boss import get_boss
|
||||
from kitty.fast_data_types import Screen, add_timer, get_options
|
||||
from kitty.utils import color_as_int
|
||||
from kitty.tab_bar import (
|
||||
DrawData,
|
||||
ExtraData,
|
||||
Formatter,
|
||||
TabBarData,
|
||||
as_rgb,
|
||||
draw_attributed_string,
|
||||
draw_title,
|
||||
)
|
||||
|
||||
opts = get_options()
|
||||
icon_fg = as_rgb(color_as_int(opts.color16))
|
||||
icon_bg = as_rgb(color_as_int(opts.color8))
|
||||
bat_text_color = as_rgb(color_as_int(opts.color15))
|
||||
clock_color = as_rgb(color_as_int(opts.color15))
|
||||
date_color = as_rgb(color_as_int(opts.color8))
|
||||
SEPARATOR_SYMBOL, SOFT_SEPARATOR_SYMBOL = ("", "")
|
||||
RIGHT_MARGIN = 0
|
||||
REFRESH_TIME = 1
|
||||
ICON = " "
|
||||
UNPLUGGED_ICONS = {
|
||||
10: "",
|
||||
20: "",
|
||||
30: "",
|
||||
40: "",
|
||||
50: "",
|
||||
60: "",
|
||||
70: "",
|
||||
80: "",
|
||||
90: "",
|
||||
100: "",
|
||||
}
|
||||
PLUGGED_ICONS = {
|
||||
1: "",
|
||||
}
|
||||
UNPLUGGED_COLORS = {
|
||||
15: as_rgb(color_as_int(opts.color1)),
|
||||
16: as_rgb(color_as_int(opts.color15)),
|
||||
}
|
||||
PLUGGED_COLORS = {
|
||||
15: as_rgb(color_as_int(opts.color1)),
|
||||
16: as_rgb(color_as_int(opts.color6)),
|
||||
99: as_rgb(color_as_int(opts.color6)),
|
||||
100: as_rgb(color_as_int(opts.color2)),
|
||||
}
|
||||
|
||||
|
||||
def _draw_icon(screen: Screen, index: int) -> int:
|
||||
if index != 1:
|
||||
return 0
|
||||
fg, bg = screen.cursor.fg, screen.cursor.bg
|
||||
screen.cursor.fg = icon_fg
|
||||
screen.cursor.bg = icon_bg
|
||||
screen.draw(ICON)
|
||||
screen.cursor.fg, screen.cursor.bg = fg, bg
|
||||
screen.cursor.x = len(ICON)
|
||||
return screen.cursor.x
|
||||
|
||||
|
||||
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:
|
||||
if screen.cursor.x >= screen.columns - right_status_length:
|
||||
return screen.cursor.x
|
||||
tab_bg = screen.cursor.bg
|
||||
tab_fg = screen.cursor.fg
|
||||
default_bg = as_rgb(int(draw_data.default_bg))
|
||||
if extra_data.next_tab:
|
||||
next_tab_bg = as_rgb(draw_data.tab_bg(extra_data.next_tab))
|
||||
needs_soft_separator = next_tab_bg == tab_bg
|
||||
else:
|
||||
next_tab_bg = default_bg
|
||||
needs_soft_separator = False
|
||||
if screen.cursor.x <= len(ICON):
|
||||
screen.cursor.x = len(ICON)
|
||||
screen.draw(" ")
|
||||
screen.cursor.bg = tab_bg
|
||||
# if tab.title.rindex(tab.title[-1]) + 1 > 15:
|
||||
# title = f'{os.path.splitext(tab.title)[0][:14-len(os.path.splitext(tab.title))]}…{os.path.splitext(tab.title)[1]}'
|
||||
# tab = TabBarData(title, tab.is_active, tab.needs_attention, tab.num_windows, tab.num_window_groups, tab.layout_name, tab.has_activity_since_last_focus, tab.active_fg, tab.active_bg, tab.inactive_fg, tab.inactive_bg)
|
||||
draw_title(draw_data, screen, tab, index)
|
||||
if not needs_soft_separator:
|
||||
screen.draw(" ")
|
||||
screen.cursor.fg = tab_bg
|
||||
screen.cursor.bg = next_tab_bg
|
||||
screen.draw(SEPARATOR_SYMBOL)
|
||||
else:
|
||||
prev_fg = screen.cursor.fg
|
||||
if tab_bg == tab_fg:
|
||||
screen.cursor.fg = default_bg
|
||||
elif tab_bg != default_bg:
|
||||
c1 = draw_data.inactive_bg.contrast(draw_data.default_bg)
|
||||
c2 = draw_data.inactive_bg.contrast(draw_data.inactive_fg)
|
||||
if c1 < c2:
|
||||
screen.cursor.fg = default_bg
|
||||
screen.draw(" " + SOFT_SEPARATOR_SYMBOL)
|
||||
screen.cursor.fg = prev_fg
|
||||
return screen.cursor.x
|
||||
|
||||
|
||||
def _draw_right_status(screen: Screen, is_last: bool, cells: list) -> int:
|
||||
if not is_last:
|
||||
return 0
|
||||
draw_attributed_string(Formatter.reset, screen)
|
||||
screen.cursor.x = screen.columns - right_status_length
|
||||
screen.cursor.fg = 0
|
||||
for color, status in cells:
|
||||
screen.cursor.fg = color
|
||||
screen.draw(status)
|
||||
screen.cursor.bg = 0
|
||||
return screen.cursor.x
|
||||
|
||||
|
||||
|
||||
|
||||
def get_battery_cells() -> list:
|
||||
try:
|
||||
with open("/sys/class/power_supply/BAT0/status", "r") as f:
|
||||
status = f.read()
|
||||
with open("/sys/class/power_supply/BAT0/capacity", "r") as f:
|
||||
percent = int(f.read())
|
||||
if status == "Discharging\n":
|
||||
# TODO: declare the lambda once and don't repeat the code
|
||||
icon_color = UNPLUGGED_COLORS[
|
||||
min(UNPLUGGED_COLORS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
icon = UNPLUGGED_ICONS[
|
||||
min(UNPLUGGED_ICONS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
elif status == "Not charging\n":
|
||||
icon_color = UNPLUGGED_COLORS[
|
||||
min(UNPLUGGED_COLORS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
icon = PLUGGED_ICONS[
|
||||
min(PLUGGED_ICONS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
else:
|
||||
icon_color = PLUGGED_COLORS[
|
||||
min(PLUGGED_COLORS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
icon = PLUGGED_ICONS[
|
||||
min(PLUGGED_ICONS.keys(), key=lambda x: abs(x - percent))
|
||||
]
|
||||
percent_cell = (bat_text_color, str(percent) + "% ")
|
||||
icon_cell = (icon_color, icon)
|
||||
return [percent_cell, icon_cell]
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
|
||||
def _redraw_tab_bar(_):
|
||||
tm = get_boss().active_tab_manager
|
||||
if tm is not None:
|
||||
tm.mark_tab_bar_dirty()
|
||||
|
||||
timer_id = None
|
||||
right_status_length = -1
|
||||
|
||||
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
|
||||
global right_status_length
|
||||
if timer_id is None:
|
||||
timer_id = add_timer(_redraw_tab_bar, REFRESH_TIME, True)
|
||||
clock = datetime.now().strftime(" %H:%M")
|
||||
date = datetime.now().strftime(" %d.%m.%Y")
|
||||
# pprint(CwdRequest(get_boss().active_window_for_cwd))
|
||||
pprint(get_boss().active_window_for_cwd)
|
||||
cells = get_battery_cells()
|
||||
cells.append((clock_color, clock))
|
||||
cells.append((date_color, date))
|
||||
right_status_length = RIGHT_MARGIN
|
||||
for cell in cells:
|
||||
right_status_length += len(str(cell[1]))
|
||||
|
||||
_draw_icon(screen, index)
|
||||
_draw_left_status(
|
||||
draw_data,
|
||||
screen,
|
||||
tab,
|
||||
before,
|
||||
max_title_length,
|
||||
index,
|
||||
is_last,
|
||||
extra_data,
|
||||
)
|
||||
_draw_right_status(
|
||||
screen,
|
||||
is_last,
|
||||
cells,
|
||||
)
|
||||
return screen.cursor.x
|
|
@ -1,5 +1,8 @@
|
|||
# Enable colors and change prompt:
|
||||
autoload -U colors && colors # Load colors
|
||||
autoload -U colors && colors
|
||||
autoload edit-command-line && zle -N edit-command-line
|
||||
autoload -U add-zsh-hook
|
||||
autoload -U compinit
|
||||
autoload -Uz edit-command-line
|
||||
PS1="%B%F{blue}%n%F{cyan}@%F{blue}%m %F{magenta}[%f%3~%F{magenta}] %(?.%F{green}.%F{red})»%f%b "
|
||||
RPS1="%(?..%F{red}%?)"
|
||||
stty stop undef # Disable ctrl-s to freeze terminal.
|
||||
|
@ -16,14 +19,12 @@ HISTFILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/history"
|
|||
[ -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/zshnameddirrc" ] && source "${XDG_CONFIG_HOME:-$HOME/.config}/shell/zshnameddirrc"
|
||||
|
||||
# Basic auto/tab complete:
|
||||
autoload -U compinit
|
||||
zstyle ':completion:*' menu select
|
||||
zmodload zsh/complist
|
||||
compinit
|
||||
_comp_options+=(globdots) # Include hidden files.
|
||||
|
||||
# vi mode
|
||||
autoload -Uz edit-command-line
|
||||
zle -N edit-command-line
|
||||
bindkey -M vicmd v edit-command-line
|
||||
bindkey -v
|
||||
|
@ -91,12 +92,16 @@ lg () {
|
|||
[ -d "$(pwd)/.git" ] && lazygit -p $(pwd)
|
||||
zle reset-prompt
|
||||
}
|
||||
|
||||
zle -N lg{,}
|
||||
bindkey '^g' lg
|
||||
|
||||
# This function simply writes the venv of the last executed command to a file to be read by external status indicators
|
||||
function write_venv(){
|
||||
echo -n "${VIRTUAL_ENV:t}" > "/tmp/current_venv-$$"
|
||||
}
|
||||
add-zsh-hook precmd write_venv
|
||||
|
||||
# Edit line in vim with ctrl-e:
|
||||
autoload edit-command-line; zle -N edit-command-line
|
||||
bindkey '^e' edit-command-line
|
||||
|
||||
# other keybinds
|
||||
|
|
|
@ -1,21 +1,15 @@
|
|||
PYENV_DIR="${PYENVS_HOME:-${XDG_DATA_HOME:-$HOME/.local/share}/virtualenv}"
|
||||
PS1_DEFAULT="$PS1"
|
||||
PS1_PRE="${PYENV_PROMPT:- %F{yellow\}%F{reset\} }"
|
||||
function chpwd_activate(){
|
||||
[[ "$(pwd)" == "/" ]] && return 0
|
||||
for pydir in $(ls $PYENV_DIR); do
|
||||
if [[ "$(pwd|sed -e s@/@_@g|cut -c2-)" =~ "^${pydir}$" ]] || [[ "r-$(pwd|sed -e s@/@_@g|cut -c2-)" =~ "^${pydir}(_.+)?$" ]]; then
|
||||
if [ "x$VIRTUAL_ENV" != "x$PYENV_DIR/$pydir" ]; then
|
||||
export PS1="$PS1_PRE$PS1_DEFAULT"
|
||||
export VIRTUAL_ENV_DISABLE_PROMPT=1
|
||||
source "$PYENV_DIR/$pydir/bin/activate"
|
||||
unset VIRTUAL_ENV_DISABLE_PROMPT
|
||||
fi
|
||||
return
|
||||
fi
|
||||
done
|
||||
if [ "x$VIRTUAL_ENV" != "x" ]; then
|
||||
export PS1="$PS1_DEFAULT"
|
||||
deactivate
|
||||
fi
|
||||
}
|
||||
|
@ -29,10 +23,7 @@ function venv(){
|
|||
mkdir "$PYENV_DIR" -p
|
||||
python3 -m venv "$PYENV_DIR/${envdir}"
|
||||
echo "Activating virtual env ${envdir}"
|
||||
export VIRTUAL_ENV_DISABLE_PROMPT=1
|
||||
# source "$PYENV_DIR/${envdir}/bin/activate" && [[ ! $PS1 =~ "^$PS1_NEW" ]] && export PS1="$PS1_PRE$PS1_DEFAULT"
|
||||
source "$PYENV_DIR/${envdir}/bin/activate" && export PS1="$PS1_PRE$PS1_DEFAULT"
|
||||
unset VIRTUAL_ENV_DISABLE_PROMPT
|
||||
source "$PYENV_DIR/${envdir}/bin/activate"
|
||||
else
|
||||
echo "A venv for this path already exists"
|
||||
fi
|
||||
|
|
Loading…
Add table
Reference in a new issue