semf
tabulator.cpp
Go to the documentation of this file.
1
11#include <algorithm>
12
13namespace semf::esh
14{
15Tabulator::Tabulator(Printer& printer, const LinkedList<Command>& commands, char lineBuffer[], size_t lineBufferSize, std::string_view prompt)
16: m_printer(printer),
17 m_commands(commands),
18 m_lineBuffer(lineBuffer),
19 m_lineBufferSize(lineBufferSize),
20 kPrompt(prompt)
21{
22}
23
24void Tabulator::start(size_t charCount)
25{
26 if (m_busy)
27 {
28 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Start_IsBusy)));
29 return;
30 }
31
32 m_busy = true;
33 m_charCount = charCount;
34 m_matchCount = 0;
35 m_current = m_commands.cbegin();
36 searchMatches();
37}
38
40{
41 return m_busy;
42}
43
44bool Tabulator::prefixMatch(std::string_view sub, std::string_view str) const
45{
46 if (sub.size() > str.size())
47 return false;
48 str.remove_suffix(str.size() - sub.size());
49 return sub == str;
50}
51
52void Tabulator::searchMatches()
53{
54 for (; m_current != m_commands.cend(); m_current++)
55 {
56 if (prefixMatch(std::string_view(m_lineBuffer, m_charCount), m_current->name()))
57 {
58 m_matchCount++;
59 m_lastMatch = m_current;
60 m_printer.dataWritten.connect(m_printCommandNameSlot);
61 m_printer.print("\r\n");
62 return;
63 }
64 }
65
66 if (m_matchCount == 1)
67 {
68 m_charCount = m_lastMatch->name().size();
69 std::copy(m_lastMatch->name().begin(), m_lastMatch->name().end(), m_lineBuffer);
70 m_lineBuffer[m_charCount] = 0;
71 }
72
73 if (m_matchCount != 0)
74 {
75 m_printer.dataWritten.clear();
76 m_printer.dataWritten.connect(m_updatePromptSlot);
77 m_printer.print("\r\n");
78 }
79 else
80 {
81 onDone();
82 }
83}
84
85void Tabulator::printCommandName()
86{
87 m_printer.dataWritten.disconnect(m_printCommandNameSlot);
88 m_printer.dataWritten.connect(m_continueSearchSlot);
89 m_printer.print(m_current->name());
90}
91
92void Tabulator::continueSearch()
93{
94 m_printer.dataWritten.disconnect(m_continueSearchSlot);
95 m_current++;
96 searchMatches();
97}
98
99void Tabulator::updatePrompt()
100{
101 m_printer.dataWritten.clear();
102 m_printer.dataWritten.connect(m_printCommandSlot);
103 m_printer.print(kPrompt);
104}
105
106void Tabulator::printCommand()
107{
108 m_printer.dataWritten.clear();
109 m_printer.dataWritten.connect(m_onDoneSlot);
110 m_printer.print(m_lineBuffer);
111}
112
113void Tabulator::onDone()
114{
115 m_printer.dataWritten.disconnect(m_onDoneSlot);
116 m_busy = false;
117 done(m_charCount);
118}
119} // namespace semf::esh
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
LinkedList is an managed double linked list implementation.
Definition: linkedlist.h:43
Class for string related UART communication. Using this class outside of an esh-context can make sens...
Definition: printer.h:25
void print(char character, size_t count)
Prints a single character count times.
Definition: printer.cpp:20
void start(size_t charCount)
Starts the auto completion.
Definition: tabulator.cpp:24
bool isBusy() const
Returns the busy-flag.
Definition: tabulator.cpp:39
Tabulator(Printer &printer, const LinkedList< Command > &commands, char lineBuffer[], size_t lineBufferSize, std::string_view prompt)
Constructor.
Definition: tabulator.cpp:15