2012-06-05 04:24:42 +00:00
|
|
|
#
|
2016-05-08 22:57:56 +00:00
|
|
|
# Wrap the builtin history command to provide additional functionality.
|
2012-06-05 04:24:42 +00:00
|
|
|
#
|
2016-08-25 05:56:19 +00:00
|
|
|
function history --description "display or manipulate interactive command history"
|
2016-07-14 05:33:50 +00:00
|
|
|
set -l cmd
|
2016-07-30 04:24:26 +00:00
|
|
|
set -l search_mode
|
2016-07-14 05:33:50 +00:00
|
|
|
set -l with_time
|
2016-06-29 05:22:40 +00:00
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
# 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
|
2016-07-30 04:24:26 +00:00
|
|
|
case -e --exact
|
|
|
|
set search_mode --exact
|
2016-07-14 05:33:50 +00:00
|
|
|
case --
|
|
|
|
set -e argv[1]
|
|
|
|
break
|
|
|
|
case '*'
|
|
|
|
break
|
2016-07-11 08:45:30 +00:00
|
|
|
end
|
2016-07-14 05:33:50 +00:00
|
|
|
set -e argv[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if not set -q cmd[1]
|
2016-08-25 05:56:19 +00:00
|
|
|
set cmd search # default to "search" if the user didn't explicitly specify a command
|
2016-07-14 05:33:50 +00:00
|
|
|
else if set -q cmd[2]
|
|
|
|
printf (_ "You cannot specify multiple commands: %s\n") "$cmd"
|
|
|
|
return 1
|
2016-06-29 05:22:40 +00:00
|
|
|
end
|
|
|
|
|
2016-05-08 23:27:15 +00:00
|
|
|
switch $cmd
|
2016-06-29 05:22:40 +00:00
|
|
|
case search
|
2016-07-30 04:24:26 +00:00
|
|
|
test -z "$search_mode"
|
|
|
|
and set search_mode "--contains"
|
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
if isatty stdout
|
2016-07-11 13:47:55 +00:00
|
|
|
set -l pager less
|
|
|
|
set -q PAGER
|
|
|
|
and set pager $PAGER
|
2016-07-14 05:33:50 +00:00
|
|
|
builtin history --search $search_mode $with_time -- $argv | eval $pager
|
2016-07-11 13:47:55 +00:00
|
|
|
else
|
2016-07-14 05:33:50 +00:00
|
|
|
builtin history --search $search_mode $with_time -- $argv
|
2016-07-11 13:47:55 +00:00
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-08-25 05:56:19 +00:00
|
|
|
case delete # Interactively delete history
|
2016-07-14 05:33:50 +00:00
|
|
|
# TODO: Fix this to deal with history entries that have multiple lines.
|
|
|
|
if not set -q argv[1]
|
2016-09-10 00:15:23 +00:00
|
|
|
printf (_ "You must specify at least one search term when deleting entries\n") >&2
|
2016-07-14 05:33:50 +00:00
|
|
|
return 1
|
2016-07-11 13:47:55 +00:00
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-07-30 04:24:26 +00:00
|
|
|
test -z "$search_mode"
|
|
|
|
and set search_mode "--exact"
|
|
|
|
|
|
|
|
if test $search_mode = "--exact"
|
|
|
|
builtin history --delete $search_mode $argv
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
# 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)
|
2016-07-14 05:33:50 +00:00
|
|
|
printf "[%s] %s\n" $i $found_items[$i]
|
2016-07-11 13:47:55 +00:00
|
|
|
end
|
2016-07-14 05:33:50 +00:00
|
|
|
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
|
2016-07-14 05:33:50 +00:00
|
|
|
echo ''
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
if test -z "$choice"
|
|
|
|
printf "Cancelling the delete!\n"
|
|
|
|
return
|
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
if test "$choice" = "all"
|
|
|
|
printf "Deleting all matching entries!\n"
|
|
|
|
builtin history --delete $search_mode -- $argv
|
|
|
|
builtin history --save
|
|
|
|
return
|
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
for i in (string split " " -- $choice)
|
|
|
|
if test -z "$i"
|
2016-08-25 05:56:19 +00:00
|
|
|
or not string match -qr '^[1-9][0-9]*$' -- $i
|
|
|
|
or test $i -gt $found_items_count
|
2016-07-14 05:33:50 +00:00
|
|
|
printf "Ignoring invalid history entry ID \"%s\"\n" $i
|
2016-07-11 13:47:55 +00:00
|
|
|
continue
|
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
printf "Deleting history entry %s: \"%s\"\n" $i $found_items[$i]
|
|
|
|
builtin history --delete "$found_items[$i]"
|
2016-05-08 23:27:15 +00:00
|
|
|
end
|
2016-07-11 13:47:55 +00:00
|
|
|
builtin history --save
|
|
|
|
end
|
2016-07-14 05:33:50 +00:00
|
|
|
|
2016-07-11 13:47:55 +00:00
|
|
|
case save
|
2016-09-10 00:15:23 +00:00
|
|
|
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
|
2016-07-14 05:33:50 +00:00
|
|
|
builtin history --save -- $argv
|
|
|
|
|
2016-07-11 13:47:55 +00:00
|
|
|
case merge
|
2016-09-10 00:15:23 +00:00
|
|
|
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
|
2016-07-14 05:33:50 +00:00
|
|
|
builtin history --merge -- $argv
|
|
|
|
|
2016-07-11 13:47:55 +00:00
|
|
|
case help
|
|
|
|
builtin history --help
|
2016-07-14 05:33:50 +00:00
|
|
|
|
2016-07-11 13:47:55 +00:00
|
|
|
case clear
|
|
|
|
# Erase the entire history.
|
2016-09-10 00:15:23 +00:00
|
|
|
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"
|
2016-07-14 05:33:50 +00:00
|
|
|
builtin history --clear -- $argv
|
2016-09-10 00:15:23 +00:00
|
|
|
and printf (_ "Command history cleared!")
|
|
|
|
else
|
|
|
|
printf (_ "You did not say 'yes' so I will not clear your command history\n")
|
2016-05-08 23:27:15 +00:00
|
|
|
end
|
|
|
|
end
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|