LineEditor.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "LineEditor.h"
  2. #include "GlobalState.h"
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. LineEditor::LineEditor()
  7. {
  8. }
  9. LineEditor::~LineEditor()
  10. {
  11. }
  12. void LineEditor::add_to_history(const String& line)
  13. {
  14. if ((m_history.size() + 1) > m_history_capacity)
  15. m_history.take_first();
  16. m_history.append(line);
  17. }
  18. void LineEditor::clear_line()
  19. {
  20. for (int i = 0; i < m_cursor; ++i)
  21. fputc(0x8, stdout);
  22. fputs("\033[K", stdout);
  23. fflush(stdout);
  24. m_buffer.clear();
  25. m_cursor = 0;
  26. }
  27. void LineEditor::append(const String& string)
  28. {
  29. m_buffer.append(string.characters(), string.length());
  30. fputs(string.characters(), stdout);
  31. fflush(stdout);
  32. m_cursor = m_buffer.size();
  33. }
  34. String LineEditor::get_line(const String& prompt)
  35. {
  36. fputs(prompt.characters(), stdout);
  37. fflush(stdout);
  38. m_history_cursor = m_history.size();
  39. m_cursor = 0;
  40. for (;;) {
  41. char keybuf[16];
  42. ssize_t nread = read(0, keybuf, sizeof(keybuf));
  43. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  44. if (nread == 0)
  45. exit(0);
  46. if (nread < 0) {
  47. if (errno == EINTR) {
  48. if (g.was_interrupted) {
  49. g.was_interrupted = false;
  50. if (!m_buffer.is_empty())
  51. printf("^C");
  52. }
  53. if (g.was_resized) {
  54. g.was_resized = false;
  55. printf("\033[2K\r");
  56. m_buffer.clear();
  57. return String::empty();
  58. }
  59. m_buffer.clear();
  60. putchar('\n');
  61. return String::empty();
  62. }
  63. perror("read failed");
  64. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  65. exit(2);
  66. }
  67. auto do_delete = [&] {
  68. if (m_cursor == m_buffer.size()) {
  69. fputc('\a', stdout);
  70. fflush(stdout);
  71. return;
  72. }
  73. m_buffer.remove(m_cursor - 1);
  74. fputs("\033[3~", stdout);
  75. fflush(stdout);
  76. vt_save_cursor();
  77. vt_clear_to_end_of_line();
  78. for (int i = m_cursor; i < m_buffer.size(); ++i)
  79. fputc(m_buffer[i], stdout);
  80. vt_restore_cursor();
  81. };
  82. for (ssize_t i = 0; i < nread; ++i) {
  83. char ch = keybuf[i];
  84. if (ch == 0)
  85. continue;
  86. switch (m_state) {
  87. case InputState::ExpectBracket:
  88. if (ch == '[') {
  89. m_state = InputState::ExpectFinal;
  90. continue;
  91. } else {
  92. m_state = InputState::Free;
  93. break;
  94. }
  95. case InputState::ExpectFinal:
  96. switch (ch) {
  97. case 'A': // up
  98. if (m_history_cursor > 0)
  99. --m_history_cursor;
  100. clear_line();
  101. if (m_history_cursor < m_history.size())
  102. append(m_history[m_history_cursor]);
  103. m_state = InputState::Free;
  104. continue;
  105. case 'B': // down
  106. if (m_history_cursor < m_history.size())
  107. ++m_history_cursor;
  108. clear_line();
  109. if (m_history_cursor < m_history.size())
  110. append(m_history[m_history_cursor]);
  111. m_state = InputState::Free;
  112. continue;
  113. case 'D': // left
  114. if (m_cursor > 0) {
  115. --m_cursor;
  116. fputs("\033[D", stdout);
  117. fflush(stdout);
  118. }
  119. m_state = InputState::Free;
  120. continue;
  121. case 'C': // right
  122. if (m_cursor < m_buffer.size()) {
  123. ++m_cursor;
  124. fputs("\033[C", stdout);
  125. fflush(stdout);
  126. }
  127. m_state = InputState::Free;
  128. continue;
  129. case 'H':
  130. if (m_cursor > 0) {
  131. fprintf(stdout, "\033[%dD", m_cursor);
  132. fflush(stdout);
  133. m_cursor = 0;
  134. }
  135. m_state = InputState::Free;
  136. continue;
  137. case 'F':
  138. if (m_cursor < m_buffer.size()) {
  139. fprintf(stdout, "\033[%dC", m_buffer.size() - m_cursor);
  140. fflush(stdout);
  141. m_cursor = m_buffer.size();
  142. }
  143. m_state = InputState::Free;
  144. continue;
  145. case '3':
  146. do_delete();
  147. m_state = InputState::ExpectTerminator;
  148. continue;
  149. default:
  150. dbgprintf("Shell: Unhandled final: %b (%c)\n", ch, ch);
  151. m_state = InputState::Free;
  152. continue;
  153. }
  154. break;
  155. case InputState::ExpectTerminator:
  156. m_state = InputState::Free;
  157. continue;
  158. case InputState::Free:
  159. if (ch == 27) {
  160. m_state = InputState::ExpectBracket;
  161. continue;
  162. }
  163. break;
  164. }
  165. if (ch == '\t') {
  166. // FIXME: Implement tab-completion.
  167. continue;
  168. }
  169. auto do_backspace = [&] {
  170. if (m_cursor == 0) {
  171. fputc('\a', stdout);
  172. fflush(stdout);
  173. return;
  174. }
  175. m_buffer.remove(m_cursor - 1);
  176. --m_cursor;
  177. putchar(8);
  178. vt_save_cursor();
  179. vt_clear_to_end_of_line();
  180. for (int i = m_cursor; i < m_buffer.size(); ++i)
  181. fputc(m_buffer[i], stdout);
  182. vt_restore_cursor();
  183. };
  184. if (ch == 8 || ch == g.termios.c_cc[VERASE]) {
  185. do_backspace();
  186. continue;
  187. }
  188. if (ch == g.termios.c_cc[VWERASE]) {
  189. bool has_seen_nonspace = false;
  190. while (m_cursor > 0) {
  191. if (isspace(m_buffer[m_cursor - 1])) {
  192. if (has_seen_nonspace)
  193. break;
  194. } else {
  195. has_seen_nonspace = true;
  196. }
  197. do_backspace();
  198. }
  199. continue;
  200. }
  201. if (ch == g.termios.c_cc[VKILL]) {
  202. while (m_cursor > 0)
  203. do_backspace();
  204. continue;
  205. }
  206. if (ch == 0xc) { // ^L
  207. printf("\033[3J\033[H\033[2J"); // Clear screen.
  208. fputs(prompt.characters(), stdout);
  209. for (int i = 0; i < m_buffer.size(); ++i)
  210. fputc(m_buffer[i], stdout);
  211. if (m_cursor < m_buffer.size())
  212. printf("\033[%dD", m_buffer.size() - m_cursor); // Move cursor N steps left.
  213. fflush(stdout);
  214. continue;
  215. }
  216. putchar(ch);
  217. fflush(stdout);
  218. if (ch == '\n') {
  219. auto string = String::copy(m_buffer);
  220. m_buffer.clear();
  221. return string;
  222. }
  223. if (m_cursor == m_buffer.size()) {
  224. m_buffer.append(ch);
  225. ++m_cursor;
  226. continue;
  227. }
  228. vt_save_cursor();
  229. vt_clear_to_end_of_line();
  230. for (int i = m_cursor; i < m_buffer.size(); ++i)
  231. fputc(m_buffer[i], stdout);
  232. vt_restore_cursor();
  233. m_buffer.insert(m_cursor, move(ch));
  234. ++m_cursor;
  235. }
  236. }
  237. }
  238. void LineEditor::vt_save_cursor()
  239. {
  240. fputs("\033[s", stdout);
  241. fflush(stdout);
  242. }
  243. void LineEditor::vt_restore_cursor()
  244. {
  245. fputs("\033[u", stdout);
  246. fflush(stdout);
  247. }
  248. void LineEditor::vt_clear_to_end_of_line()
  249. {
  250. fputs("\033[K", stdout);
  251. fflush(stdout);
  252. }