aboutsummaryrefslogtreecommitdiff
path: root/src/line/cursor.cc
blob: 9630d341affb22d238632dba7cd0ed0f651c1ddb (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

#include "line/cursor.h"

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

using namespace std;

namespace ov4
{

T_position get_cursor_position()
{
    char c;
    string buffer;
    T_position pos;
    int row, col;
    tcflush(STDIN_FILENO, TCIFLUSH);
    cout << "\x1b[6n" << flush;
    while (read(STDIN_FILENO, &c, 1) == 1)
    {
        if (c == 'R') break;
        buffer += c;
    }
    if (buffer.size() <= 2 || buffer[0] != '\x1b' || buffer[1] != '[')
    {
        pos.row = -1;
        pos.col = -1;
        return pos;
    }
    if ((sscanf(buffer.c_str(), "\x1b[%d;%d", &row, &col)) != 2)
    {
        pos.row = -1;
        pos.col = -1;
        return pos;
    }
    pos.row = row;
    pos.col = col;
    // print("row {}, col {}\r\n", row, col);
    // fflush(stdout);
    return pos;
} 

void print_override(const std::string &s)
{

    print("\x1b[s"); fflush(stdout);
    print("\r"); fflush(stdout);

    // move to the "start" of the zone
    // should skip prompt zone
    print("\x1b[{}C", line_begin_pos-1); fflush(stdout);

    // erase rest of it
    print("\x1b[K"); fflush(stdout);
    
    print("{}", s);
    fflush(stdout);
    //line_end_pos = s.size()+1;
    print("\x1b[u"); fflush(stdout);
}

void move_cursor(char c)
{
    switch (c)
    {
        // case 'A':
        //     print("\x1b[A");
        //     fflush(stdout);
        //     cursor_row--;
        //     break;
        // case 'B':
        //     print("\x1b[B");
        //     fflush(stdout);
        //     cursor_row++;
        //     break;
        case 'C':
            if (get_cursor_real_pos() < line_end_pos)
            {
                print("\x1b[C");
                fflush(stdout);
                line_buffer_index++;
            }
            break;
        case 'D':
            if (get_cursor_real_pos() > line_begin_pos)
            {
                print("\x1b[D");
                fflush(stdout);
                line_buffer_index--;
            }
            break;
        default:
            return;
    }
}

void cursor_input(char c, std::string &buffer)
{
    if (c != '\b')
    {
        buffer.insert(line_buffer_index, 1, c);
        print("\x1b[C"); fflush(stdout);
        line_buffer_index++;
        line_end_pos = line_begin_pos + buffer.size();
        return;
    }

    // BACKSPACE
    buffer.erase(line_buffer_index, 1);
    print("\x1b[D"); fflush(stdout);
    line_buffer_index--;
    line_end_pos = line_begin_pos + buffer.size();
}

int get_cursor_real_pos()
{
    return line_begin_pos + line_buffer_index;
}

}