1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

Shell+LibLine: Support HISTCONTROL environment variable

This is implemented in Line::Editor meaning not only the Shell will
respect it, but also js, Debugger etc.

Possible values are "ignorespace", "ignoredups" and "ignoreboth", as
documented in Shell-vars(7), for now.

The default value for the anon user (set in .shellrc) is "ignoreboth".
This commit is contained in:
Linus Groh 2020-10-25 23:35:00 +00:00 committed by Andreas Kling
parent 4a4b1b1131
commit d412fbdcf3
3 changed files with 21 additions and 1 deletions

View file

@ -3,3 +3,5 @@
# IFS controls what $(...) (inline evaluate) would split its captured # IFS controls what $(...) (inline evaluate) would split its captured
# string with. the default is \x0a (i.e. newline). # string with. the default is \x0a (i.e. newline).
IFS="\x0a" IFS="\x0a"
export HISTCONTROL="ignoreboth"

View file

@ -17,7 +17,18 @@ The value of this variable is used to join lists or split strings into lists, it
2. History 2. History
`HISTFILE` (environment) `HISTCONTROL` (environment)
The value of this variable is used to determine which entries are kept in the Shell's history, both regarding the current active session and when writing the history to disk on exit.
- `ignorespace`: Entries starting with one or more space characters are ignored
- `ignoredups`: Consecutive duplicate entries are ignored
- `ignoreboth`: The behaviour of `ignorespace` and `ignoredups` is combined
- If the variable is unset (this is the default) or has any other value than the above, no entries will be excluded from history.
Note: This variable is respected by every program using `Line::Editor`, e.g. [`js`(1)](../man1/js.md).
`HISTFILE` (environment)
The value of this variable is used as the Shell's history file path, both for reading history at startup and writing history on exit. The value of this variable is used as the Shell's history file path, both for reading history at startup and writing history on exit.
Its default value is `~/.history`. Its default value is `~/.history`.

View file

@ -208,6 +208,13 @@ void Editor::add_to_history(const String& line)
{ {
if (line.is_empty()) if (line.is_empty())
return; return;
String histcontrol = getenv("HISTCONTROL");
auto ignoredups = histcontrol == "ignoredups" || histcontrol == "ignoreboth";
auto ignorespace = histcontrol == "ignorespace" || histcontrol == "ignoreboth";
if (ignoredups && !m_history.is_empty() && line == m_history.last())
return;
if (ignorespace && line.starts_with(' '))
return;
if ((m_history.size() + 1) > m_history_capacity) if ((m_history.size() + 1) > m_history_capacity)
m_history.take_first(); m_history.take_first();
m_history.append(line); m_history.append(line);