Get prompt marking working for th fish shell
This commit is contained in:
parent
0ec10b52e0
commit
23f94b6e67
@ -5,6 +5,7 @@
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from tempfile import mkstemp
|
||||
from typing import Optional, Union
|
||||
|
||||
@ -15,7 +16,7 @@ from .utils import log_error, resolved_shell
|
||||
|
||||
posix_template = '''
|
||||
# BEGIN_KITTY_SHELL_INTEGRATION
|
||||
[[ -a {path} ]] && source {path}
|
||||
test -e {path} && source {path}
|
||||
# END_KITTY_SHELL_INTEGRATION
|
||||
'''
|
||||
|
||||
@ -28,14 +29,16 @@ def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
||||
with open(fd, mode) as f:
|
||||
shutil.copystat(path, tpath)
|
||||
f.write(data)
|
||||
try:
|
||||
os.rename(tpath, path)
|
||||
except OSError:
|
||||
os.unlink(tpath)
|
||||
raise
|
||||
|
||||
|
||||
def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None:
|
||||
import re
|
||||
rc_path = os.path.realpath(rc_path)
|
||||
if not os.access(rc_path, os.W_OK, effective_ids=os.access in os.supports_effective_ids):
|
||||
return
|
||||
try:
|
||||
with open(rc_path) as f:
|
||||
rc = f.read()
|
||||
@ -66,7 +69,30 @@ def setup_bash_integration() -> None:
|
||||
setup_integration('bash', os.path.expanduser('~/.bashrc'))
|
||||
|
||||
|
||||
SUPPORTED_SHELLS = {'zsh': setup_zsh_integration, 'bash': setup_bash_integration}
|
||||
def atomic_symlink(destination: str, in_directory: str) -> str:
|
||||
os.makedirs(in_directory, exist_ok=True)
|
||||
name = os.path.basename(destination)
|
||||
tmpname = os.path.join(in_directory, f'{name}-{os.getpid()}-{time.monotonic()}')
|
||||
os.symlink(destination, tmpname)
|
||||
try:
|
||||
os.rename(tmpname, os.path.join(in_directory, name))
|
||||
except OSError:
|
||||
os.unlink(tmpname)
|
||||
raise
|
||||
|
||||
|
||||
def setup_fish_integration() -> None:
|
||||
base = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
|
||||
base = os.path.join(base, 'fish', 'conf.d')
|
||||
path = os.path.join(shell_integration_dir, 'kitty.fish')
|
||||
atomic_symlink(path, base)
|
||||
|
||||
|
||||
SUPPORTED_SHELLS = {
|
||||
'zsh': setup_zsh_integration,
|
||||
'bash': setup_bash_integration,
|
||||
'fish': setup_fish_integration,
|
||||
}
|
||||
|
||||
|
||||
def get_supported_shell_name(path: str) -> Optional[str]:
|
||||
|
||||
78
shell-integration/kitty.fish
Normal file
78
shell-integration/kitty.fish
Normal file
@ -0,0 +1,78 @@
|
||||
#!/bin/fish
|
||||
|
||||
function _ksi_main
|
||||
test -z "$KITTY_SHELL_INTEGRATION" && return
|
||||
set --local _ksi (string split " " -- "$KITTY_SHELL_INTEGRATION")
|
||||
set --erase KITTY_SHELL_INTEGRATION
|
||||
|
||||
function _ksi_osc
|
||||
printf "\e]%s\a" "$argv[1]"
|
||||
end
|
||||
|
||||
if ! contains "no-prompt-mark" $_ksi
|
||||
set --global _ksi_prompt_state "first-run"
|
||||
|
||||
function _ksi_function_is_not_empty -d "Check if the specified function exists and is not empty"
|
||||
test (functions $argv[1] | grep -cvE '^ *(#|function |end$|$)') != 0
|
||||
end
|
||||
|
||||
function _ksi_mark -d "tell kitty to mark the current cursor position using OSC 133"
|
||||
_ksi_osc "133;$argv[1]";
|
||||
end
|
||||
|
||||
function _ksi_start_prompt
|
||||
if test "$_ksi_prompt_state" != "postexec" -a "$_ksi_prompt_state" != "first-run"
|
||||
_ksi_mark "D"
|
||||
end
|
||||
set --global _ksi_prompt_state "prompt_start"
|
||||
_ksi_mark "A"
|
||||
end
|
||||
|
||||
function _ksi_end_prompt
|
||||
_ksi_original_fish_prompt
|
||||
set --global _ksi_prompt_state "prompt_end"
|
||||
_ksi_mark "B"
|
||||
end
|
||||
|
||||
functions -c fish_prompt _ksi_original_fish_prompt
|
||||
|
||||
if _ksi_function_is_not_empty fish_mode_prompt
|
||||
functions -c fish_mode_prompt _ksi_original_fish_mode_prompt
|
||||
function fish_mode_prompt
|
||||
_ksi_start_prompt
|
||||
_ksi_original_fish_mode_prompt
|
||||
end
|
||||
function fish_prompt
|
||||
_ksi_end_prompt
|
||||
end
|
||||
else
|
||||
function fish_prompt
|
||||
_ksi_start_prompt
|
||||
_ksi_end_prompt
|
||||
end
|
||||
end
|
||||
|
||||
function _ksi_mark_output_start --on-event fish_preexec
|
||||
set --global _ksi_prompt_state "preexec"
|
||||
_ksi_mark "C"
|
||||
end
|
||||
|
||||
function _ksi_mark_output_end --on-event fish_postexec
|
||||
set --global _ksi_prompt_state "postexec"
|
||||
_ksi_mark "D;$status"
|
||||
end
|
||||
# with prompt marking kitty clears the current prompt on resize so we need
|
||||
# fish to redraw it
|
||||
set --global fish_handle_reflow 1
|
||||
end
|
||||
functions --erase _ksi_main
|
||||
functions --erase _ksi_schedule
|
||||
end
|
||||
|
||||
if status --is-interactive
|
||||
function _ksi_schedule --on-event fish_prompt -d "Setup kitty integration after other scripts have run, we hope"
|
||||
_ksi_main
|
||||
end
|
||||
else
|
||||
functions --erase _ksi_main
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user