Sanitize history item whitespace

Coalesces commands with leading (if even possible) and trailing
whitespace into the same item, improving the experience when iterating
over history entries.

Closes #4908.
This commit is contained in:
Mahmoud Al-Qudsi 2018-10-01 16:48:52 -05:00
parent a9845dc026
commit f702c42068
2 changed files with 9 additions and 5 deletions

View file

@ -3456,9 +3456,9 @@ void history_tests_t::test_history_formats() {
err(L"Couldn't open file tests/history_sample_bash");
} else {
// The results are in the reverse order that they appear in the bash history file.
// We don't expect whitespace to be elided.
// We don't expect whitespace to be elided (#4908: except for leading/trailing whitespace)
const wchar_t *expected[] = {L"sleep 123",
L" final line",
L"final line",
L"echo supsup",
L"export XVAR='exported'",
L"history --help",

View file

@ -41,6 +41,7 @@
#include "path.h"
#include "reader.h"
#include "tnode.h"
#include "wcstringutil.h"
#include "wildcard.h" // IWYU pragma: keep
#include "wutil.h" // IWYU pragma: keep
@ -556,9 +557,12 @@ bool history_item_t::merge(const history_item_t &item) {
}
history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident)
: contents(str), contents_lower(L""), creation_timestamp(when), identifier(ident) {
for (wcstring::const_iterator it = str.begin(); it != str.end(); ++it) {
contents_lower.push_back(towlower(*it));
: creation_timestamp(when), identifier(ident) {
contents = trim(str);
contents_lower.reserve(contents.size());
for (const auto &c : contents) {
contents_lower.push_back(towlower(c));
}
}