summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-10-25 23:35:00 +0000
committerAndreas Kling <kling@serenityos.org>2020-10-26 11:27:54 +0100
commitd412fbdcf3919a48c05b5ca4f5b369627b3a1710 (patch)
tree5d7f7b4a7c9f2b4d15bdcbce7c2b3888696c4313
parent4a4b1b1131f4622d7c83bd5a55961bcc56ec7943 (diff)
downloadserenity-d412fbdcf3919a48c05b5ca4f5b369627b3a1710.zip
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".
-rw-r--r--Base/home/anon/.shellrc2
-rw-r--r--Base/usr/share/man/man7/Shell-vars.md13
-rw-r--r--Libraries/LibLine/Editor.cpp7
3 files changed, 21 insertions, 1 deletions
diff --git a/Base/home/anon/.shellrc b/Base/home/anon/.shellrc
index 21821bef78..9c33c7f968 100644
--- a/Base/home/anon/.shellrc
+++ b/Base/home/anon/.shellrc
@@ -3,3 +3,5 @@
# IFS controls what $(...) (inline evaluate) would split its captured
# string with. the default is \x0a (i.e. newline).
IFS="\x0a"
+
+export HISTCONTROL="ignoreboth"
diff --git a/Base/usr/share/man/man7/Shell-vars.md b/Base/usr/share/man/man7/Shell-vars.md
index cce5fc7457..1107bed799 100644
--- a/Base/usr/share/man/man7/Shell-vars.md
+++ b/Base/usr/share/man/man7/Shell-vars.md
@@ -17,7 +17,18 @@ The value of this variable is used to join lists or split strings into lists, it
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.
Its default value is `~/.history`.
diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp
index c103d5ccf9..3fef7468de 100644
--- a/Libraries/LibLine/Editor.cpp
+++ b/Libraries/LibLine/Editor.cpp
@@ -208,6 +208,13 @@ void Editor::add_to_history(const String& line)
{
if (line.is_empty())
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)
m_history.take_first();
m_history.append(line);