console : store the history with timestamp

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4275 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-07-08 12:06:06 +00:00
parent 11435d848e
commit 6afe1b5c52

View File

@@ -735,7 +735,7 @@ class console
// }}} // }}}
/** Clear the history /** Clear the history
* Do NOT write the empty history on disk * This method do NOT write the empty history on disk
*/ */
public function clearHistory () public function clearHistory ()
// {{{ // {{{
@@ -771,8 +771,10 @@ class console
$this->consoleException ("History file '$historyFile' ". $this->consoleException ("History file '$historyFile' ".
"can not be created: parent directory is not a directory"); "can not be created: parent directory is not a directory");
file_put_contents ($historyFile, "__HISTORY__\n"); file_put_contents ($historyFile, "__HISTORY__\n");
file_put_contents ($historyFile, implode ("\n", $this->history), $history = "";
FILE_APPEND); foreach ($this->history as $time => $command)
$history .= "$time $command\n";
file_put_contents ($historyFile, $history, FILE_APPEND|LOCK_EX);
return $this; return $this;
} }
// }}} // }}}
@@ -780,6 +782,7 @@ class console
/** Read the history from the disk /** Read the history from the disk
* If the file doesn't exists, return an empty array * If the file doesn't exists, return an empty array
* @param string $historyFile The history file where the history is stored * @param string $historyFile The history file where the history is stored
* @return the read history with timestamp as key and command as value
*/ */
public function readHistory ($historyFile) public function readHistory ($historyFile)
// {{{ // {{{
@@ -799,14 +802,20 @@ class console
$this->consoleException ("History file '$historyFile' ". $this->consoleException ("History file '$historyFile' ".
"is not an history file : do not touch\n"); "is not an history file : do not touch\n");
array_shift ($historyArr); array_shift ($historyArr);
$this->history = $historyArr; foreach ($historyArr as $line)
{
@list ($time, $command) = @explode (" ", $line, 2);
if ($time === null || $command === null)
continue;
$this->history[$time] = $command;
}
return $this->history; return $this->history;
} }
// }}} // }}}
/** Add a new entry in history. /** Add a new entry in history.
* The new line can not be empty : it is not stored, but without error * The new line can not be empty : it is not stored, but without error
* Do NOT write the history on disk. * This method do NOT write the history on disk : you must use writeHistory
* @param string The new entry to add in history * @param string The new entry to add in history
*/ */
public function addHistory ($line) public function addHistory ($line)
@@ -817,8 +826,9 @@ class console
"it is not a string"); "it is not a string");
if (trim ($line) === "") if (trim ($line) === "")
return $this; return $this;
$this->history[] = $line; $this->history[time()] = $line;
$this->history = array_slice ($this->history, -$this->historyMaxSize); $this->history = array_slice ($this->history, -$this->historyMaxSize, null,
true);
return $this; return $this;
} }
// }}} // }}}