fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Mike Yuanfunctions/history: honor explicitly specified --color=cbd5d7640

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