Skip to content

Commit

Permalink
Introduce feature to search for processes (#216)
Browse files Browse the repository at this point in the history
Useful for commands such as kill -STOP $pid or lsof -p $pid

Includes a preview that shows other useful information about the process selected such as parent pid, CPU use, memory use, and start time.

There will be a follow up commit to this one to clean up the code, add tests, add a gif, and enable fzf multi-select. I'm merging this in early because the PR has been open for far too long and is already in a usable, tested state.
  • Loading branch information
cr-mitmit authored Jan 29, 2022
1 parent 0dc2795 commit c25cb75
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ Use `fzf.fish` to interactively find and insert different shell entities into th
- **Remarks**
- `$history` is excluded for technical reasons so use the search command history feature instead to inspect it

### A process id

- **Search input:** the pid and command of all running processes, outputted by `ps`
- **Key binding and mnemonic:** <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>P</kbd> (`P` for process)
- **Preview window:** the CPU usage, memory usage, start time, and other information about the process

_The prompt used in the screencasts was created using [IlanCosman/tide](https://github.com/IlanCosman/tide)._

## Installation
Expand Down Expand Up @@ -113,6 +119,7 @@ The following variables can store custom options that will be passed to fzf by t
| Search git log | `fzf_git_log_opts` |
| Search command history | `fzf_history_opts` |
| Search shell variables | `fzf_shell_vars_opts` |
| Search processes | `fzf_processes_opts` |

They are always appended last to fzf's argument list. Because fzf uses the option appearing last when options conflict, your custom options can override hardcoded options. Custom fzf options unlocks a variety of possibilities in customizing and augmenting each feature such as:

Expand Down
2 changes: 1 addition & 1 deletion conf.d/fzf.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ end
# Because of scoping rules, to capture the shell variables exactly as they are, we must read
# them before even executing _fzf_search_variables. We use psub to store the
# variables' info in temporary files and pass in the filenames as arguments.
# # This variable is global so that it can be referenced by fzf_configure_bindings and in tests
# This variable is global so that it can be referenced by fzf_configure_bindings and in tests
set --global _fzf_search_vars_command '_fzf_search_variables (set --show | psub) (set --names | psub)'


Expand Down
1 change: 1 addition & 0 deletions functions/_fzf_configure_bindings_help.fish
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ DESCRIPTION
Search git status | Ctrl+Alt+S (S for status) | --git_status
Search history | Ctrl+R (R for reverse) | --history
Search variables | Ctrl+V (V for variable) | --variables
Search processes | Ctrl+Alt+P (P for process) | --processes
An option with a key sequence value overrides the binding for its feature, while an option
without a value disables the binding. A feature that is not customized retains its default
menomonic binding specified above. Key bindings are installed for default and insert modes.
Expand Down
19 changes: 19 additions & 0 deletions functions/_fzf_search_processes.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function _fzf_search_processes --description "Search all running processes. Replace the current token with the pid of the selected process."
set token (commandline --current-token)

set process_selected (
ps -A -opid,command | \
_fzf_wrapper --query "$token" \
--ansi \
--header-lines=1 \
--preview='ps -o pid,user,ppid,%cpu,rss,time,start,command -p {1}' \
$fzf_processes_opts
)

if test $status -eq 0
set pid_selected (string split --no-empty " " $process_selected)[1]
commandline --current-token --replace -- (string escape -- $pid_selected)
end

commandline --function repaint
end
8 changes: 5 additions & 3 deletions functions/fzf_configure_bindings.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function fzf_configure_bindings --description "Installs the default key bindings
# no need to install bindings if not in interactive mode or running tests
status is-interactive || test "$CI" = true; or return

set options_spec h/help 'directory=?' 'git_log=?' 'git_status=?' 'history=?' 'variables=?'
set options_spec h/help 'directory=?' 'git_log=?' 'git_status=?' 'history=?' 'variables=?' 'processes=?'
argparse --max-args=0 --ignore-unknown $options_spec -- $argv 2>/dev/null
if test $status -ne 0
echo "Invalid option or a positional argument was provided." 1>&2
Expand All @@ -15,13 +15,14 @@ function fzf_configure_bindings --description "Installs the default key bindings
return
else
# Initialize with default key sequences and then override or disable them based on flags
# index 1 = directory, 2 = git_log, 3 = git_status, 4 = history, 5 = variables
set key_sequences \e\cf \e\cl \e\cs \cr \cv # \c = control, \e = escape
# index 1 = directory, 2 = git_log, 3 = git_status, 4 = history, 5 = variables, 6 = processes
set key_sequences \e\cf \e\cl \e\cs \cr \cv \e\cp # \c = control, \e = escape
set --query _flag_directory && set key_sequences[1] "$_flag_directory"
set --query _flag_git_log && set key_sequences[2] "$_flag_git_log"
set --query _flag_git_status && set key_sequences[3] "$_flag_git_status"
set --query _flag_history && set key_sequences[4] "$_flag_history"
set --query _flag_variables && set key_sequences[5] "$_flag_variables"
set --query _flag_processes && set key_sequences[6] "$_flag_processes"

# If fzf bindings already exists, uninstall it first for a clean slate
if functions --query _fzf_uninstall_bindings
Expand All @@ -34,6 +35,7 @@ function fzf_configure_bindings --description "Installs the default key bindings
test -n $key_sequences[3] && bind --mode $mode $key_sequences[3] _fzf_search_git_status
test -n $key_sequences[4] && bind --mode $mode $key_sequences[4] _fzf_search_history
test -n $key_sequences[5] && bind --mode $mode $key_sequences[5] "$_fzf_search_vars_command"
test -n $key_sequences[6] && bind --mode $mode $key_sequences[6] _fzf_search_processes
end

function _fzf_uninstall_bindings --inherit-variable key_sequences
Expand Down

0 comments on commit c25cb75

Please sign in to comment.