auto-complete.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 #include "auto-complete.h"
4 #include <iostream>
5 #include <thread>
6 #include <algorithm>
7 #include <cmath>
8 
9 using namespace std;
10 
11 
12 vector<string> auto_complete::search(const string& word) const
13 {
14  vector<string> finds;
15 
16  for (auto it = _dictionary.begin(); it != _dictionary.end(); ++it)
17  {
18  it = find_if(it, _dictionary.end(), [&](string elemnt) -> bool {
19  return (elemnt.compare(0, word.size(), word) == 0) ? true : false;
20  });
21 
22  if (it != _dictionary.end())
23  {
24  if (it->compare(word) == 0)
25  continue;
26 
27  finds.push_back(it->substr(word.size(), it->size()));
28  }
29  else
30  {
31  break;
32  }
33  }
34 
35  return finds;
36 }
37 
38 auto_complete::auto_complete(set<string> dictionary, bool bypass)
39  : _dictionary(dictionary), _history_words_index(0), _num_of_chars2_in_line(0), _tab_index(0), _is_first_time_up_arrow(true), _bypass(bypass)
40 {}
41 
43 {
44  auto temp_chars2_queue(_chars2_queue);
45  temp_chars2_queue.push_back('\0');
46  auto word = string(temp_chars2_queue.data());
47 
48  return word;
49 }
50 
51 string auto_complete::get_last_word(const string& line) const
52 {
53  auto index = line.find_last_of(' ');
54  if (index > line.size())
55  return line;
56 
57  return line.substr(index + 1);
58 }
59 
60 void auto_complete::backspace(const int num_of_backspaces)
61 {
62  for (auto i = 0; i < num_of_backspaces; ++i)
63  {
64  if (_num_of_chars2_in_line == 0)
65  break;
66 
67  cout << '\b';
69  cout << '\b';
70 
71  if (_num_of_chars2_in_line != 0)
73 
74  if (!_chars2_queue.empty())
75  {
76  _chars2_queue.pop_back();
77  }
78  }
79 }
80 
81 void auto_complete::handle_special_key(const vector<uint8_t>& chars)
82 {
83  auto up_arrow_key = vector<uint8_t>(UP_ARROW_KEY);
84  auto down_arrow_key = vector<uint8_t>(DOWN_ARROW_KEY);
85  if (up_arrow_key == chars && _history_words.size()) // Up arrow key
86  {
89 
90  _finds_vec.clear();
91  _tab_index = 0;
94 
95  for (auto c : _history_words[_history_words_index])
96  {
97  _chars2_queue.push_back(c);
99  }
100 
102  {
103  _is_first_time_up_arrow = false;
104  }
105  }
106  else if (down_arrow_key == chars && _history_words.size()) // Down arrow key
107  {
108  if ((_history_words_index + 1) < _history_words.size())
110 
111  _finds_vec.clear();
112  _tab_index = 0;
115 
116  for (auto c : _history_words[_history_words_index])
117  {
118  _chars2_queue.push_back(c);
120  }
121  }
122 }
123 
124 char auto_complete::getch_nolock(std::function <bool()> stop)
125 {
126 #if defined(_WIN32)
127  auto ch = static_cast<char>(_getch_nolock());
128  if (ch < 0)
129  {
130  vector<uint8_t> vec_ch = {static_cast<uint8_t>(_getch_nolock())};
131  handle_special_key(vec_ch);
132  return -1;
133  }
134 
135  return ch;
136 #else
137  auto fd = fileno(stdin);
138 
139  struct termios old_settings, new_settings;
140  tcgetattr(fd, &old_settings);
141  new_settings = old_settings;
142  new_settings.c_lflag &= (~ICANON & ~ECHO);
143 
144  tcsetattr(fd, TCSANOW, &new_settings);
145  fd_set fds;
146  struct timeval tv;
147 
148  tv.tv_sec = 1;
149  tv.tv_usec = 0;
150 
151  while(!stop())
152  {
153  FD_ZERO(&fds);
154  FD_SET(fd, &fds);
155 
156  auto res = select(fd+1, &fds, NULL, NULL, &tv );
157  if((res > 0) && (FD_ISSET(fd, &fds)))
158  {
159  uint8_t ch[10];
160  auto num_of_chars = read(fd, ch, 10 );
161  tcsetattr(fd, TCSAFLUSH, &old_settings );
162  if (num_of_chars == 1)
163  return ch[0];
164 
165  //TODO: Arrow keys
166  vector<uint8_t> vec_ch;
167  for (auto i = 0; i < num_of_chars; ++i)
168  vec_ch.push_back(ch[i]);
169 
170  handle_special_key(vec_ch);
171  return -1;
172  }
173  tcsetattr(fd, TCSAFLUSH, &old_settings );
174  return res;
175  }
176  return -1;
177 #endif
178 }
179 
180 
181 string auto_complete::get_line(std::function <bool()> to_stop)
182 {
183  if (_bypass)
184  {
185  string res;
186  std::getline(std::cin, res);
187  return res;
188  }
189 
190  while (!to_stop())
191  {
192  auto ch = getch_nolock();
193 
194  if (ch == -1)
195  {
196  cout.clear();
197  cin.clear();
198  fflush(nullptr);
199  continue;
200  }
201 
202  if (ch == '\t')
203  {
204  if (_finds_vec.empty())
205  {
207  }
208 
209 
210  if (_finds_vec.empty() || _tab_index == _finds_vec.size())
211  continue;
212 
213 
214  if (_tab_index != 0)
215  {
216  backspace(int(_finds_vec[_tab_index - 1].length()));
217  }
218 
219  auto vec_word = _finds_vec[_tab_index++];
220  cout << vec_word;
221 
222  for (auto c : vec_word)
223  {
224  _chars2_queue.push_back(c);
226  }
227  }
228  else if (ch == ' ')
229  {
230  _finds_vec.clear();
231  _tab_index = 0;
232  _chars2_queue.push_back(' ');
234 
235  cout << ch;
236  }
237  else if (ch == BACKSPACE)
238  {
239  _finds_vec.clear();
240  _tab_index = 0;
241 
242  backspace(1);
243  }
244  else if (ch == NEW_LINE)
245  {
246  _finds_vec.clear();
247  _tab_index = 0;
248 
249  cout << '\n';
250  cout << ch;
251  auto str = chars2_queue_to_string();
252 
253  if (str.compare(""))
254  {
255  _history_words.push_back(str);
256  _history_words_index = int(_history_words.size()) - 1;
257  }
259  }
260  else if (isprint(ch))
261  {
262  _finds_vec.clear();
263  _tab_index = 0;
264 
265  cout << ch;
266  _chars2_queue.push_back(ch);
268  }
269  else if(ch != 0)
270  getch_nolock(); // TODO
271 
272  if (ch == NEW_LINE)
273  {
274  auto ret_str = chars2_queue_to_string();
275  _chars2_queue.clear();
277  return ret_str;
278  }
279 
280  cout.clear();
281  cin.clear();
282  fflush(nullptr);
283  }
284  _chars2_queue.clear();
286  cout.clear();
287  cin.clear();
288  fflush(nullptr);
289  return std::string{};
290 }
std::vector< std::string > search(const std::string &word) const
std::string get_last_word(const std::string &line) const
GLsizei const GLchar *const * string
bool _is_first_time_up_arrow
Definition: auto-complete.h:49
auto_complete(std::set< std::string > dictionary, bool bypass=false)
unsigned char uint8_t
Definition: stdint.h:78
static const textual_icon stop
Definition: model-views.h:226
#define BACKSLASH_ZERO
Definition: auto-complete.h:23
std::vector< std::string > _history_words
Definition: auto-complete.h:43
GLuint index
#define NEW_LINE
Definition: auto-complete.h:21
void handle_special_key(const std::vector< uint8_t > &chars)
std::ostream & cout()
std::string get_line(std::function< bool()> to_stop=[]() {return false;})
#define UP_ARROW_KEY
Definition: auto-complete.h:24
std::vector< char > _chars2_queue
Definition: auto-complete.h:45
#define DOWN_ARROW_KEY
Definition: auto-complete.h:25
std::string chars2_queue_to_string() const
std::vector< std::string > _finds_vec
Definition: auto-complete.h:48
static auto it
void backspace(const int num_of_backspaces)
#define NULL
Definition: tinycthread.c:47
unsigned _history_words_index
Definition: auto-complete.h:44
int _num_of_chars2_in_line
Definition: auto-complete.h:46
int i
GLenum GLuint GLenum GLsizei length
unsigned _tab_index
Definition: auto-complete.h:47
#define BACKSPACE
Definition: auto-complete.h:22
char getch_nolock(std::function< bool()> stop=[](){return false;})


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:41:42