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"
|
|
|
|
if not set -q argv[1]
|
|
|
|
# No arguments so execute history builtin using it's default behavior to display the entire
|
|
|
|
# history.
|
2016-05-08 23:27:15 +00:00
|
|
|
if status --is-interactive
|
2016-06-29 05:22:40 +00:00
|
|
|
set -l pager less
|
|
|
|
set -q PAGER
|
|
|
|
and set pager $PAGER
|
|
|
|
builtin history --with-time | eval $pager
|
2016-05-08 23:27:15 +00:00
|
|
|
else
|
|
|
|
builtin history
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-06-29 05:22:40 +00:00
|
|
|
set -l cmd search
|
|
|
|
set -l prefix_args ""
|
|
|
|
set -l contains_args ""
|
|
|
|
set -l search_mode none
|
|
|
|
set -l time_args
|
|
|
|
|
|
|
|
for i in (seq (count $argv))
|
|
|
|
switch $argv[$i]
|
|
|
|
case -d --delete
|
|
|
|
set cmd delete
|
|
|
|
case -v --save
|
|
|
|
set cmd save
|
|
|
|
case -l --clear
|
|
|
|
set cmd clear
|
|
|
|
case -s --search
|
|
|
|
set cmd search
|
|
|
|
case -m --merge
|
|
|
|
set cmd merge
|
|
|
|
case -h --help
|
|
|
|
set cmd help
|
|
|
|
case -t --with-time
|
|
|
|
set time_args --with-time
|
|
|
|
case -p --prefix
|
|
|
|
set search_mode prefix
|
|
|
|
set prefix_args $argv[(math $i + 1)]
|
|
|
|
case -c --contains
|
|
|
|
set search_mode contains
|
|
|
|
set contains_args $argv[(math $i + 1)]
|
|
|
|
case --
|
|
|
|
set -e argv[1..$i]
|
|
|
|
break
|
|
|
|
case "-*" "--*"
|
|
|
|
printf ( _ "%s: invalid option -- %s\n" ) history $argv[$i] >&2
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-08 23:27:15 +00:00
|
|
|
switch $cmd
|
2016-06-29 05:22:40 +00:00
|
|
|
case search
|
|
|
|
builtin history $time_args --search $argv
|
2016-05-08 23:27:15 +00:00
|
|
|
|
|
|
|
case delete
|
|
|
|
# Interactively delete history
|
|
|
|
set -l found_items ""
|
|
|
|
switch $search_mode
|
|
|
|
case prefix
|
|
|
|
set found_items (builtin history --search --prefix $prefix_args)
|
|
|
|
case contains
|
|
|
|
set found_items (builtin history --search --contains $contains_args)
|
|
|
|
case none
|
|
|
|
builtin history $argv
|
2016-05-08 22:57:56 +00:00
|
|
|
# Save changes after deleting item.
|
2016-05-08 23:27:15 +00:00
|
|
|
builtin history --save
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
set found_items_count (count $found_items)
|
|
|
|
if test $found_items_count -gt 0
|
|
|
|
echo "[0] cancel"
|
|
|
|
echo "[1] all"
|
|
|
|
echo
|
|
|
|
|
|
|
|
for i in (seq $found_items_count)
|
|
|
|
printf "[%s] %s \n" (math $i + 1) $found_items[$i]
|
|
|
|
end
|
|
|
|
|
|
|
|
read --local --prompt "echo 'Delete which entries? > '" choice
|
|
|
|
set choice (string split " " -- $choice)
|
|
|
|
|
|
|
|
for i in $choice
|
|
|
|
|
|
|
|
# Skip empty input, for example, if the user just hits return
|
|
|
|
if test -z $i
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
|
2016-05-08 22:57:56 +00:00
|
|
|
# Following two validations could be embedded with "and" but I find the syntax
|
|
|
|
# kind of weird.
|
2016-05-08 23:27:15 +00:00
|
|
|
if not string match -qr '^[0-9]+$' $i
|
|
|
|
printf "Invalid input: %s\n" $i
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
|
|
|
|
if test $i -gt (math $found_items_count + 1)
|
|
|
|
printf "Invalid input : %s\n" $i
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
|
|
|
|
if test $i = "0"
|
|
|
|
printf "Cancel\n"
|
|
|
|
return
|
|
|
|
else
|
|
|
|
if test $i = "1"
|
|
|
|
for item in $found_items
|
|
|
|
builtin history --delete $item
|
|
|
|
end
|
|
|
|
printf "Deleted all!\n"
|
|
|
|
else
|
|
|
|
builtin history --delete $found_items[(math $i - 1)]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2016-05-08 22:57:56 +00:00
|
|
|
# Save changes after deleting item(s).
|
2016-05-08 23:27:15 +00:00
|
|
|
builtin history --save
|
|
|
|
end
|
|
|
|
case save
|
2016-05-08 22:57:56 +00:00
|
|
|
# Save changes to history file.
|
2016-05-08 23:27:15 +00:00
|
|
|
builtin history $argv
|
|
|
|
case merge
|
|
|
|
builtin history --merge
|
|
|
|
case help
|
|
|
|
builtin history --help
|
|
|
|
case clear
|
2016-05-08 22:57:56 +00:00
|
|
|
# Erase the entire history.
|
2016-05-08 23:27:15 +00:00
|
|
|
echo "Are you sure you want to clear history ? (y/n)"
|
|
|
|
read ch
|
|
|
|
if test $ch = "y"
|
|
|
|
builtin history $argv
|
|
|
|
echo "History cleared!"
|
|
|
|
end
|
|
|
|
end
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|