mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 13:39:02 +00:00
update history
to use argparse
This commit is contained in:
parent
5dc78dd858
commit
c22df3b823
4 changed files with 59 additions and 95 deletions
|
@ -2,22 +2,9 @@
|
||||||
# Wrap the builtin history command to provide additional functionality.
|
# Wrap the builtin history command to provide additional functionality.
|
||||||
#
|
#
|
||||||
|
|
||||||
# This function is meant to mimic the `set_hist_cmd` function in *src/builtin.cpp*.
|
|
||||||
# In particular the error message should be identical in both locations.
|
|
||||||
function __fish_set_hist_cmd --no-scope-shadowing
|
|
||||||
if set -q hist_cmd[1]
|
|
||||||
set -l msg (printf (_ "you cannot do both '%ls' and '%ls' in the same invocation") \
|
|
||||||
$hist_cmd $argv[1])
|
|
||||||
printf (_ "%ls: Invalid combination of options,\n%ls\n") $cmd $msg >&2
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
set hist_cmd $argv[1]
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
|
|
||||||
function __fish_unexpected_hist_args --no-scope-shadowing
|
function __fish_unexpected_hist_args --no-scope-shadowing
|
||||||
if test -n "$search_mode"
|
if test -n "$search_mode"
|
||||||
or test -n "$show_time"
|
or set -q show_time[1]
|
||||||
printf (_ "%ls: you cannot use any options with the %ls command\n") $cmd $hist_cmd >&2
|
printf (_ "%ls: you cannot use any options with the %ls command\n") $cmd $hist_cmd >&2
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
@ -30,90 +17,68 @@ end
|
||||||
|
|
||||||
function history --description "display or manipulate interactive command history"
|
function history --description "display or manipulate interactive command history"
|
||||||
set -l cmd history
|
set -l cmd history
|
||||||
set -l hist_cmd
|
|
||||||
set -l search_mode
|
|
||||||
set -l show_time
|
|
||||||
set -l max_count
|
|
||||||
set -l case_sensitive
|
|
||||||
set -l null
|
|
||||||
|
|
||||||
# Check for a recognized subcommand as the first argument.
|
set -l options --exclusive 'c,e,p' --exclusive 'S,D,M,V,C' --exclusive 't,T'
|
||||||
if set -q argv[1]
|
set options $options 'h/help' 'c/contains' 'e/exact' 'p/prefix'
|
||||||
and not string match -q -- '-*' $argv[1]
|
set options $options 'C/case-sensitive' 'z/null' 't/show-time=?' 'n/max=' '#-max'
|
||||||
switch $argv[1]
|
# This long option is deprecated and here solely for legacy compatibility. People should use
|
||||||
case search delete merge save clear
|
# -t or --show-time now.
|
||||||
set hist_cmd $argv[1]
|
set options $options 'T-with-time=?'
|
||||||
set -e argv[1]
|
# The following options are deprecated and will be removed in the next major release.
|
||||||
end
|
# Note that they do not have usable short flags.
|
||||||
|
set options $options 'S-search' 'D-delete' 'M-merge' 'V-save' 'R-clear'
|
||||||
|
argparse -n $cmd $options -- $argv
|
||||||
|
or return
|
||||||
|
|
||||||
|
if set -q _flag_help
|
||||||
|
__fish_print_help history
|
||||||
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
# The "set cmd $cmd xyz" lines are to make it easy to detect if the user specifies more than one
|
set -l hist_cmd
|
||||||
# subcommand.
|
set -l show_time
|
||||||
#
|
set -l max_count $_flag_max
|
||||||
# TODO: Remove the long options that correspond to subcommands (e.g., '--delete') on or after
|
|
||||||
# 2017-10 (which will be a full year after these flags have been deprecated).
|
set -q _flag_with_time
|
||||||
while set -q argv[1]
|
and set -l _flag_show_time $_flag_with_time
|
||||||
switch $argv[1]
|
if set -q _flag_show_time[1]
|
||||||
case --delete
|
set show_time --show-time=$_flag_show_time
|
||||||
__fish_set_hist_cmd delete
|
else if set -q _flag_show_time
|
||||||
or return
|
set show_time --show-time
|
||||||
case --save
|
end
|
||||||
__fish_set_hist_cmd save
|
|
||||||
or return
|
set -q _flag_null
|
||||||
case --clear
|
and set -l null --null
|
||||||
__fish_set_hist_cmd clear
|
|
||||||
or return
|
set -q _flag_case_sensitive
|
||||||
case --search
|
and set -l case_sensitive --case-sensitive
|
||||||
__fish_set_hist_cmd search
|
|
||||||
or return
|
set -q _flag_prefix
|
||||||
case --merge
|
and set -l search_mode --prefix
|
||||||
__fish_set_hist_cmd merge
|
set -q _flag_contains
|
||||||
or return
|
and set -l search_mode --contains
|
||||||
case -C --case_sensitive
|
set -q _flag_exact
|
||||||
set case_sensitive --case-sensitive
|
and set -l search_mode --exact
|
||||||
case -h --help
|
|
||||||
builtin history --help
|
if set -q _flag_delete
|
||||||
return
|
set hist_cmd delete
|
||||||
case -t --show-time '--show-time=*' --with-time '--with-time=*'
|
else if set -q _flag_save
|
||||||
set show_time $argv[1]
|
set hist_cmd save
|
||||||
case -p --prefix
|
else if set -q _flag_clear
|
||||||
set search_mode --prefix
|
set hist_cmd clear
|
||||||
case -c --contains
|
else if set -q _flag_search
|
||||||
set search_mode --contains
|
set hist_cmd search
|
||||||
case -e --exact
|
else if set -q _flag_merge
|
||||||
set search_mode --exact
|
set hist_cmd merge
|
||||||
case -z --null
|
|
||||||
set null --null
|
|
||||||
case -n --max
|
|
||||||
if string match -- '-n?*' $argv[1]
|
|
||||||
or string match -- '--max=*' $argv[1]
|
|
||||||
set max_count $argv[1]
|
|
||||||
else
|
|
||||||
set max_count $argv[1] $argv[2]
|
|
||||||
set -e argv[1]
|
|
||||||
end
|
|
||||||
case --
|
|
||||||
set -e argv[1]
|
|
||||||
break
|
|
||||||
case '*'
|
|
||||||
if string match -r -- '-\d+' $argv[1]
|
|
||||||
set max_count $argv[1]
|
|
||||||
set -e argv[1]
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
set -e argv[1]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# If a history command has not already been specified check the first non-flag argument for a
|
# 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.
|
# command. This allows the flags to appear before or after the subcommand.
|
||||||
if not set -q hist_cmd[1]
|
if not set -q hist_cmd[1]
|
||||||
and set -q argv[1]
|
and set -q argv[1]
|
||||||
switch $argv[1]
|
if contains $argv[1] search delete merge save clear
|
||||||
case search delete merge save clear
|
set hist_cmd $argv[1]
|
||||||
set hist_cmd $argv[1]
|
set -e argv[1]
|
||||||
set -e argv[1]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ struct history_cmd_opts_t {
|
||||||
/// the non-flag subcommand form. While many of these flags are deprecated they must be
|
/// the non-flag subcommand form. While many of these flags are deprecated they must be
|
||||||
/// supported at least until fish 3.0 and possibly longer to avoid breaking everyones
|
/// supported at least until fish 3.0 and possibly longer to avoid breaking everyones
|
||||||
/// config.fish and other scripts.
|
/// config.fish and other scripts.
|
||||||
static const wchar_t *short_options = L":Cmn:epchtz";
|
static const wchar_t *short_options = L":Cmn:epcht::z";
|
||||||
static const struct woption long_options[] = {{L"prefix", no_argument, NULL, 'p'},
|
static const struct woption long_options[] = {{L"prefix", no_argument, NULL, 'p'},
|
||||||
{L"contains", no_argument, NULL, 'c'},
|
{L"contains", no_argument, NULL, 'c'},
|
||||||
{L"help", no_argument, NULL, 'h'},
|
{L"help", no_argument, NULL, 'h'},
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
history: Invalid combination of options,
|
history: Mutually exclusive flags 'merge' and `search` seen
|
||||||
you cannot do both 'search' and 'merge' in the same invocation
|
|
||||||
history: you cannot use any options with the clear command
|
history: you cannot use any options with the clear command
|
||||||
history: you cannot use any options with the merge command
|
history: you cannot use any options with the merge command
|
||||||
history: save expected 0 args, got 1
|
history: save expected 0 args, got 1
|
||||||
|
|
|
@ -11,7 +11,7 @@ history --save xyz
|
||||||
|
|
||||||
# Now with the history builtin.
|
# Now with the history builtin.
|
||||||
builtin history --save --prefix
|
builtin history --save --prefix
|
||||||
builtin history --clear --with-time
|
builtin history --clear --show-time
|
||||||
builtin history --merge xyz
|
builtin history --merge xyz
|
||||||
builtin history --clear abc def
|
builtin history --clear abc def
|
||||||
|
|
||||||
|
@ -27,13 +27,13 @@ history clear --contains
|
||||||
history merge -t
|
history merge -t
|
||||||
history save xyz
|
history save xyz
|
||||||
history --prefix clear
|
history --prefix clear
|
||||||
history --with-time merge
|
history --show-time merge
|
||||||
echo >&2
|
echo >&2
|
||||||
|
|
||||||
# Now with the history builtin.
|
# Now with the history builtin.
|
||||||
builtin history --search --merge
|
builtin history --search --merge
|
||||||
builtin history save --prefix
|
builtin history save --prefix
|
||||||
builtin history clear --with-time
|
builtin history clear --show-time
|
||||||
builtin history merge xyz
|
builtin history merge xyz
|
||||||
builtin history clear abc def
|
builtin history clear abc def
|
||||||
builtin history --contains save
|
builtin history --contains save
|
||||||
|
|
Loading…
Reference in a new issue