aboutsummaryrefslogtreecommitdiff
path: root/src/line/cursor.cc
blob: 81454ae72c0b7f5a9733dbccd9b6703281a81c9e (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

#include "line/line_editor.h"

#include <print>
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <termios.h>
#include <sys/ioctl.h>

#include "history.h"

using namespace std;

namespace ov4
{

void T_line_editor::print_override(const std::string &s)
{
    check_window_update();
    
    // save current cursor position
    // print("\x1b[s"); fflush(stdout);
    // WTF
    // ESC 7     Save Cursor (DECSC), VT100.
    print("\x1b" "7"); fflush(stdout);

    cursor_skip_prompt();

    // erase rest of the line
    print("\x1b[K"); fflush(stdout);
    
    print("{}", s.substr(get_buffer_index_display_begin(), 
        get_buffer_index_display_end() - get_buffer_index_display_begin() + 1)); fflush(stdout);
    //line_end_pos = s.size()+1;

    // restore cursor position
    // ESC 8     Restore Cursor (DECRC), VT100.
    print("\x1b" "8"); fflush(stdout);
}

void T_line_editor::move_cursor(char c)
{
    check_window_update();

    switch (c)
    {
        case 'A':
            switch_history(T_cursor_movement_direction::UP);
            break;
        case 'B':
            switch_history(T_cursor_movement_direction::DOWN);
            break;
        case 'C':
            move_cursor(T_cursor_movement_direction::RIGHT);
            break;
        case 'D':
            move_cursor(T_cursor_movement_direction::LEFT);
            break;
        default:
            return;
    }
}

void T_line_editor::cursor_input_char(char c)
{
    check_window_update();

    // if receives any input
    // it should move to the latest history
    history_index = get_last_index();
    // normal input
    if (c != '\b' && c != '\x7f')
    {
        line_buffer.insert(buffer_index_cursor, 1, c);
        move_cursor(T_cursor_movement_direction::RIGHT);
        
        return;
    }

    // BACKSPACE
    if (buffer_index_cursor != 0)
    {
        line_buffer.erase(buffer_index_cursor-1, 1);
        move_cursor(T_cursor_movement_direction::LEFT);
    }
    else
    {
        print("\a"); fflush(stdout);
    }
    
}



void T_line_editor::update_window_size() 
{
    winsize ws;
    if (ioctl(1, TIOCGWINSZ, &ws) == -1) 
        editor_exit(-1); // TODO: fix

    window_size_row = ws.ws_row;
    window_size_col = ws.ws_col;
}

void T_line_editor::move_cursor(T_cursor_movement_direction d)
{
    check_window_update();

    switch (d)
    {
        case T_cursor_movement_direction::LEFT:
            // if cursor is not at left margin
            if (prompt_pos+1 < get_terminal_pos_cursor())
            {
                buffer_index_cursor--;
                print("\x1b[D"); fflush(stdout); // move cursor to the left
            }
            // margin
            else
            {
                // already at the beginning of buffer
                if (buffer_index_cursor == 0)
                {
                    print("\a"); fflush(stdout);
                    break;
                }

                buffer_index_cursor--;
                _buffer_index_display_begin--;
                print_override(line_buffer);
            }
            break;

        case T_cursor_movement_direction::RIGHT:
            // if cursor is not at right margin
            if (window_size_col > get_terminal_pos_cursor())
            {
                // already at the end of buffer
                if (buffer_index_cursor == line_buffer.size())
                {
                    print("\a"); fflush(stdout);
                    break;
                }

                buffer_index_cursor++;
                print("\x1b[C"); fflush(stdout); // move cursor to the right
            }
            // margin
            else
            {
                // already at the end of buffer
                if (buffer_index_cursor == line_buffer.size())
                {
                    print("\a"); fflush(stdout);
                    break;
                }

                buffer_index_cursor++;
                _buffer_index_display_begin++;
                print_override(line_buffer);
            }
            break;

        default:
            editor_exit(-1); // TODO: fix
    }
}

void T_line_editor::switch_history(T_cursor_movement_direction d)
{
    check_window_update();

    switch (d)
    {
        case T_cursor_movement_direction::UP:
            if (history_index == 0)
            {
                print("\a"); fflush(stdout);
            }
            else
            {
                history_index--;
                reset_editor();
                line_buffer = history_vec[history_index];
                print_override(line_buffer);
            }
            break;

        case T_cursor_movement_direction::DOWN:
            if (history_index == get_last_index())
            {
                print("\a"); fflush(stdout);
            }
            else
            {
                history_index++;
                reset_editor();
                line_buffer = history_vec[history_index];
                print_override(line_buffer);
            }
            break;

        default:
            editor_exit(-1); // TODO: fix
    }
}

void T_line_editor::reset_editor()
{
    line_buffer = "";
    buffer_index_cursor = 0;
    _buffer_index_display_begin = 0;
    cursor_skip_prompt();
}

void T_line_editor::cursor_skip_prompt()
{
    // move to the "start" of the zone
    // should skip prompt zone
    print("\r\x1b[{}C", prompt_pos-1); fflush(stdout);
}

void T_line_editor::check_window_update()
{
    if (windows_size_changed)
    {
        update_window_size();
        clear_below();
        windows_size_changed = false;
    }
}

}