This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathLineEditor.cpp
166 lines (142 loc) · 2.98 KB
/
LineEditor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright © 2016-2023 Byteduck */
#include "LineEditor.h"
#include <unistd.h>
using namespace TUI;
int main() {
LineEditor editor;
while(true) {
printf("Type: ");
fflush(stdout);
printf("You typed: '%s'\n", editor.get_line().c_str());
}
}
LineEditor::LineEditor() {
// Get the termios and set it up, we want to turn off canonical mode and echoing
tcgetattr(STDIN_FILENO, &m_termios);
m_termios.c_lflag &= ~(ICANON | ECHO);
}
std::string LineEditor::get_line() {
// Set up our termios
termios old_termios;
tcgetattr(STDIN_FILENO, &old_termios);
tcsetattr(STDIN_FILENO, TCSANOW, &m_termios);
m_buffer.clear();
m_cursor = m_buffer.size();
m_state = REGULAR;
while(m_state != DONE) {
int ch = std::getchar();
if (ch == EOF)
continue;
switch(m_state) {
case REGULAR:
handle_regular_char(ch);
break;
case ESCAPE_START:
m_state = ch == '[' ? ESCAPE : REGULAR;
break;
case ESCAPE:
handle_escape_char(ch);
break;
case DONE:
break; // Shouldn't happen
}
}
printf("\n");
// Restore our old termios
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
return m_buffer;
}
void LineEditor::set_line(const std::string& line) {
clear_line();
m_buffer = line;
reprint_line();
move_cursor(line.size());
}
void LineEditor::clear_line() {
m_buffer = "";
reprint_line();
m_cursor = m_buffer.size();
}
void LineEditor::reprint_line() {
// Clear line
printf("\033[%dD", m_cursor);
printf("\033[K");
// Reprint line
if(!m_buffer.empty())
printf("%s", m_buffer.c_str());
// Move cursor back to where it was
if(m_buffer.size() - m_cursor)
printf("\033[%dD", m_buffer.size() - m_cursor);
fflush(stdout);
}
void LineEditor::move_cursor(int amount) {
if(!amount)
return;
m_cursor += amount;
if(m_cursor < 0)
m_cursor = 0;
else if(m_cursor > m_buffer.size())
m_cursor = m_buffer.size();
else {
char control_char = 'C';
if(amount < 0) {
control_char = 'D';
amount *= -1;
}
printf("\033[%d%c", amount, control_char);
fflush(stdout);
}
}
void LineEditor::handle_regular_char(char ch) {
if(ch == '\n') {
m_state = DONE;
return;
}
if(ch == '\b' || ch == 0x7f) {
move_cursor(-1);
if(!m_buffer.empty())
m_buffer.erase(m_cursor);
reprint_line();
return;
}
if(ch == '\033') {
m_state = ESCAPE_START;
return;
}
if(ch == '\t') {
if(tab_pressed)
tab_pressed();
return;
}
m_buffer.insert(m_buffer.begin() + m_cursor, ch);
move_cursor(1);
reprint_line();
}
void LineEditor::handle_escape_char(char ch) {
switch(ch) {
case 'A': // Up arrow
m_state = REGULAR;
if(up_pressed)
up_pressed();
break;
case 'B': // Down Arrow
m_state = REGULAR;
if(down_pressed)
down_pressed();
break;
case 'C': // Right arrow
if(m_cursor < m_buffer.size())
move_cursor(1);
m_state = REGULAR;
break;
case 'D': // Left arrow
if(m_cursor >= 0)
move_cursor(-1);
m_state = REGULAR;
break;
default:
m_state = REGULAR;
break;
}
}