2012-06-05 04:24:42 +00:00
|
|
|
#
|
|
|
|
#Deletes an item from history
|
|
|
|
#
|
|
|
|
|
|
|
|
function history --description "Deletes an item from history"
|
|
|
|
|
|
|
|
set -l argc (count $argv)
|
|
|
|
set -l prefix_args ""
|
|
|
|
set -l contains_args ""
|
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
set -l cmd print
|
2012-06-05 04:24:42 +00:00
|
|
|
|
|
|
|
set -l search_mode none
|
|
|
|
|
2014-10-26 09:19:29 +00:00
|
|
|
set -l pager less
|
|
|
|
if set -q PAGER
|
|
|
|
set pager $PAGER
|
|
|
|
end
|
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
if test $argc -gt 0
|
2012-06-05 04:24:42 +00:00
|
|
|
for i in (seq $argc)
|
|
|
|
switch $argv[$i]
|
|
|
|
case --delete
|
2012-06-05 07:40:42 +00:00
|
|
|
set cmd delete
|
2012-06-05 04:24:42 +00:00
|
|
|
case --prefix
|
|
|
|
set search_mode prefix
|
|
|
|
set prefix_args $argv[(math $i + 1)]
|
|
|
|
case --contains
|
|
|
|
set search_mode contains
|
|
|
|
set contains_args $argv[(math $i + 1)]
|
2012-06-06 15:21:37 +00:00
|
|
|
case --save
|
|
|
|
set cmd save
|
2012-06-05 04:24:42 +00:00
|
|
|
case --clear
|
2012-06-05 07:40:42 +00:00
|
|
|
set cmd clear
|
|
|
|
case --search
|
|
|
|
set cmd print
|
2012-06-05 06:40:08 +00:00
|
|
|
end
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
2012-06-06 15:21:37 +00:00
|
|
|
else
|
|
|
|
#Execute history builtin without any argument
|
2014-10-26 09:19:29 +00:00
|
|
|
if status --is-interactive
|
|
|
|
builtin history | eval $pager
|
|
|
|
else
|
|
|
|
builtin history
|
|
|
|
end
|
2012-06-06 15:21:37 +00:00
|
|
|
return
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
2012-06-05 04:24:42 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
switch $cmd
|
|
|
|
case print
|
|
|
|
# Print matching items
|
|
|
|
# Note this may end up passing --search twice to the builtin,
|
|
|
|
# but that's harmless
|
|
|
|
builtin history --search $argv
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +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
|
2012-06-12 08:47:25 +00:00
|
|
|
|
|
|
|
#Save changes after deleting item
|
|
|
|
builtin history --save
|
2012-11-18 10:23:22 +00:00
|
|
|
return 0
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
set found_items_count (count $found_items)
|
2012-11-18 10:23:22 +00:00
|
|
|
if test $found_items_count -gt 0
|
2012-06-05 07:40:42 +00:00
|
|
|
echo "[0] cancel"
|
|
|
|
echo "[1] all"
|
|
|
|
echo
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
for i in (seq $found_items_count)
|
|
|
|
printf "[%s] %s \n" (math $i + 1) $found_items[$i]
|
|
|
|
end
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
read --local --prompt "echo 'Delete which entries? > '" choice
|
|
|
|
set choice (echo $choice | tr " " "\n")
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
for i in $choice
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
# Skip empty input, for example, if the user just hits return
|
|
|
|
if test -z $i
|
|
|
|
continue
|
|
|
|
end
|
2012-11-18 10:23:22 +00:00
|
|
|
|
|
|
|
#Following two validations could be embedded with "and" but I find the syntax kind of weird.
|
2012-06-05 07:40:42 +00:00
|
|
|
if not echo $i | grep -E -q "^[0-9]+\$"
|
2012-11-18 10:23:22 +00:00
|
|
|
printf "Invalid input: %s\n" $i
|
|
|
|
continue
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
if test $i -gt (math $found_items_count + 1)
|
2012-11-18 10:23:22 +00:00
|
|
|
printf "Invalid input : %s\n" $i
|
2012-06-05 07:40:42 +00:00
|
|
|
continue
|
|
|
|
end
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
if test $i = "0"
|
|
|
|
printf "Cancel\n"
|
2012-11-18 10:23:22 +00:00
|
|
|
return
|
2012-06-05 07:40:42 +00:00
|
|
|
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
|
2012-06-12 08:47:25 +00:00
|
|
|
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
|
|
|
end
|
2012-06-12 08:47:25 +00:00
|
|
|
#Save changes after deleting item(s)
|
|
|
|
builtin history --save
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
2012-06-06 15:21:37 +00:00
|
|
|
case save
|
|
|
|
#Save changes to history file
|
|
|
|
builtin history $argv
|
2012-06-05 07:40:42 +00:00
|
|
|
case clear
|
|
|
|
# Erase the entire history
|
2012-06-06 15:21:37 +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
|
2012-06-05 07:40:42 +00:00
|
|
|
end
|
|
|
|
end
|