add history append subcommand

This commit is contained in:
Anurag Singh 2024-04-13 18:21:51 +08:00 committed by Johannes Altmanninger
parent 6f408211a1
commit c044d5e3f0
7 changed files with 30 additions and 5 deletions

View File

@ -90,6 +90,7 @@ Scripting improvements
- ``string pad`` no longer allows non-printable characters as padding. (:issue:`9854`) - ``string pad`` no longer allows non-printable characters as padding. (:issue:`9854`)
- ``string repeat`` now allows omission of ``-n`` when the first argument is an integer. (:issue:`10282`) - ``string repeat`` now allows omission of ``-n`` when the first argument is an integer. (:issue:`10282`)
- ``functions --handlers-type caller-exit`` once again lists functions defined as ``function --on-job-exit caller``, rather than them being listed by ``functions --handlers-type process-exit``. - ``functions --handlers-type caller-exit`` once again lists functions defined as ``function --on-job-exit caller``, rather than them being listed by ``functions --handlers-type process-exit``.
- Add ``history append`` subcommand to append a command to the history without executing it (:issue:`4506`).
Interactive improvements Interactive improvements
------------------------ ------------------------

View File

@ -17,6 +17,7 @@ Synopsis
history save history save
history clear history clear
history clear-session history clear-session
history append COMMAND ...
Description Description
----------- -----------
@ -43,6 +44,9 @@ The following operations (sub-commands) are available:
**clear-session** **clear-session**
Clears the history file from all activity of the current session. Note: If ``history merge`` or ``builtin history merge`` is run in a session, only the history after this will be erased. Clears the history file from all activity of the current session. Note: If ``history merge`` or ``builtin history merge`` is run in a session, only the history after this will be erased.
**append**
Appends commands to the history without needing to execute them.
The following options are available: The following options are available:
These flags can appear before or immediately after one of the sub-commands listed above. These flags can appear before or immediately after one of the sub-commands listed above.
@ -83,10 +87,10 @@ Example
history clear history clear
# Deletes all history items # Deletes all history items
history search --contains "foo" history search --contains "foo"
# Outputs a list of all previous commands containing the string "foo". # Outputs a list of all previous commands containing the string "foo".
history delete --prefix "foo" history delete --prefix "foo"
# Interactively deletes commands which start with "foo" from the history. # Interactively deletes commands which start with "foo" from the history.
# You can select more than one entry by entering their IDs separated by a space. # You can select more than one entry by entering their IDs separated by a space.

View File

@ -1,5 +1,5 @@
# Note that when a completion file is sourced a new block scope is created so `set -l` works. # Note that when a completion file is sourced a new block scope is created so `set -l` works.
set -l __fish_history_all_commands search delete save merge clear clear-session set -l __fish_history_all_commands search delete save merge clear clear-session append
complete -c history -s h -l help -d "Display help and exit" complete -c history -s h -l help -d "Display help and exit"
@ -35,3 +35,5 @@ complete -f -c history -n "not __fish_seen_subcommand_from $__fish_history_all_c
-a clear -d "Clears history file" -a clear -d "Clears history file"
complete -f -c history -n "not __fish_seen_subcommand_from $__fish_history_all_commands" \ complete -f -c history -n "not __fish_seen_subcommand_from $__fish_history_all_commands" \
-a clear-session -d "Clears all history from the current session" -a clear-session -d "Clears all history from the current session"
complete -f -c history -n "not __fish_seen_subcommand_from $__fish_history_all_commands" \
-a append -d "Appends commands to the history without needing to execute them"

View File

@ -70,7 +70,7 @@ function history --description "display or manipulate interactive command histor
# 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]
if contains $argv[1] search delete merge save clear clear-session if contains $argv[1] search delete merge save clear clear-session append
set hist_cmd $argv[1] set hist_cmd $argv[1]
set -e argv[1] set -e argv[1]
end end
@ -226,6 +226,13 @@ function history --description "display or manipulate interactive command histor
builtin history clear-session -- $argv builtin history clear-session -- $argv
printf (_ "Command history for session cleared!\n") printf (_ "Command history for session cleared!\n")
case append
set -l newitem $argv
if not set -q argv[1]
read -P "Command: " newitem
end
builtin history append -- $newitem
case '*' case '*'
printf "%ls: unexpected subcommand '%ls'\n" $cmd $hist_cmd printf "%ls: unexpected subcommand '%ls'\n" $cmd $hist_cmd
return 2 return 2

View File

@ -16,6 +16,7 @@ enum HistCmd {
#[default] #[default]
HIST_UNDEF, HIST_UNDEF,
HIST_CLEAR_SESSION, HIST_CLEAR_SESSION,
HIST_APPEND,
} }
impl HistCmd { impl HistCmd {
@ -28,6 +29,7 @@ impl HistCmd {
HistCmd::HIST_SAVE => L!("save"), HistCmd::HIST_SAVE => L!("save"),
HistCmd::HIST_UNDEF => panic!(), HistCmd::HIST_UNDEF => panic!(),
HistCmd::HIST_CLEAR_SESSION => L!("clear-session"), HistCmd::HIST_CLEAR_SESSION => L!("clear-session"),
HistCmd::HIST_APPEND => L!("append"),
} }
} }
} }
@ -42,6 +44,7 @@ impl TryFrom<&wstr> for HistCmd {
_ if val == "merge" => Ok(HistCmd::HIST_MERGE), _ if val == "merge" => Ok(HistCmd::HIST_MERGE),
_ if val == "save" => Ok(HistCmd::HIST_SAVE), _ if val == "save" => Ok(HistCmd::HIST_SAVE),
_ if val == "clear-session" => Ok(HistCmd::HIST_CLEAR_SESSION), _ if val == "clear-session" => Ok(HistCmd::HIST_CLEAR_SESSION),
_ if val == "append" => Ok(HistCmd::HIST_APPEND),
_ => Err(()), _ => Err(()),
} }
} }
@ -343,6 +346,11 @@ pub fn history(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) ->
history.save(); history.save();
} }
HistCmd::HIST_UNDEF => panic!("Unexpected HIST_UNDEF seen"), HistCmd::HIST_UNDEF => panic!("Unexpected HIST_UNDEF seen"),
HistCmd::HIST_APPEND => {
for &arg in args {
history.add_commandline(arg.to_owned());
}
}
} }
status status

View File

@ -1493,7 +1493,6 @@ impl History {
self.imp().add(item, pending, true) self.imp().add(item, pending, true)
} }
/// Exposed for testing.
pub fn add_commandline(&self, s: WString) { pub fn add_commandline(&self, s: WString) {
let mut imp = self.imp(); let mut imp = self.imp();
let when = imp.timestamp_now(); let when = imp.timestamp_now();

View File

@ -197,6 +197,10 @@ fn test_history() {
assert_eq!(bef.get_required_paths(), aft.get_required_paths()); assert_eq!(bef.get_required_paths(), aft.get_required_paths());
} }
// Items should be explicitly added to the history.
history.add_commandline(L!("test-command").into());
assert!(history_contains(&history, L!("test-command")));
// Clean up after our tests. // Clean up after our tests.
history.clear(); history.clear();
} }