Also remove ephemeral item if command is effectively empty

Fixes #8232.

Note that this needed to have expect_prompt used in the pexpect test -
we might want to add a "catchup" there so you can just ignore the
prompt counter for a bit and pick it back up later.
This commit is contained in:
Fabian Homborg 2021-08-20 19:36:11 +02:00
parent b748417af7
commit 70e3e0beac
2 changed files with 39 additions and 23 deletions

View File

@ -3351,23 +3351,25 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat
text.pop_back();
}
if (!text.empty() && history && !conf.in_silent_mode) {
// Remove ephemeral items.
if (history && !conf.in_silent_mode) {
// Remove ephemeral items - even if the text is empty
history->remove_ephemeral_items();
// Mark this item as ephemeral if there is a leading space (#615).
history_persistence_mode_t mode;
if (text.front() == L' ') {
// Leading spaces are ephemeral (#615).
mode = history_persistence_mode_t::ephemeral;
} else if (in_private_mode(vars)) {
// Private mode means in-memory only.
mode = history_persistence_mode_t::memory;
} else {
mode = history_persistence_mode_t::disk;
if (!text.empty()) {
// Mark this item as ephemeral if there is a leading space (#615).
history_persistence_mode_t mode;
if (text.front() == L' ') {
// Leading spaces are ephemeral (#615).
mode = history_persistence_mode_t::ephemeral;
} else if (in_private_mode(vars)) {
// Private mode means in-memory only.
mode = history_persistence_mode_t::memory;
} else {
mode = history_persistence_mode_t::disk;
}
history_t::add_pending_with_file_detection(history, text, vars.snapshot(),
mode);
}
history_t::add_pending_with_file_detection(history, text, vars.snapshot(),
mode);
}
rls.finished = true;

View File

@ -98,9 +98,9 @@ expect_prompt("echo start1; builtin history; echo end1\r\n")
# ==========
# Delete a single command we recently ran.
sendline("history delete -e -C 'echo hello'")
expect_str("history delete -e -C 'echo hello'\r\n")
expect_prompt("history delete -e -C 'echo hello'\r\n")
sendline("echo count hello (history search -e -C 'echo hello' | wc -l | string trim)")
expect_re("count hello 0\r\n")
expect_prompt("count hello 0\r\n")
# ==========
# Interactively delete one of multiple matched commands. This verifies that we
@ -115,30 +115,44 @@ expect_re(
)
expect_re("Delete which entries\? >")
sendline("1")
expect_re('Deleting history entry 1: "echo hello AGAIN"\r\n')
expect_prompt('Deleting history entry 1: "echo hello AGAIN"\r\n')
# Verify that the deleted history entry is gone and the other one that matched
# the prefix search above is still there.
sendline(
"echo count AGAIN (history search -e -C 'echo hello AGAIN' | wc -l | string trim)"
)
expect_re("count AGAIN 0\r\n")
expect_prompt("count AGAIN 0\r\n")
sendline(
"echo count again (history search -e -C 'echo hello again' | wc -l | string trim)"
)
expect_re("count again 1\r\n")
expect_prompt("count again 1\r\n")
# Verify that the $history var has the expected content.
sendline("echo history2=$history\[2\]")
expect_re("history2=echo count AGAIN .*\r\n")
expect_prompt("history2=echo count AGAIN .*\r\n")
# Verify that history search is case-insensitive by default
sendline("echo term")
expect_str("term")
expect_prompt("term")
sendline("echo TERM")
expect_str("TERM")
expect_prompt("TERM")
sendline("echo banana")
expect_str("banana")
expect_prompt("banana")
send("ter\x1b[A") # up-arrow
expect_re("echo TERM")
sendline("")
expect_prompt("TERM")
# Check that leading space makes an ephemeral item
sendline(" echo ephemeral")
expect_prompt("ephemeral")
send("\x1b[A") # up-arrow
expect_re(" echo ephemeral")
sendline("")
expect_prompt("ephemeral")
sendline(" ")
expect_prompt()
send("\x1b[A")
expect_re("echo TERM") # not ephemeral!