fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
cbd5d7640
master
1# localization: tier1 2# 3# Wrap the builtin history command to provide additional functionality. 4# 5function history --description"display or manipulate interactive command history" 6set -l cmd history 7set -l options --exclusive'c,e,p' --exclusive'S,D,M,V,X' 8set -a options h/help c/contains e/exact p/prefix 9set -a options C/case-sensitive R/reverse z/null't/show-time=?' 'n#max' 'color=' 10# The following options are deprecated and will be removed in the next major release. 11# Note that they do not have usable short flags. 12set -a options S-search D-delete M-merge V-save X-clear 13argparse -n$cmd $options --$argv 14or return 15 16if set -q _flag_help 17__fish_print_help history 18return 0 19end 20 21set -l hist_cmd 22set -l show_time 23set -l max_count 24set -l search_mode 25set -l color_opt 26set -q _flag_max 27set max_count -n$_flag_max 28 29set color_opt --color=$_flag_color 30 31set -q _flag_with_time 32and set -l _flag_show_time$_flag_with_time 33if set -q _flag_show_time[ 1 ] 34set show_time --show-time=$_flag_show_time 35else if set -q _flag_show_time 36set show_time --show-time 37end 38 39set -q _flag_prefix 40and set -l search_mode --prefix 41set -q _flag_contains 42and set -l search_mode --contains 43set -q _flag_exact 44and set -l search_mode --exact 45 46if set -q _flag_delete 47set hist_cmd delete 48else if set -q _flag_save 49set hist_cmd save 50else if set -q _flag_clear 51set hist_cmd clear 52else if set -q _flag_search 53set hist_cmd search 54else if set -q _flag_merge 55set hist_cmd merge 56else if set -q _flag_clear-session 57set hist_cmd clear-session 58end 59 60# If a history command has not already been specified check the first non-flag argument for a 61# command. This allows the flags to appear before or after the subcommand. 62if not set -q hist_cmd[ 1 ] 63and set -q argv[ 1 ] 64if contains $argv [ 1 ] search delete merge save clear clear-session append 65set hist_cmd$argv [ 1 ] 66set -e argv[ 1 ] 67end 68end 69 70if not set -q hist_cmd[ 1 ] 71set hist_cmd search# default to "search" if the user didn't specify a subcommand 72end 73 74switch $hist_cmd 75case search# search the interactive command history 76if isatty stdout 77set -l pager( __fish_anypager ) 78and isatty stdout 79or set pager cat 80 81# If the user hasn't preconfigured less with the $LESS environment variable, 82# we do so to have it behave like cat if output fits on one screen. 83if not set -qx LESS 84set -fx LESS --quit-if-one-screen --RAW-CONTROL-CHARS 85# Also set --no-init for less < v530, see #8157. 86if type -q less;and test ( less --version| string match -r'less (\d+)' )[ 2 ] -lt 530 2> /dev/null 87set LESS$LESS --no-init 88end 89end 90not set -qx LV# ask the pager lv not to strip colors 91and set -fx LV -c 92 93if not set -q color_opt[ 1 ] 94set color_opt --color=always 95end 96 97builtin history search$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$argv | $pager 98else 99builtin history search$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$argv 100end 101 102case delete# interactively delete history 103set -l searchterm$argv 104if not set -q argv[ 1 ] 105read -P"Search term: " searchterm 106or return $status 107end 108 109if test " $search_mode " = --exact 110builtin history delete$color_opt $search_mode $_flag_case_sensitive --$searchterm 111builtin history save 112return 113end 114 115# TODO: Fix this so that requesting history entries with a timestamp works: 116# set -l found_items (builtin history search $color_opt $search_mode $show_time -- $argv) 117set -l found_items 118set found_items( builtin history search$color_opt $search_mode $_flag_case_sensitive --null --$searchterm | string split0) 119if set -q found_items[ 1 ] 120set -l found_items_count( count $found_items ) 121for iin ( seq $found_items_count ) 122printf "[%s] %s\n" $i $found_items [ $i ] 123end 124echo 125echo "Enter nothing to cancel the delete, or" 126echo "Enter one or more of the entry IDs or ranges like '5..12', separated by a space." 127echo "For example '7 10..15 35 788..812'." 128echo "Enter 'all' to delete all the matching entries." 129echo 130read --local --prompt"echo 'Delete which entries? '" choice 131or return $status 132echo '' 133 134if test -z " $choice " 135printf "Cancelling the delete!\n" 136return 137end 138 139if test " $choice " = all 140printf "Deleting all matching entries!\n" 141for itemin $found_items 142builtin history delete$color_opt --exact --case-sensitive --$item 143end 144builtin history save 145return 146end 147 148set -l choices 149for iin ( string split" " --$choice ) 150# Expand ranges like 577..580 151if set -l inds( string match -rg'^([1-9][0-9]*)\.\.([1-9][0-9]*)' --$i ) 152if test $inds [ 1 ] -gt $found_items_count 153or test $inds [ 1 ] -gt $inds [ 2 ] 154printf ( _ "Ignoring invalid history entry ID \"%s\"\n" ) $i 155continue 156end 157 158set -l indexes( seq $inds [ 1 ] 1 $inds [ 2 ]) 159if set -q indexes[ 1 ] 160set -a choices$indexes 161end 162continue 163end 164 165if string match -qr'^[1-9][0-9]*$' --$i 166and test $i -le $found_items_count 167set -a choices$i 168else 169printf ( _ "Ignoring invalid history entry ID \"%s\"\n" ) $i 170continue 171end 172end 173 174if not set -q choices[ 1 ] 175return 1 176end 177 178set choices( path sort -u --$choices ) 179 180echo Deleting choices:$choices 181for xin $choices 182printf "Deleting history entry %s: \"%s\"\n" $x $found_items [ $x ] 183builtin history delete$color_opt --exact --case-sensitive --" $found_items [ $x ] " 184end 185builtin history save 186end 187 188case save# save our interactive command history to the persistent history 189builtin history save$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$argv 190case merge# merge the persistent interactive command history with our history 191builtin history merge$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$argv 192case clear# clear the interactive command history 193if test -n " $search_mode " 194or set -q show_time[ 1 ] 195printf ( _ "%s: %s: subcommand takes no options\n" ) history$hist_cmd >&2 196return 1 197end 198if set -q argv[ 1 ] 199printf ( _ "%s: %s: expected %d arguments; got %d\n" ) history$hist_cmd 0 ( count $argv ) >&2 200return 1 201end 202 203printf ( _ "If you enter 'yes' your entire interactive command history will be erased\n" ) 204read --local --prompt"echo 'Are you sure you want to clear history? (yes/no) '" choice 205or return $status 206if test " $choice " = yes 207builtin history clear$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$argv 208and printf ( _ "Command history cleared!\n" ) 209else 210printf ( _ "You did not say 'yes' so I will not clear your command history\n" ) 211end 212case clear-session# clears only session 213builtin history clear-session$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$argv 214and printf ( _ "Command history for session cleared!\n" ) 215case append 216set -l newitem$argv 217if not set -q argv[ 1 ] 218read -P"Command: " newitem 219or return $status 220end 221 222builtin history append$color_opt $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null --$newitem 223case '*' 224printf "%s: unexpected subcommand '%s'\n" $cmd $hist_cmd 225return 2 226end 227end