autopyenv fixes
This commit is contained in:
parent
bef6054ba4
commit
ac1be14b49
3 changed files with 47 additions and 140 deletions
|
@ -37,7 +37,8 @@ window_alert_on_bell no
|
||||||
#: }}}
|
#: }}}
|
||||||
|
|
||||||
#: Tab bar {{{
|
#: Tab bar {{{
|
||||||
tab_bar_style hidden
|
tab_bar_style custom
|
||||||
|
tab_bar_min_tabs 0
|
||||||
#: }}}
|
#: }}}
|
||||||
|
|
||||||
#: Color scheme {{{
|
#: Color scheme {{{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# from pprint import pprint
|
# from pprint import pprint
|
||||||
# from kitty.window import CwdRequest
|
# from kitty.window import CwdRequest
|
||||||
from kitty.boss import get_boss
|
from kitty.boss import get_boss
|
||||||
from kitty.fast_data_types import Screen, add_timer, get_options
|
from kitty.fast_data_types import Screen, get_options
|
||||||
from kitty.utils import color_as_int
|
from kitty.utils import color_as_int
|
||||||
from kitty.tab_bar import (
|
from kitty.tab_bar import (
|
||||||
DrawData,
|
DrawData,
|
||||||
|
@ -11,100 +11,18 @@ from kitty.tab_bar import (
|
||||||
TabBarData,
|
TabBarData,
|
||||||
as_rgb,
|
as_rgb,
|
||||||
draw_attributed_string,
|
draw_attributed_string,
|
||||||
draw_title,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
opts = get_options()
|
opts = get_options()
|
||||||
text_fg = as_rgb(color_as_int(opts.color15))
|
text_fg = as_rgb(color_as_int(opts.foreground))
|
||||||
icon_fg = as_rgb(color_as_int(opts.color16))
|
icon_fg = as_rgb(color_as_int(opts.color3))
|
||||||
icon_bg = as_rgb(color_as_int(opts.color8))
|
bg = as_rgb(color_as_int(opts.color8))
|
||||||
SEPARATOR_SYMBOL, SOFT_SEPARATOR_SYMBOL = ("", "")
|
|
||||||
RIGHT_MARGIN = 0
|
|
||||||
REFRESH_TIME = 1
|
|
||||||
ICON = " "
|
|
||||||
|
|
||||||
|
|
||||||
def _draw_icon(screen: Screen, index: int) -> int:
|
def draw(screen: Screen, fg: int, bg: int, text: str) -> None:
|
||||||
if index != 1:
|
screen.cursor.fg = fg
|
||||||
return 0
|
screen.cursor.bg = bg
|
||||||
fg, bg = screen.cursor.fg, screen.cursor.bg
|
screen.draw(text)
|
||||||
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 _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(
|
def draw_tab(
|
||||||
|
@ -117,39 +35,27 @@ def draw_tab(
|
||||||
is_last: bool,
|
is_last: bool,
|
||||||
extra_data: ExtraData,
|
extra_data: ExtraData,
|
||||||
) -> int:
|
) -> int:
|
||||||
global timer_id
|
|
||||||
global right_status_length
|
|
||||||
if timer_id is None:
|
|
||||||
timer_id = add_timer(_redraw_tab_bar, REFRESH_TIME, True)
|
|
||||||
active_window = get_boss().active_window_for_cwd
|
active_window = get_boss().active_window_for_cwd
|
||||||
# active_dir = CwdRequest(active_window)
|
# active_dir = CwdRequest(active_window)
|
||||||
active_pid = active_window.child.pid
|
active_pid = active_window.child.pid
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(f"/tmp/current_venv-{active_pid}", "r") as f:
|
with open(f"/tmp/current_venv-{active_pid}", "r") as f:
|
||||||
env = f.read()
|
env = f.read()
|
||||||
if env != "":
|
if env != "":
|
||||||
env = f" {env}"
|
env = f"{env}"
|
||||||
|
else:
|
||||||
|
return screen.cursor.x
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
env = ""
|
print(f"KITTY TAB BAR ERROR: Can't find /tmp/current_venv for {active_pid}")
|
||||||
cells = [(text_fg, env)]
|
return screen.cursor.x
|
||||||
right_status_length = RIGHT_MARGIN
|
|
||||||
for cell in cells:
|
screen.cursor.x = screen.columns - len(str(env)) - 4
|
||||||
right_status_length += len(str(cell[1]))
|
|
||||||
|
draw(screen, bg, 0, "")
|
||||||
|
draw(screen, 0, bg, " ")
|
||||||
|
draw(screen, icon_fg, bg, "")
|
||||||
|
draw(screen, text_fg, bg, " " + env)
|
||||||
|
|
||||||
_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
|
return screen.cursor.x
|
||||||
|
|
|
@ -1,33 +1,33 @@
|
||||||
# vim:set ft=zsh
|
# vim:set ft=zsh
|
||||||
PYENV_DIR="${PYENVS_HOME:-$XDG_DATA_HOME}/virtualenv"
|
PYENV_DIR="${PYENVS_HOME:-$XDG_DATA_HOME}/virtualenv"
|
||||||
function chpwd_activate(){
|
function chpwd_activate(){
|
||||||
[[ "$(pwd)" == "/" ]] && return 0
|
[[ "$(pwd)" == "/" ]] && return 0
|
||||||
for pydir in $(ls $PYENV_DIR); do
|
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 [[ "$(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
|
if [ "x$VIRTUAL_ENV" != "x$PYENV_DIR/$pydir" ]; then
|
||||||
source "$PYENV_DIR/$pydir/bin/activate"
|
source "$PYENV_DIR/$pydir/bin/activate"
|
||||||
fi
|
fi
|
||||||
return
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "x$VIRTUAL_ENV" != "x" ]; then
|
||||||
|
deactivate
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
if [ "x$VIRTUAL_ENV" != "x" ]; then
|
|
||||||
deactivate
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function venv(){
|
function venv(){
|
||||||
[[ "$(pwd)" == "/" ]] && echo "Cannot create venv at root" && return 1
|
[[ "$(pwd)" == "/" ]] && echo "Cannot create venv at root" && return 1
|
||||||
[[ "$(pwd)" != "/" ]] && envdir=$(pwd | sed -e 's|/|~|g' | cut -c2-)
|
[[ "$(pwd)" != "/" ]] && envdir=$(pwd | sed -e 's|/|~|g' | cut -c2-)
|
||||||
[[ "$1" == "-r" ]] && envdir="r-$envdir" # create a venv that will be activated for all subdirectories as well
|
[[ "$1" == "-r" ]] && envdir="r-$envdir" # create a venv that will be activated for all subdirectories as well
|
||||||
if [ ! -e "$PYENV_DIR/${envdir}" ]; then
|
if [ ! -e "$PYENV_DIR/${envdir}" ]; then
|
||||||
echo "Creating python venv for ${envdir}.."
|
echo "Creating python venv for ${envdir}.."
|
||||||
mkdir "$PYENV_DIR" -p
|
mkdir "$PYENV_DIR" -p
|
||||||
python3 -m venv "$PYENV_DIR/${envdir}"
|
python3 -m venv "$PYENV_DIR/${envdir}"
|
||||||
echo "Activating virtual env ${envdir}"
|
echo "Activating virtual env ${envdir}"
|
||||||
source "$PYENV_DIR/${envdir}/bin/activate"
|
source "$PYENV_DIR/${envdir}/bin/activate"
|
||||||
else
|
else
|
||||||
echo "A venv for this path already exists"
|
echo "A venv for this path already exists"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
autoload -U add-zsh-hook
|
autoload -U add-zsh-hook
|
||||||
|
|
Loading…
Add table
Reference in a new issue