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-06-29 05:22:40 +00:00
|
|
|
function history --shadow-builtin --description "display or manipulate interactive command history"
|
2016-07-14 05:33:50 +00:00
|
|
|
set -l cmd
|
|
|
|
set -l search_mode --contains
|
|
|
|
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
|
|
|
|
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]
|
|
|
|
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
|
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-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-07-14 05:33:50 +00:00
|
|
|
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 have to specify at least one search term to find entries to delete" >&2
|
|
|
|
return 1
|
2016-07-11 13:47:55 +00:00
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
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"
|
|
|
|
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
|
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-07-14 05:33:50 +00:00
|
|
|
builtin history --save -- $argv
|
|
|
|
|
2016-07-11 13:47:55 +00:00
|
|
|
case merge
|
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-07-14 05:33:50 +00:00
|
|
|
read --local --prompt "echo 'Are you sure you want to clear history? (y/n) '" choice
|
|
|
|
if test "$choice" = "y"
|
|
|
|
or test "$choice" = "yes"
|
|
|
|
builtin history --clear -- $argv
|
|
|
|
and echo "History cleared!"
|
2016-05-08 23:27:15 +00:00
|
|
|
end
|
|
|
|
end
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|