semf
history.cpp
Go to the documentation of this file.
1
10#ifndef THIRDPARTY_ESH_SHELLHISTORY_CPP_
11#define THIRDPARTY_ESH_SHELLHISTORY_CPP_
12
14#include <algorithm>
15
16namespace semf::esh
17{
18History::History(char* historyBuffer, size_t numberOfEntries, size_t lineSize)
19: m_history{historyBuffer},
20 m_numberOfEntries{numberOfEntries},
21 m_lineSize{lineSize}
22{
23}
24
25void History::insert(std::string_view command)
26{
27 if (command.empty() || m_history == nullptr)
28 {
29 return;
30 }
31 int index = m_savedCommands % m_numberOfEntries;
32 std::copy_n(command.begin(), std::min(command.size(), m_lineSize - 1), m_history + index * m_lineSize);
33 m_history[index * m_lineSize + std::min(command.size(), m_lineSize - 1)] = 0;
34 m_index = ++m_savedCommands;
35}
36
37void History::handleArrowUp(char commandBuffer[])
38{
39 if (!(m_index < static_cast<int32_t>(m_savedCommands - m_numberOfEntries) || m_index == 0 || m_history == nullptr))
40 {
41 m_index--;
42 int index = m_index % m_numberOfEntries;
43 if (std::equal(m_history + index * m_lineSize, m_history + index * m_lineSize + m_lineSize, commandBuffer))
44 return;
45 std::copy(m_history + index * m_lineSize, m_history + index * m_lineSize + m_lineSize, commandBuffer);
46 }
47 else
48 {
49 commandBuffer[0] = 0;
50 }
51}
52void History::handleArrowDown(char commandBuffer[])
53{
54 if (m_index == m_savedCommands || m_history == nullptr)
55 {
56 commandBuffer[0] = 0;
57 return;
58 }
59
60 m_index++;
61 int index = m_index % m_numberOfEntries;
62 std::copy(m_history + index * m_lineSize, m_history + index * m_lineSize + m_lineSize, commandBuffer);
63}
64} // namespace semf::esh
65#endif /* THIRDPARTY_ESH_SHELLHISTORY_CPP_ */
void insert(std::string_view command)
Inserts a command.
Definition: history.cpp:25
History(char *historyBuffer, size_t numberOfEntries, size_t lineSize)
Constructor.
Definition: history.cpp:18
void handleArrowUp(char commandBuffer[])
Performes a lookup based on an up-arrow key stroke.
Definition: history.cpp:37
void handleArrowDown(char commandBuffer[])
Performes a lookup based on an down-arrow key stroke.
Definition: history.cpp:52