LineEditor.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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()
  35. {
  36. m_history_cursor = m_history.size();
  37. m_cursor = 0;
  38. for (;;) {
  39. char keybuf[16];
  40. ssize_t nread = read(0, keybuf, sizeof(keybuf));
  41. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  42. if (nread == 0)
  43. exit(0);
  44. if (nread < 0) {
  45. if (errno == EINTR) {
  46. if (g.was_interrupted) {
  47. g.was_interrupted = false;
  48. if (!m_buffer.is_empty())
  49. printf("^C");
  50. }
  51. if (g.was_resized) {
  52. g.was_resized = false;
  53. printf("\033[2K\r");
  54. m_buffer.clear();
  55. return String::empty();
  56. }
  57. m_buffer.clear();
  58. putchar('\n');
  59. return String::empty();
  60. }
  61. perror("read failed");
  62. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  63. exit(2);
  64. }
  65. for (ssize_t i = 0; i < nread; ++i) {
  66. char ch = keybuf[i];
  67. if (ch == 0)
  68. continue;
  69. switch (m_state) {
  70. case InputState::ExpectBracket:
  71. if (ch == '[') {
  72. m_state = InputState::ExpectFinal;
  73. continue;
  74. } else {
  75. m_state = InputState::Free;
  76. break;
  77. }
  78. case InputState::ExpectFinal:
  79. switch (ch) {
  80. case 'A': // up
  81. if (m_history_cursor > 0)
  82. --m_history_cursor;
  83. clear_line();
  84. if (m_history_cursor < m_history.size())
  85. append(m_history[m_history_cursor]);
  86. m_state = InputState::Free;
  87. continue;
  88. case 'B': // down
  89. if (m_history_cursor < m_history.size())
  90. ++m_history_cursor;
  91. clear_line();
  92. if (m_history_cursor < m_history.size())
  93. append(m_history[m_history_cursor]);
  94. m_state = InputState::Free;
  95. continue;
  96. case 'D': // left
  97. if (m_cursor > 0) {
  98. --m_cursor;
  99. fputs("\033[D", stdout);
  100. fflush(stdout);
  101. }
  102. m_state = InputState::Free;
  103. continue;
  104. case 'C': // right
  105. if (m_cursor < m_buffer.size()) {
  106. ++m_cursor;
  107. fputs("\033[C", stdout);
  108. fflush(stdout);
  109. }
  110. m_state = InputState::Free;
  111. continue;
  112. case 'H':
  113. if (m_cursor > 0) {
  114. fprintf(stdout, "\033[%dD", m_cursor);
  115. fflush(stdout);
  116. m_cursor = 0;
  117. }
  118. m_state = InputState::Free;
  119. continue;
  120. case 'F':
  121. if (m_cursor < m_buffer.size()) {
  122. fprintf(stdout, "\033[%dC", m_buffer.size() - m_cursor);
  123. fflush(stdout);
  124. m_cursor = m_buffer.size();
  125. }
  126. m_state = InputState::Free;
  127. continue;
  128. default:
  129. dbgprintf("Shell: Unhandled final: %b (%c)\n", ch, ch);
  130. m_state = InputState::Free;
  131. continue;
  132. }
  133. break;
  134. case InputState::Free:
  135. if (ch == 27) {
  136. m_state = InputState::ExpectBracket;
  137. continue;
  138. }
  139. break;
  140. }
  141. if (ch == '\t') {
  142. // FIXME: Implement tab-completion.
  143. continue;
  144. }
  145. auto do_backspace = [&] {
  146. if (m_cursor == 0) {
  147. fputc('\a', stdout);
  148. fflush(stdout);
  149. return;
  150. }
  151. m_buffer.remove(m_cursor - 1);
  152. --m_cursor;
  153. putchar(8);
  154. vt_save_cursor();
  155. vt_clear_to_end_of_line();
  156. for (int i = m_cursor; i < m_buffer.size(); ++i)
  157. fputc(m_buffer[i], stdout);
  158. vt_restore_cursor();
  159. };
  160. if (ch == 8 || ch == g.termios.c_cc[VERASE]) {
  161. do_backspace();
  162. continue;
  163. }
  164. if (ch == g.termios.c_cc[VWERASE]) {
  165. bool has_seen_nonspace = false;
  166. while (m_cursor > 0) {
  167. if (isspace(m_buffer[m_cursor - 1])) {
  168. if (has_seen_nonspace)
  169. break;
  170. } else {
  171. has_seen_nonspace = true;
  172. }
  173. do_backspace();
  174. }
  175. continue;
  176. }
  177. if (ch == g.termios.c_cc[VKILL]) {
  178. while (m_cursor > 0)
  179. do_backspace();
  180. continue;
  181. }
  182. putchar(ch);
  183. fflush(stdout);
  184. if (ch == '\n') {
  185. auto string = String::copy(m_buffer);
  186. m_buffer.clear();
  187. return string;
  188. }
  189. if (m_cursor == m_buffer.size()) {
  190. m_buffer.append(ch);
  191. ++m_cursor;
  192. continue;
  193. }
  194. vt_save_cursor();
  195. vt_clear_to_end_of_line();
  196. for (int i = m_cursor; i < m_buffer.size(); ++i)
  197. fputc(m_buffer[i], stdout);
  198. vt_restore_cursor();
  199. m_buffer.insert(m_cursor, move(ch));
  200. ++m_cursor;
  201. }
  202. }
  203. }
  204. void LineEditor::vt_save_cursor()
  205. {
  206. fputs("\033[s", stdout);
  207. fflush(stdout);
  208. }
  209. void LineEditor::vt_restore_cursor()
  210. {
  211. fputs("\033[u", stdout);
  212. fflush(stdout);
  213. }
  214. void LineEditor::vt_clear_to_end_of_line()
  215. {
  216. fputs("\033[K", stdout);
  217. fflush(stdout);
  218. }