aboutsummaryrefslogtreecommitdiff
path: root/include/line/line_editor.h
blob: 8fa384147163fa9e075316afda5e0c9a0dc403e5 (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

#pragma once

#include <termios.h>
#include <string>

#include "line/type.h"

namespace ov4
{

// mode
inline termios attr;
inline bool raw_enabled = false;

void disable_raw();
void enable_raw();
void editor_exit(int code);
inline bool windows_size_changed = false;

class T_line_editor
{
public:
    // cursor

    // 1-based
    int cursor_row = 1;
    int cursor_col = 1;
    int prompt_pos = 1;
    int window_size_row = 24;
    int window_size_col = 80;

    // 0-based index
    int buffer_index_cursor = 0;
    // index [begin, end] is displayed
    // NB: CLOSE interval
    int _buffer_index_display_begin = 0;
    //int buffer_index_display_end;

    std::string line_buffer;

    void print_override(const std::string &s);
    void move_cursor(char c);
    void cursor_input_char(char c);
    void update_window_size();
    void move_cursor(T_cursor_movement_direction d);
    void switch_history(T_cursor_movement_direction d);
    void reset_editor();
    void cursor_skip_prompt();
    void check_window_update();


    // getter

    int get_terminal_pos_cursor();
    int get_buffer_index_display_begin();
    int get_buffer_index_display_end();
    int get_terminal_pos_line_end();

    //void update_line_end_pos();

    T_position get_cursor_position();


    // input
    void clear_below();
    std::string readline();
};

}