126 lines
3.8 KiB
Fish
126 lines
3.8 KiB
Fish
function palette --description 'Command palette: switch between shells and/or utilities via fzf'
|
|
set -l mode both
|
|
if set -q argv[1]
|
|
set mode $argv[1]
|
|
end
|
|
|
|
set -l lines
|
|
switch $mode
|
|
case both
|
|
set lines (__palette_lines utility; __palette_lines shell)
|
|
case shell
|
|
set lines (__palette_lines shell)
|
|
case utility util
|
|
set lines (__palette_lines utility)
|
|
case '*'
|
|
echo "usage: palette [both|shell|utility]" >&2
|
|
return 1
|
|
end
|
|
|
|
test -n "$lines"; or return 1
|
|
|
|
set -l picked (printf '%s\n' $lines | fzf --no-multi --no-sort \
|
|
--delimiter=\t --nth=1 --with-nth=1 \
|
|
--prompt="palette($mode)> " \
|
|
--preview='echo -n {} | cut -f4')
|
|
or return 1
|
|
|
|
__palette_dispatch "$picked"
|
|
end
|
|
|
|
function __palette_lines -a kind
|
|
switch $kind
|
|
case shell
|
|
set -l shells fish:fish bash:bash zsh:zsh nushell:nu dash:dash tcsh:tcsh
|
|
for s in $shells
|
|
set -l name (string split : -- $s)[1]
|
|
set -l bin (string split : -- $s)[2]
|
|
command -q $bin; or continue
|
|
test $bin = fish; and continue
|
|
printf '%s\tshell\t%s\t%s\n' $name $bin "switch to $name (nested)"
|
|
end
|
|
case utility
|
|
printf 'cd\tutil\tcd\tzoxide: jump to a directory\n'
|
|
printf 'files\tutil\tfiles\tfd/fzf: pick a file or directory\n'
|
|
printf 'history\tutil\thistory\tatuin: search command history\n'
|
|
printf 'yazi\tutil\tyazi\tyazi: file manager\n'
|
|
printf 'git\tutil\tgit\tgit: common actions\n'
|
|
printf 'jobs\tutil\tjobs\tfg: pick a background job\n'
|
|
end
|
|
end
|
|
|
|
function __palette_dispatch
|
|
set -l f (string split \t -- $argv[1])
|
|
set -l kind $f[2]
|
|
set -l action $f[3]
|
|
switch $kind
|
|
case util
|
|
__palette_run_util $action
|
|
case shell
|
|
command $action
|
|
commandline -f repaint
|
|
end
|
|
end
|
|
|
|
function __palette_run_util -a action
|
|
switch $action
|
|
case cd
|
|
zi
|
|
case files
|
|
__palette_file
|
|
case history
|
|
if functions -q _atuin_search
|
|
_atuin_search
|
|
else if type -q atuin
|
|
commandline -r -- (atuin search --interactive)
|
|
end
|
|
case yazi
|
|
y
|
|
case jobs
|
|
__fg_select
|
|
case git
|
|
__palette_git
|
|
end
|
|
commandline -f repaint
|
|
end
|
|
|
|
function __palette_file
|
|
command -q fd; or return 1
|
|
set -l pick (fd -0 -H -t f -t d --max-depth=8 . 2>/dev/null | fzf --read0 \
|
|
--delimiter=/ --with-nth=-1 --prompt='files> ' \
|
|
--preview='test -d "{}"; and lls "{}" 2>/dev/null; or bat --color=always --line-range=:80 "{}" 2>/dev/null; or cat "{}" 2>/dev/null')
|
|
or return 1
|
|
|
|
if test -d "$pick"
|
|
cd "$pick"
|
|
else
|
|
commandline -r -- (string escape -- $pick)
|
|
end
|
|
end
|
|
|
|
function __palette_git
|
|
set -l lines \
|
|
(string join \t -- status 'git status') \
|
|
(string join \t -- log 'git log --oneline -20') \
|
|
(string join \t -- switch '__palette_git_switch') \
|
|
(string join \t -- diff 'git diff') \
|
|
(string join \t -- 'add -p' 'git add -p') \
|
|
(string join \t -- commit 'git commit') \
|
|
(string join \t -- push 'git push')
|
|
set -l pick (printf '%s\n' $lines | fzf --delimiter=\t --with-nth=1 --prompt='git> ')
|
|
or return 1
|
|
|
|
set -l cmd (string split \t -- $pick)[2]
|
|
switch $cmd
|
|
case __palette_git_switch
|
|
__palette_git_switch
|
|
case '*'
|
|
commandline -r -- $cmd
|
|
end
|
|
end
|
|
|
|
function __palette_git_switch
|
|
set -l branch (git for-each-ref --format='%(refname:short)' refs/heads | fzf --prompt='branch> ')
|
|
or return 1
|
|
git switch $branch
|
|
end |