Fix history bug

This commit is contained in:
Tagir Kuskarov 2020-06-05 01:31:51 +04:00
parent 9e3024976e
commit 83ba86c5f6
2 changed files with 15 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
#include <functional>
#include <sys/file.h>
namespace
{
@ -24,7 +25,10 @@ ReplxxLineReader::ReplxxLineReader(
using Replxx = replxx::Replxx;
if (!history_file_path.empty())
{
rx.history_load(history_file_path);
history_file_fd = open(history_file_path.c_str(), O_RDWR);
}
auto callback = [&suggest] (const String & context, size_t context_size)
{
@ -68,7 +72,15 @@ LineReader::InputStatus ReplxxLineReader::readOneLine(const String & prompt)
void ReplxxLineReader::addToHistory(const String & line)
{
// locking history file to prevent from inconsistent concurrent changes
flock(history_file_fd, LOCK_EX);
rx.history_add(line);
// flush changes to the disk
rx.history_save(history_file_path);
flock(history_file_fd, LOCK_UN);
}
void ReplxxLineReader::enableBracketedPaste()

View File

@ -17,4 +17,7 @@ private:
void addToHistory(const String & line) override;
replxx::Replxx rx;
// used to call flock() to synchronize multiple clients using same history file
int history_file_fd = -1;
};