From 0ca5daf997d08b415c7ea9149489ccf3366e3a74 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20L=C3=BCbking?= <thomas.luebking@gmail.com>
Date: Sat, 23 Jul 2016 14:19:10 +0200
Subject: limit fbrun history size

the default is 1024-1025, values are read from the FBRUN_HISTORY_SIZE
environment variable
NOTICE: the limit isn't hard, but will typically be n+1 and only n if
the new entry is already present in the last n entries

REQUEST: 202
---
 util/fbrun/FbRun.cc | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc
index fdc8b38..321959e 100644
--- a/util/fbrun/FbRun.cc
+++ b/util/fbrun/FbRun.cc
@@ -166,14 +166,23 @@ void FbRun::run(const std::string &command) {
         return;
     }
 
-    for (unsigned i = 0; i != m_history.size(); ++i) {
+    int n = 1024;
+    char *a = getenv("FBRUN_HISTORY_SIZE");
+    if (a)
+        n = atoi(a);
+    int j = m_history.size();
+    --n; // NOTICE: this should be "-=2", but a duplicate entry in the late
+         // (good) section would wait "too" long
+         // (we'd wait until 3 items are left and then still skip one for being a dupe)
+         // IOW: the limit is either n or n+1, depending in the history structure
+    for (unsigned int i = 0; i != m_history.size(); ++i) {
         // don't allow duplicates into the history file
-        if (m_history[i] == command)
+        if (--j > n || m_history[i] == command)
             continue;
-
         outfile<<m_history[i]<<endl;
     }
-    outfile<<command<<endl;
+    if (++n > 0) // n was decremented for the loop
+        outfile << command << endl;
     outfile.close();
 }
 
-- 
cgit v0.11.2