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-09-11 03:42:52 +00:00
|
|
|
function __fish_unexpected_hist_args --no-scope-shadowing
|
|
|
|
if test -n "$search_mode"
|
2017-07-13 23:21:14 +00:00
|
|
|
or set -q show_time[1]
|
2016-09-11 03:42:52 +00:00
|
|
|
printf (_ "%ls: you cannot use any options with the %ls command\n") $cmd $hist_cmd >&2
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if set -q argv[1]
|
2016-10-09 21:38:26 +00:00
|
|
|
printf (_ "%ls: %ls expected %d args, got %d\n") $cmd $hist_cmd 0 (count $argv) >&2
|
2016-09-11 03:42:52 +00:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2016-08-25 05:56:19 +00:00
|
|
|
function history --description "display or manipulate interactive command history"
|
2016-09-11 03:42:52 +00:00
|
|
|
set -l cmd history
|
2017-09-16 02:28:44 +00:00
|
|
|
set -l options --exclusive 'c,e,p' --exclusive 'S,D,M,V,C'
|
2017-08-05 01:02:24 +00:00
|
|
|
set -a options 'h/help' 'c/contains' 'e/exact' 'p/prefix'
|
2017-09-14 22:44:17 +00:00
|
|
|
set -a options 'C/case-sensitive' 'R/reverse' 'z/null' 't/show-time=?' 'n#max'
|
2017-07-13 23:21:14 +00:00
|
|
|
# The following options are deprecated and will be removed in the next major release.
|
|
|
|
# Note that they do not have usable short flags.
|
2017-09-14 22:44:17 +00:00
|
|
|
set -a options 'S-search' 'D-delete' 'M-merge' 'V-save' 'X-clear'
|
2017-07-13 23:21:14 +00:00
|
|
|
argparse -n $cmd $options -- $argv
|
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help history
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
set -l hist_cmd
|
|
|
|
set -l show_time
|
2017-07-17 01:27:41 +00:00
|
|
|
|
2017-09-14 22:44:17 +00:00
|
|
|
set -l max_count
|
|
|
|
set -q _flag_max
|
|
|
|
set max_count -n$_flag_max
|
2016-09-11 03:42:52 +00:00
|
|
|
|
2017-07-13 23:21:14 +00:00
|
|
|
set -q _flag_with_time
|
|
|
|
and set -l _flag_show_time $_flag_with_time
|
|
|
|
if set -q _flag_show_time[1]
|
|
|
|
set show_time --show-time=$_flag_show_time
|
|
|
|
else if set -q _flag_show_time
|
|
|
|
set show_time --show-time
|
2016-09-11 03:42:52 +00:00
|
|
|
end
|
2016-06-29 05:22:40 +00:00
|
|
|
|
2017-07-13 23:21:14 +00:00
|
|
|
set -q _flag_prefix
|
|
|
|
and set -l search_mode --prefix
|
|
|
|
set -q _flag_contains
|
|
|
|
and set -l search_mode --contains
|
|
|
|
set -q _flag_exact
|
|
|
|
and set -l search_mode --exact
|
|
|
|
|
|
|
|
if set -q _flag_delete
|
|
|
|
set hist_cmd delete
|
|
|
|
else if set -q _flag_save
|
|
|
|
set hist_cmd save
|
|
|
|
else if set -q _flag_clear
|
|
|
|
set hist_cmd clear
|
|
|
|
else if set -q _flag_search
|
|
|
|
set hist_cmd search
|
|
|
|
else if set -q _flag_merge
|
|
|
|
set hist_cmd merge
|
2016-07-14 05:33:50 +00:00
|
|
|
end
|
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
# If a history command has not already been specified check the first non-flag argument for a
|
|
|
|
# command. This allows the flags to appear before or after the subcommand.
|
|
|
|
if not set -q hist_cmd[1]
|
|
|
|
and set -q argv[1]
|
2017-07-13 23:21:14 +00:00
|
|
|
if contains $argv[1] search delete merge save clear
|
|
|
|
set hist_cmd $argv[1]
|
|
|
|
set -e argv[1]
|
2016-09-11 03:42:52 +00:00
|
|
|
end
|
2016-06-29 05:22:40 +00:00
|
|
|
end
|
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
if not set -q hist_cmd[1]
|
|
|
|
set hist_cmd search # default to "search" if the user didn't specify a subcommand
|
|
|
|
end
|
|
|
|
|
|
|
|
switch $hist_cmd
|
|
|
|
case search # search the interactive command history
|
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
|
2017-09-14 22:44:17 +00:00
|
|
|
builtin history search $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null -- $argv | eval $pager
|
2016-07-11 13:47:55 +00:00
|
|
|
else
|
2017-09-14 22:44:17 +00:00
|
|
|
builtin history search $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null -- $argv
|
2016-07-11 13:47:55 +00:00
|
|
|
end
|
2016-05-08 23:27:15 +00:00
|
|
|
|
2016-09-11 03:42:52 +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"
|
2016-09-24 03:12:15 +00:00
|
|
|
and set search_mode "--contains"
|
2016-07-30 04:24:26 +00:00
|
|
|
|
|
|
|
if test $search_mode = "--exact"
|
2017-07-21 23:25:03 +00:00
|
|
|
builtin history delete $search_mode $_flag_case_sensitive $argv
|
2016-07-30 04:24:26 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-07-14 05:33:50 +00:00
|
|
|
# TODO: Fix this so that requesting history entries with a timestamp works:
|
2016-09-11 03:42:52 +00:00
|
|
|
# set -l found_items (builtin history search $search_mode $show_time -- $argv)
|
2016-10-17 03:33:33 +00:00
|
|
|
set -l found_items
|
2017-07-21 23:25:03 +00:00
|
|
|
builtin history search $search_mode $_flag_case_sensitive --null -- $argv | while read -lz x
|
2016-10-17 03:33:33 +00:00
|
|
|
set found_items $found_items $x
|
|
|
|
end
|
2016-07-14 05:33:50 +00:00
|
|
|
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"
|
2016-10-12 01:59:45 +00:00
|
|
|
for item in $found_items
|
2016-09-24 03:12:15 +00:00
|
|
|
builtin history delete --exact --case-sensitive -- $item
|
2016-10-12 01:59:45 +00:00
|
|
|
end
|
2016-09-11 03:42:52 +00:00
|
|
|
builtin history save
|
2016-07-14 05:33:50 +00:00
|
|
|
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]
|
2016-09-24 03:12:15 +00:00
|
|
|
builtin history delete --exact --case-sensitive -- "$found_items[$i]"
|
2016-05-08 23:27:15 +00:00
|
|
|
end
|
2016-09-11 03:42:52 +00:00
|
|
|
builtin history save
|
2016-07-11 13:47:55 +00:00
|
|
|
end
|
2016-07-14 05:33:50 +00:00
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
case save # save our interactive command history to the persistent history
|
|
|
|
__fish_unexpected_hist_args $argv
|
|
|
|
and return 1
|
2016-07-14 05:33:50 +00:00
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
builtin history save -- $argv
|
2016-07-14 05:33:50 +00:00
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
case merge # merge the persistent interactive command history with our history
|
|
|
|
__fish_unexpected_hist_args $argv
|
|
|
|
and return 1
|
2016-07-14 05:33:50 +00:00
|
|
|
|
2016-09-11 03:42:52 +00:00
|
|
|
builtin history merge -- $argv
|
|
|
|
|
|
|
|
case clear # clear the interactive command history
|
|
|
|
__fish_unexpected_hist_args $argv
|
|
|
|
and return 1
|
2016-09-10 00:15:23 +00:00
|
|
|
|
|
|
|
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-09-11 03:42:52 +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
|
2016-09-11 03:42:52 +00:00
|
|
|
|
|
|
|
case '*'
|
|
|
|
printf "%ls: unexpected subcommand '%ls'\n" $cmd $hist_cmd
|
|
|
|
return 2
|
2016-05-08 23:27:15 +00:00
|
|
|
end
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|