souffle  2.0.2-371-g6315b36
Public Member Functions | Private Attributes
souffle::profile::InputReader Class Reference

#include <UserInputReader.h>

Collaboration diagram for souffle::profile::InputReader:
Collaboration graph

Public Member Functions

void addHistory (std::string hist)
 
void appendTabCompletion (std::string command)
 
void appendTabCompletion (std::vector< std::string > commands)
 
void backspace ()
 
void clearHistory ()
 
void clearPrompt (size_t text_len)
 
void clearTabCompletion ()
 
std::string getInput ()
 
bool hasReceivedInput ()
 
void historyDown ()
 
void historyUp ()
 
 InputReader ()
 
 InputReader (const InputReader &other)=default
 
void moveCursor (char direction)
 
void moveCursorLeft ()
 
void moveCursorRight ()
 
void readchar ()
 
void setPrompt (std::string prompt)
 
void showFullText (const std::string &text)
 
void tabComplete ()
 

Private Attributes

char current_char = 0
 
std::string current_hist_val
 
std::vector< std::string > current_tab_completes
 
std::string current_tab_val
 
size_t cursor_pos = 0
 
size_t hist_pos = 0
 
std::vector< std::string > history
 
bool in_history = false
 
bool in_tab_complete = false
 
bool inputReceived = false
 
size_t original_hist_cursor_pos = 0
 
std::string original_hist_val
 
std::string original_tab_val
 
std::string output
 
std::string prompt = "Input: "
 
std::vector< std::string > tab_completion
 
size_t tab_pos = 0
 

Detailed Description

Definition at line 24 of file UserInputReader.h.

Constructor & Destructor Documentation

◆ InputReader() [1/2]

souffle::profile::InputReader::InputReader ( )
inline

Definition at line 45 of file UserInputReader.h.

45  {
47  clearHistory();
48  }

References clearHistory(), and clearTabCompletion().

Here is the call graph for this function:

◆ InputReader() [2/2]

souffle::profile::InputReader::InputReader ( const InputReader other)
default

Member Function Documentation

◆ addHistory()

void souffle::profile::InputReader::addHistory ( std::string  hist)
inline

Definition at line 202 of file UserInputReader.h.

202  {
203  if (history.size() > 0) {
204  // only add to history if the last command wasn't the same
205  if (hist == history.at(history.size() - 1)) {
206  return;
207  }
208  }
209  history.push_back(hist);
210  }

References history.

Referenced by souffle::profile::Tui::runProf().

◆ appendTabCompletion() [1/2]

void souffle::profile::InputReader::appendTabCompletion ( std::string  command)
inline

Definition at line 148 of file UserInputReader.h.

148  {
149  tab_completion.push_back(command);
150  }

References tab_completion.

◆ appendTabCompletion() [2/2]

void souffle::profile::InputReader::appendTabCompletion ( std::vector< std::string >  commands)
inline

Definition at line 145 of file UserInputReader.h.

145  {
146  tab_completion.insert(std::end(tab_completion), std::begin(commands), std::end(commands));
147  }

References tab_completion.

Referenced by setupTabCompletion().

◆ backspace()

void souffle::profile::InputReader::backspace ( )
inline

Definition at line 289 of file UserInputReader.h.

289  {
290  if (cursor_pos > 0) {
291  output.erase(output.begin() + cursor_pos - 1);
292  moveCursorLeft();
294  }
295  }

References cursor_pos, moveCursorLeft(), output, and showFullText().

Referenced by getInput().

Here is the call graph for this function:

◆ clearHistory()

void souffle::profile::InputReader::clearHistory ( )
inline

Definition at line 154 of file UserInputReader.h.

154  {
155  history = std::vector<std::string>();
156  }

References history.

Referenced by InputReader().

◆ clearPrompt()

void souffle::profile::InputReader::clearPrompt ( size_t  text_len)
inline

Definition at line 296 of file UserInputReader.h.

296  {
297  for (size_t i = cursor_pos; i < text_len + 1; i++) {
298  std::cout << (char)27 << "[C" << std::flush;
299  }
300  for (size_t i = 0; i < text_len + 1; i++) {
301  std::cout << "\b \b" << std::flush;
302  }
303  }

References cursor_pos, and i.

Referenced by historyDown(), historyUp(), showFullText(), and tabComplete().

◆ clearTabCompletion()

void souffle::profile::InputReader::clearTabCompletion ( )
inline

Definition at line 151 of file UserInputReader.h.

151  {
152  tab_completion = std::vector<std::string>();
153  }

References tab_completion.

Referenced by InputReader(), and setupTabCompletion().

◆ getInput()

std::string souffle::profile::InputReader::getInput ( )
inline

Definition at line 80 of file UserInputReader.h.

80  {
81  output = "";
82  current_char = 0;
83  cursor_pos = 0;
84  hist_pos = 0;
85  tab_pos = 0;
86  in_tab_complete = false;
87  in_history = false;
88 
89  std::cout << this->prompt << std::flush;
90  readchar();
91  inputReceived = true;
92 
93  bool escaped = false;
94  bool arrow_key = false;
95 
96  while (current_char != '\n') {
97  if (arrow_key) {
99  escaped = false;
100  arrow_key = false;
101  } else if (escaped) {
102  if (current_char == '[') {
103  arrow_key = true;
104  }
105  } else {
106  if (current_char == 27) { // esc char for arrow keys
107  escaped = true;
108  } else if (current_char == '\t') {
109  tabComplete();
110  } else {
111  if (in_history) {
113  in_history = false;
114 
115  } else if (in_tab_complete) {
117  in_tab_complete = false;
118  }
119 
120  if (current_char == 127) {
121  backspace();
122  } else {
123  output.insert(output.begin() + (cursor_pos++), current_char);
124  std::cout << current_char << std::flush;
126  }
127  }
128  }
129 
130  readchar();
131  }
132 
133  if (in_history) {
134  return current_hist_val;
135  }
136  if (in_tab_complete) {
137  return current_tab_val;
138  }
139 
140  return output;
141  }

References backspace(), current_char, current_hist_val, current_tab_val, cursor_pos, hist_pos, in_history, in_tab_complete, inputReceived, moveCursor(), output, readchar(), showFullText(), tab_pos, and tabComplete().

Referenced by souffle::profile::Tui::runProf().

Here is the call graph for this function:

◆ hasReceivedInput()

bool souffle::profile::InputReader::hasReceivedInput ( )
inline

Definition at line 52 of file UserInputReader.h.

52  {
53  return inputReceived;
54  }

References inputReceived.

Referenced by souffle::profile::Tui::runCommand(), and souffle::profile::Tui::Tui().

◆ historyDown()

void souffle::profile::InputReader::historyDown ( )
inline

Definition at line 241 of file UserInputReader.h.

241  {
242  if (in_history) {
244  if (hist_pos + 1 < history.size()) {
245  hist_pos++;
246  current_hist_val = history.at((unsigned)hist_pos);
247  cursor_pos = current_hist_val.size();
248  std::cout << current_hist_val << std::flush;
249  } else {
250  in_history = false;
252  std::cout << original_hist_val << std::flush;
253  }
254  } else {
255  std::cout << '\a' << std::flush;
256  }
257  }

References clearPrompt(), current_hist_val, cursor_pos, hist_pos, history, in_history, original_hist_cursor_pos, and original_hist_val.

Referenced by moveCursor().

Here is the call graph for this function:

◆ historyUp()

void souffle::profile::InputReader::historyUp ( )
inline

Definition at line 211 of file UserInputReader.h.

211  {
212  if (history.empty()) {
213  std::cout << '\a' << std::flush;
214  return;
215  }
216  if (in_tab_complete) {
218  in_tab_complete = false;
219  }
220  if (!in_history) {
223  in_history = true;
224  clearPrompt(output.size());
225  hist_pos = history.size() - 1;
226  current_hist_val = history.back();
227  cursor_pos = current_hist_val.size();
228  std::cout << current_hist_val << std::flush;
229  } else {
230  if (hist_pos > 0) {
231  hist_pos--;
233  current_hist_val = history.at((unsigned)hist_pos);
234  cursor_pos = current_hist_val.size();
235  std::cout << current_hist_val << std::flush;
236  } else {
237  std::cout << '\a' << std::flush;
238  }
239  }
240  }

References clearPrompt(), current_hist_val, current_tab_val, cursor_pos, hist_pos, history, in_history, in_tab_complete, original_hist_cursor_pos, original_hist_val, and output.

Referenced by moveCursor().

Here is the call graph for this function:

◆ moveCursor()

void souffle::profile::InputReader::moveCursor ( char  direction)
inline

Definition at line 258 of file UserInputReader.h.

258  {
259  switch (direction) {
260  case 'A': historyUp(); break;
261  case 'B': historyDown(); break;
262  case 'C': moveCursorRight(); break;
263  case 'D': moveCursorLeft(); break;
264  default: break;
265  }
266  }

References historyDown(), historyUp(), moveCursorLeft(), and moveCursorRight().

Referenced by getInput().

Here is the call graph for this function:

◆ moveCursorLeft()

void souffle::profile::InputReader::moveCursorLeft ( )
inline

Definition at line 283 of file UserInputReader.h.

283  {
284  if (cursor_pos > 0) {
285  cursor_pos--;
286  std::cout << (char)27 << '[' << 'D' << std::flush;
287  }
288  }

References cursor_pos.

Referenced by backspace(), and moveCursor().

◆ moveCursorRight()

void souffle::profile::InputReader::moveCursorRight ( )
inline

Definition at line 267 of file UserInputReader.h.

267  {
268  if (in_history) {
269  if (cursor_pos < current_hist_val.size()) {
270  cursor_pos++;
271  std::cout << (char)27 << '[' << 'C' << std::flush;
272  }
273  } else if (in_tab_complete) {
274  if (cursor_pos < current_tab_val.size()) {
275  cursor_pos++;
276  std::cout << (char)27 << '[' << 'C' << std::flush;
277  }
278  } else if (cursor_pos < output.size()) {
279  cursor_pos++;
280  std::cout << (char)27 << '[' << 'C' << std::flush;
281  }
282  }

References current_hist_val, current_tab_val, cursor_pos, in_history, in_tab_complete, and output.

Referenced by moveCursor().

◆ readchar()

void souffle::profile::InputReader::readchar ( )
inline

Definition at line 56 of file UserInputReader.h.

56  {
57  char buf = 0;
58  struct termios old = {};
59  if (tcgetattr(0, &old) < 0) {
60  perror("tcsetattr()");
61  }
62  old.c_lflag &= ~ICANON;
63  old.c_lflag &= ~ECHO;
64  old.c_cc[VMIN] = 1;
65  old.c_cc[VTIME] = 0;
66  if (tcsetattr(0, TCSANOW, &old) < 0) {
67  perror("tcsetattr ICANON");
68  }
69  if (::read(0, &buf, 1) < 0) {
70  perror("read()");
71  }
72  old.c_lflag |= ICANON;
73  old.c_lflag |= ECHO;
74  if (tcsetattr(0, TCSADRAIN, &old) < 0) {
75  perror("tcsetattr ~ICANON");
76  }
77 
78  current_char = buf;
79  }

References current_char.

Referenced by getInput().

◆ setPrompt()

void souffle::profile::InputReader::setPrompt ( std::string  prompt)
inline

Definition at line 142 of file UserInputReader.h.

142  {
143  this->prompt = prompt;
144  }

References prompt.

Referenced by souffle::profile::Tui::runProf().

◆ showFullText()

void souffle::profile::InputReader::showFullText ( const std::string &  text)
inline

Definition at line 304 of file UserInputReader.h.

304  {
305  clearPrompt(text.size());
306  for (char i : text) {
307  std::cout << i << std::flush;
308  }
309 
310  for (size_t i = cursor_pos; i < text.size(); i++) {
311  std::cout << "\b" << std::flush;
312  }
313  }

References clearPrompt(), cursor_pos, and i.

Referenced by backspace(), and getInput().

Here is the call graph for this function:

◆ tabComplete()

void souffle::profile::InputReader::tabComplete ( )
inline

Definition at line 157 of file UserInputReader.h.

157  {
158  if (in_history) {
160  in_history = false;
161  }
162  if (!in_tab_complete) {
163  current_tab_completes = std::vector<std::string>();
165  bool found_tab = false;
166  for (auto& a : tab_completion) {
167  if (a.find(original_tab_val) == 0) {
168  current_tab_completes.push_back(a);
169  found_tab = true;
170  }
171  }
172  if (!found_tab) {
173  std::cout << '\a' << std::flush;
174  return;
175  } else {
176  in_tab_complete = true;
177  tab_pos = 0;
179  clearPrompt(output.size());
180 
181  cursor_pos = current_tab_val.size();
182  std::cout << current_tab_val << std::flush;
183  }
184  } else {
185  if (tab_pos + 1 >= current_tab_completes.size()) {
188  in_tab_complete = false;
189  cursor_pos = output.size();
190  std::cout << output << std::flush;
191  } else {
192  tab_pos++;
193 
196 
197  cursor_pos = current_tab_val.size();
198  std::cout << current_tab_val << std::flush;
199  }
200  }
201  }

References clearPrompt(), current_hist_val, current_tab_completes, current_tab_val, cursor_pos, in_history, in_tab_complete, original_tab_val, output, tab_completion, and tab_pos.

Referenced by getInput().

Here is the call graph for this function:

Field Documentation

◆ current_char

char souffle::profile::InputReader::current_char = 0
private

Definition at line 30 of file UserInputReader.h.

Referenced by getInput(), and readchar().

◆ current_hist_val

std::string souffle::profile::InputReader::current_hist_val
private

Definition at line 37 of file UserInputReader.h.

Referenced by getInput(), historyDown(), historyUp(), moveCursorRight(), and tabComplete().

◆ current_tab_completes

std::vector<std::string> souffle::profile::InputReader::current_tab_completes
private

Definition at line 40 of file UserInputReader.h.

Referenced by tabComplete().

◆ current_tab_val

std::string souffle::profile::InputReader::current_tab_val
private

Definition at line 38 of file UserInputReader.h.

Referenced by getInput(), historyUp(), moveCursorRight(), and tabComplete().

◆ cursor_pos

size_t souffle::profile::InputReader::cursor_pos = 0
private

◆ hist_pos

size_t souffle::profile::InputReader::hist_pos = 0
private

Definition at line 32 of file UserInputReader.h.

Referenced by getInput(), historyDown(), and historyUp().

◆ history

std::vector<std::string> souffle::profile::InputReader::history
private

Definition at line 28 of file UserInputReader.h.

Referenced by addHistory(), clearHistory(), historyDown(), and historyUp().

◆ in_history

bool souffle::profile::InputReader::in_history = false
private

Definition at line 35 of file UserInputReader.h.

Referenced by getInput(), historyDown(), historyUp(), moveCursorRight(), and tabComplete().

◆ in_tab_complete

bool souffle::profile::InputReader::in_tab_complete = false
private

Definition at line 34 of file UserInputReader.h.

Referenced by getInput(), historyUp(), moveCursorRight(), and tabComplete().

◆ inputReceived

bool souffle::profile::InputReader::inputReceived = false
private

Definition at line 42 of file UserInputReader.h.

Referenced by getInput(), and hasReceivedInput().

◆ original_hist_cursor_pos

size_t souffle::profile::InputReader::original_hist_cursor_pos = 0
private

Definition at line 41 of file UserInputReader.h.

Referenced by historyDown(), and historyUp().

◆ original_hist_val

std::string souffle::profile::InputReader::original_hist_val
private

Definition at line 36 of file UserInputReader.h.

Referenced by historyDown(), and historyUp().

◆ original_tab_val

std::string souffle::profile::InputReader::original_tab_val
private

Definition at line 39 of file UserInputReader.h.

Referenced by tabComplete().

◆ output

std::string souffle::profile::InputReader::output
private

Definition at line 29 of file UserInputReader.h.

Referenced by backspace(), getInput(), historyUp(), moveCursorRight(), and tabComplete().

◆ prompt

std::string souffle::profile::InputReader::prompt = "Input: "
private

Definition at line 26 of file UserInputReader.h.

Referenced by setPrompt().

◆ tab_completion

std::vector<std::string> souffle::profile::InputReader::tab_completion
private

Definition at line 27 of file UserInputReader.h.

Referenced by appendTabCompletion(), clearTabCompletion(), and tabComplete().

◆ tab_pos

size_t souffle::profile::InputReader::tab_pos = 0
private

Definition at line 33 of file UserInputReader.h.

Referenced by getInput(), and tabComplete().


The documentation for this class was generated from the following file:
souffle::profile::InputReader::current_tab_completes
std::vector< std::string > current_tab_completes
Definition: UserInputReader.h:40
souffle::profile::InputReader::moveCursor
void moveCursor(char direction)
Definition: UserInputReader.h:258
souffle::profile::InputReader::in_history
bool in_history
Definition: UserInputReader.h:35
souffle::profile::InputReader::backspace
void backspace()
Definition: UserInputReader.h:289
souffle::profile::InputReader::prompt
std::string prompt
Definition: UserInputReader.h:26
souffle::profile::InputReader::original_hist_cursor_pos
size_t original_hist_cursor_pos
Definition: UserInputReader.h:41
souffle::profile::InputReader::current_char
char current_char
Definition: UserInputReader.h:30
souffle::profile::InputReader::moveCursorRight
void moveCursorRight()
Definition: UserInputReader.h:267
souffle::profile::InputReader::hist_pos
size_t hist_pos
Definition: UserInputReader.h:32
souffle::profile::InputReader::cursor_pos
size_t cursor_pos
Definition: UserInputReader.h:31
souffle::profile::InputReader::clearHistory
void clearHistory()
Definition: UserInputReader.h:154
souffle::profile::InputReader::showFullText
void showFullText(const std::string &text)
Definition: UserInputReader.h:304
souffle::profile::InputReader::tab_pos
size_t tab_pos
Definition: UserInputReader.h:33
souffle::profile::InputReader::current_hist_val
std::string current_hist_val
Definition: UserInputReader.h:37
souffle::profile::InputReader::current_tab_val
std::string current_tab_val
Definition: UserInputReader.h:38
souffle::profile::InputReader::clearPrompt
void clearPrompt(size_t text_len)
Definition: UserInputReader.h:296
i
size_t i
Definition: json11.h:663
souffle::profile::InputReader::historyDown
void historyDown()
Definition: UserInputReader.h:241
souffle::profile::InputReader::original_tab_val
std::string original_tab_val
Definition: UserInputReader.h:39
souffle::profile::InputReader::historyUp
void historyUp()
Definition: UserInputReader.h:211
souffle::profile::InputReader::original_hist_val
std::string original_hist_val
Definition: UserInputReader.h:36
souffle::profile::InputReader::in_tab_complete
bool in_tab_complete
Definition: UserInputReader.h:34
souffle::profile::InputReader::history
std::vector< std::string > history
Definition: UserInputReader.h:28
souffle::profile::InputReader::tabComplete
void tabComplete()
Definition: UserInputReader.h:157
souffle::profile::InputReader::tab_completion
std::vector< std::string > tab_completion
Definition: UserInputReader.h:27
souffle::profile::InputReader::output
std::string output
Definition: UserInputReader.h:29
souffle::profile::InputReader::clearTabCompletion
void clearTabCompletion()
Definition: UserInputReader.h:151
souffle::profile::InputReader::moveCursorLeft
void moveCursorLeft()
Definition: UserInputReader.h:283
souffle::profile::InputReader::inputReceived
bool inputReceived
Definition: UserInputReader.h:42
souffle::profile::InputReader::readchar
void readchar()
Definition: UserInputReader.h:56