Also trim trailing newlines when adding to history

When pasting and executing a full line, the trailing newline character
will be included in history.

I usually manually delete the newline before executing, but sometimes
I forget. When I recall my (typically single-line) commands, it's
surprising that the cursor is on the blank second line.

The newline doesn't seem useful. Let's remove it automagically.
I wonder if anyone will be thrown off by this smart behavior.

In future, we can make this space trimming configurable, similar to
fish_should_add_to_history.
This commit is contained in:
Johannes Altmanninger 2025-01-15 07:19:50 +01:00
parent 081c3282b7
commit 6f480d1d85

View File

@ -5722,7 +5722,10 @@ impl<'a> Reader<'a> {
// Historical behavior is to trim trailing spaces, unless escape (#7661).
let mut text = self.command_line.text().to_owned();
while text.chars().next_back() == Some(' ')
while text
.chars()
.next_back()
.is_some_and(|c| matches!(c, ' ' | '\n'))
&& count_preceding_backslashes(&text, text.len() - 1) % 2 == 0
{
text.pop();