+/config

my dotfiles and nix configuration

git clone https://git.julia.blue/+/config

Julia RyanAdd ghosttyd33df88

main
3.0 KiB91 linesraw
1# This is mostly taken from kitty's shell integration docs. The subset of
2# features used here should work with wezterm and ghostty as well:
3# - OSC133 prompt awareness
4# - changing window title to current dir/host/command
5# - changing cursor shape to line in the line editor
6# https://sw.kovidgoyal.net/kitty/shell-integration/#how-it-works
7# https://wezfurlong.org/wezterm/shell-integration.html
8# https://ghostty.org/docs/features/shell-integration
9
10typeset -gi _tty_fd
11{
12    zmodload zsh/system && {
13        { [[ -w $TTY     ]] && sysopen -o cloexec -wu _tty_fd -- $TTY     } ||
14        { [[ -w /dev/tty ]] && sysopen -o cloexec -wu _tty_fd -- /dev/tty }
15    }
16} 2>/dev/null || (( _tty_fd = 1 ))
17
18typeset -gi _prompt_state
19_prompt_precmd() {
20    local -i cmd_status=$?
21    if ! zle; then
22        if (( _prompt_state == 1 )); then
23            print -nu $_tty_fd '\e]133;D;'$cmd_status'\a'
24            (( _prompt_state = 2 ))
25        elif (( _prompt_state == 2 )); then
26            print -nu $_tty_fd '\e]133;D\a'
27        fi
28    fi
29
30    local mark1=$'%{\e]133;A\a%}'
31    if [[ -o prompt_percent ]]; then
32        typeset -g precmd_functions
33        if [[ ${precmd_functions[-1]} == _prompt_precmd ]]; then
34            local mark2=$'%{\e]133;A;k=s\a%}'
35            [[ $PS1 == *$mark1* ]] || PS1=${mark1}${PS1}
36            [[ $PS2 == *$mark2* ]] || PS2=${mark2}${PS2}
37            (( _prompt_state = 2 ))
38        else
39            precmd_functions=(${precmd_functions:#_prompt_precmd} _prompt_precmd)
40            if ! zle; then
41                print -rnu $_tty_fd -- $mark1[3,-3]
42                (( _prompt_state = 2 ))
43            fi
44        fi
45    elif ! zle; then
46        print -rnu $_tty_fd -- $mark1[3,-3]
47        (( _prompt_state = 2 ))
48    fi
49}
50
51_prompt_preexec() {
52    PS1=${PS1//$'%{\e]133;A\a%}'}
53    PS2=${PS2//$'%{\e]133;A;k=s\a%}'}
54    print -nu $_tty_fd '\e]133;C\a'
55    (( _prompt_state = 1 ))
56
57    print -nu $_tty_fd '\e[1 q' # block cursor
58}
59
60# use "application mode" inside the line editor so keybinds don't change
61zle-line-init() {
62    print -nu "$_tty_fd" '\e[5 q' # line cursor
63    echoti smkx
64}
65zle-line-finish() { echoti rmkx; }
66if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
67    zle -N zle-line-init && zle -N zle-line-finish
68fi
69
70local is_ssh_session="n"
71if [[ -n "$KITTY_PID" ]]; then
72elif [[ -n "$SSH_TTY" ]]; then
73    is_ssh_session="y"
74elif [[ -n "$(command -v who)" ]]; then
75    [[ "$(command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]] && is_ssh_session="y"
76fi
77
78if [[ "$is_ssh_session" == "y" ]]; then
79    functions[_prompt_precmd]+="
80        print -Prnu $_tty_fd \$'\\e]2;'\"%m: \${(%):-%(4~|…/%3~|%~)}\"\$'\\a'"
81    functions[_prompt_preexec]+="
82        print -Prnu $_tty_fd \$'\\e]2;'\"%m: \${(V)1}\"\$'\\a'"
83else
84    functions[_prompt_precmd]+="
85        print -rnu $_tty_fd \$'\\e]2;'\"\${(%):-%(4~|…/%3~|%~)}\"\$'\\a'"
86    functions[_prompt_preexec]+="
87        print -rnu $_tty_fd \$'\\e]2;'\"\${(V)1}\"\$'\\a'"
88fi
89
90preexec_functions+=(_prompt_preexec)
91precmd_functions+=(_prompt_precmd)