summaryrefslogtreecommitdiff
path: root/home-config/fish/functions/_fzf_search_processes.fish
diff options
context:
space:
mode:
authorRyan Schanzenbacher <ryan@rschanz.org>2024-04-05 11:57:45 -0400
committerRyan Schanzenbacher <ryan@rschanz.org>2024-04-05 11:57:45 -0400
commitb88d77fb2d0d5028a6f5670695dee6bec129501f (patch)
tree2536b0461d297e647cf07157641c9b1223339adf /home-config/fish/functions/_fzf_search_processes.fish
parent776a7ca55d2d2c3f396de91db1c6c3c5065012cd (diff)
ahhh sloppy fish, still needs fixing, esp with the vars
Diffstat (limited to 'home-config/fish/functions/_fzf_search_processes.fish')
-rw-r--r--home-config/fish/functions/_fzf_search_processes.fish32
1 files changed, 32 insertions, 0 deletions
diff --git a/home-config/fish/functions/_fzf_search_processes.fish b/home-config/fish/functions/_fzf_search_processes.fish
new file mode 100644
index 0000000..133a880
--- /dev/null
+++ b/home-config/fish/functions/_fzf_search_processes.fish
@@ -0,0 +1,32 @@
1function _fzf_search_processes --description "Search all running processes. Replace the current token with the pid of the selected process."
2 # Directly use ps command because it is often aliased to a different command entirely
3 # or with options that dirty the search results and preview output
4 set -f ps_cmd (command -v ps || echo "ps")
5 # use all caps to be consistent with ps default format
6 # snake_case because ps doesn't seem to allow spaces in the field names
7 set -f ps_preview_fmt (string join ',' 'pid' 'ppid=PARENT' 'user' '%cpu' 'rss=RSS_IN_KB' 'start=START_TIME' 'command')
8 set -f processes_selected (
9 $ps_cmd -A -opid,command | \
10 _fzf_wrapper --multi \
11 --prompt="Processes> " \
12 --query (commandline --current-token) \
13 --ansi \
14 # first line outputted by ps is a header, so we need to mark it as so
15 --header-lines=1 \
16 # ps uses exit code 1 if the process was not found, in which case show an message explaining so
17 --preview="$ps_cmd -o '$ps_preview_fmt' -p {1} || echo 'Cannot preview {1} because it exited.'" \
18 --preview-window="bottom:4:wrap" \
19 $fzf_processes_opts
20 )
21
22 if test $status -eq 0
23 for process in $processes_selected
24 set -f --append pids_selected (string split --no-empty --field=1 -- " " $process)
25 end
26
27 # string join to replace the newlines outputted by string split with spaces
28 commandline --current-token --replace -- (string join ' ' $pids_selected)
29 end
30
31 commandline --function repaint
32end