diff --git a/functions/__pet_new.fish b/functions/__pet_new.fish index 5c8ff85..a7acb02 100644 --- a/functions/__pet_new.fish +++ b/functions/__pet_new.fish @@ -1,9 +1,9 @@ function __pet_new --description 'Saves a command as a snippet in pet' # Use the current contents of the command line - set -l cmd (commandline) + set -l cmd (commandline | string split0) # If the command line is empty, let's search history - if test -z "$cmd" + if test -z (string join "" -- (commandline -o)) # Copying parts of fzf.fish's _fzf_search_history here, since we can't # directly re-use it if test -z "$fish_private_mode" @@ -16,8 +16,7 @@ function __pet_new --description 'Saves a command as a snippet in pet' set --export FZF_DEFAULT_OPTS '--cycle --layout=reverse --border --height=90% --preview-window=wrap --marker="*"' end set --local --export SHELL (command --search fish) - - set cmd (builtin history --null --show-time="%m-%d %H:%M:%S │ " | \ + set cmd (builtin history --null --show-time="%m-%d %H:%M:%S │ " | fzf --read0 \ --print0 \ --ansi \ @@ -26,13 +25,14 @@ function __pet_new --description 'Saves a command as a snippet in pet' --query=(commandline) \ --preview="echo -- {4..} | fish_indent --ansi" \ --preview-window="bottom:3:wrap" \ - $fzf_history_opts | \ + $fzf_history_opts | # remove timestamps from commands selected - string replace --regex '^\d\d-\d\d \d\d:\d\d:\d\d │ ' '') + string replace --regex '^\d\d-\d\d \d\d:\d\d:\d\d │ ' '' | + string split0) end if test -n "$cmd" - echo "* Save a new pet snippet:" + echo -e "\n* Save a new pet snippet:" pet new --tag -- "$cmd" else commandline --function repaint diff --git a/functions/__pet_preview.fish b/functions/__pet_preview.fish index 186ede1..4fa4284 100644 --- a/functions/__pet_preview.fish +++ b/functions/__pet_preview.fish @@ -1,4 +1,5 @@ function __pet_preview --description "Takes a snippet formatted by `pet search` and turns it into a preview pane for fzf." + set -l tags (echo (echo $argv | sed 's/\\\\n/\n/g')[-1] | sed 's/[^#]*#/#/') # Description set_color blue --bold echo $argv | sed 's/^\[// ; s/\]: .*//' @@ -6,14 +7,16 @@ function __pet_preview --description "Takes a snippet formatted by `pet search` echo "" # Code (syntax-highlighted for fish) echo $argv | \ - sed 's/.*]: // ; s/#.*//' | # extract code + sed 's/\\\\n/\n/g' | # replace pet newlines with normal ones + sed '0,/\[[^]]*\]:/s///' | # delete bracketed description + sed "s/$tags//g" | # find and delete tags sed 's/<\([^ =]\+\)\([^ ]*\)>/{$\U\1}/g' | # replace pet placeholders with variable names fish_indent --ansi - # Tags, if present + # Tags, if present if string match -e -q "#" "$argv" echo "" # to be able to set tags with background color, loop over them individually - for tag in (echo $argv | sed 's/[^#]*#/#/' | string split " ") + for tag in $tags set_color magenta # -b white echo -n "$tag" set_color normal