fish-shell/share/functions/history.fish

157 lines
5.5 KiB
Fish
Raw Normal View History

2012-06-05 04:24:42 +00:00
#
# Wrap the builtin history command to provide additional functionality.
2012-06-05 04:24:42 +00:00
#
function history --description "display or manipulate interactive command history"
set -l cmd
set -l search_mode
set -l with_time
# The "set cmd $cmd xyz" lines are to make it easy to detect if the user specifies more than one
# subcommand.
while set -q argv[1]
switch $argv[1]
case -d --delete
set cmd $cmd delete
case -v --save
set cmd $cmd save
case -l --clear
set cmd $cmd clear
case -s --search
set cmd $cmd search
case -m --merge
set cmd $cmd merge
case -h --help
set cmd $cmd help
case -t --with-time
set with_time -t
case -p --prefix
set search_mode --prefix
case -c --contains
set search_mode --contains
case -e --exact
set search_mode --exact
case --
set -e argv[1]
break
case '*'
break
end
set -e argv[1]
end
if not set -q cmd[1]
set cmd search # default to "search" if the user didn't explicitly specify a command
else if set -q cmd[2]
printf (_ "You cannot specify multiple commands: %s\n") "$cmd"
return 1
end
switch $cmd
case search
test -z "$search_mode"
and set search_mode "--contains"
if isatty stdout
2016-07-11 13:47:55 +00:00
set -l pager less
set -q PAGER
and set pager $PAGER
builtin history --search $search_mode $with_time -- $argv | eval $pager
2016-07-11 13:47:55 +00:00
else
builtin history --search $search_mode $with_time -- $argv
2016-07-11 13:47:55 +00:00
end
case delete # Interactively delete history
# TODO: Fix this to deal with history entries that have multiple lines.
if not set -q argv[1]
printf (_ "You must specify at least one search term when deleting entries\n") >&2
return 1
2016-07-11 13:47:55 +00:00
end
test -z "$search_mode"
and set search_mode "--exact"
if test $search_mode = "--exact"
builtin history --delete $search_mode $argv
return
end
# TODO: Fix this so that requesting history entries with a timestamp works:
# set -l found_items (builtin history --search $search_mode $with_time -- $argv)
set -l found_items (builtin history --search $search_mode -- $argv)
if set -q found_items[1]
set -l found_items_count (count $found_items)
2016-07-11 13:47:55 +00:00
for i in (seq $found_items_count)
printf "[%s] %s\n" $i $found_items[$i]
2016-07-11 13:47:55 +00:00
end
echo ""
echo "Enter nothing to cancel the delete, or"
echo "Enter one or more of the entry IDs separated by a space, or"
echo "Enter \"all\" to delete all the matching entries."
echo ""
2016-07-11 13:47:55 +00:00
read --local --prompt "echo 'Delete which entries? > '" choice
echo ''
if test -z "$choice"
printf "Cancelling the delete!\n"
return
end
if test "$choice" = "all"
printf "Deleting all matching entries!\n"
builtin history --delete $search_mode -- $argv
builtin history --save
return
end
for i in (string split " " -- $choice)
if test -z "$i"
or not string match -qr '^[1-9][0-9]*$' -- $i
or test $i -gt $found_items_count
printf "Ignoring invalid history entry ID \"%s\"\n" $i
2016-07-11 13:47:55 +00:00
continue
end
printf "Deleting history entry %s: \"%s\"\n" $i $found_items[$i]
builtin history --delete "$found_items[$i]"
end
2016-07-11 13:47:55 +00:00
builtin history --save
end
2016-07-11 13:47:55 +00:00
case save
if test -n "$search_mode"
or test -n "$with_time"
printf (_ "history: you cannot use any options with %s command\n") save >&2
return 1
end
builtin history --save -- $argv
2016-07-11 13:47:55 +00:00
case merge
if test -n "$search_mode"
or test -n "$with_time"
printf (_ "history: you cannot use any options with %s command\n") merge >&2
return 1
end
builtin history --merge -- $argv
2016-07-11 13:47:55 +00:00
case help
builtin history --help
2016-07-11 13:47:55 +00:00
case clear
# Erase the entire history.
if test -n "$search_mode"
or test -n "$with_time"
printf (_ "history: you cannot use any options with %s command\n") clear >&2
return 1
end
printf (_ "If you enter 'yes' your entire interactive command history will be erased\n")
read --local --prompt "echo 'Are you sure you want to clear history? (yes/no) '" choice
if test "$choice" = "yes"
builtin history --clear -- $argv
and printf (_ "Command history cleared!")
else
printf (_ "You did not say 'yes' so I will not clear your command history\n")
end
end
end