diff options
| -rw-r--r-- | include/line/input.h | 14 | ||||
| -rw-r--r-- | include/line/mode.h | 4 | ||||
| -rw-r--r-- | module-test/line_test.cc | 2 | ||||
| -rw-r--r-- | src/line/cursor.cc | 1 | ||||
| -rw-r--r-- | src/line/input.cc | 94 | ||||
| -rw-r--r-- | src/line/mode.cc | 78 |
6 files changed, 111 insertions, 82 deletions
diff --git a/include/line/input.h b/include/line/input.h new file mode 100644 index 0000000..5330742 --- /dev/null +++ b/include/line/input.h @@ -0,0 +1,14 @@ +#pragma once + +#include <string> + +namespace ov4 +{ + +void clear_below(); + +std::string readline(); + +void editor_exit(int code); + +}
\ No newline at end of file diff --git a/include/line/mode.h b/include/line/mode.h index 97cea39..6a4d14d 100644 --- a/include/line/mode.h +++ b/include/line/mode.h @@ -14,10 +14,6 @@ inline bool raw_enabled = false; void disable_raw(); void enable_raw(); -void clear_below(); -std::string readline(); - -void editor_exit(int code); } diff --git a/module-test/line_test.cc b/module-test/line_test.cc index a54085c..6a0ef00 100644 --- a/module-test/line_test.cc +++ b/module-test/line_test.cc @@ -5,6 +5,7 @@ src/line/mode.cc \ src/line/cursor.cc \ src/line/getter.cc \ + src/line/input.cc \ src/history.cc \ src/path.cc \ src/util/string.cc \ @@ -23,6 +24,7 @@ #include "line/cursor.h" #include "line/getter.h" #include "line/type.h" +#include "line/input.h" #include "history.h" #include "path.h" diff --git a/src/line/cursor.cc b/src/line/cursor.cc index 9c0bf28..02e43cf 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -12,6 +12,7 @@ #include "line/getter.h" #include "history.h" #include "line/mode.h" +#include "line/input.h" using namespace std; diff --git a/src/line/input.cc b/src/line/input.cc new file mode 100644 index 0000000..9c593f7 --- /dev/null +++ b/src/line/input.cc @@ -0,0 +1,94 @@ +#include "line/input.h" + +#include <print> +#include <string> +#include <unistd.h> + +#include "line/cursor.h" +#include "history.h" +#include "line/getter.h" + +using namespace std; + +namespace ov4 +{ + +void clear_below() +{ + print("\x1b[0J"); +} + +string readline() +{ + update_window_size(); + history_vec.push_back(""); + history_index = get_last_index(); + reset_editor(); + + T_position pos = get_cursor_position(); + prompt_pos = pos.col; + char c; + while (read(STDIN_FILENO, &c, 1) == 1) + { + if (iscntrl(c)) + { + //print("control: {}\r\n", static_cast<unsigned char>(c)); + //fflush(stdout); + if (c == 0x0A || c == 0x0D) // \n OR \r + { + //print("line: {}\r\n", line_buffer); + if (line_buffer.empty()) + { + history_index--; + history_vec.pop_back(); + } + else + { + history_vec[get_last_index()] = line_buffer; + } + return line_buffer; + } + + else if (c == 0x03) // control C + { + history_index--; + history_vec.pop_back(); + return ""; + } + + // ESC sequence + else if (c == '\x1b') + { + char seq[10]; + // eat [ + if (read(STDIN_FILENO, seq + 0, 1) != 1) + continue; + + if (read(STDIN_FILENO, seq + 1, 1) != 1) + continue; + + move_cursor(seq[1]); + } + } + + // normal char + else + { + cursor_input_char(c); + print_override(line_buffer); + } + } + + if (line_buffer.empty()) + { + history_index--; + history_vec.pop_back(); + } + else + { + history_vec[get_last_index()] = line_buffer; + } + return line_buffer; +} + +}
\ No newline at end of file diff --git a/src/line/mode.cc b/src/line/mode.cc index 6f59ace..06f1727 100644 --- a/src/line/mode.cc +++ b/src/line/mode.cc @@ -63,84 +63,6 @@ void enable_raw() tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr_raw); } -void clear_below() -{ - print("\x1b[0J"); -} - -string readline() -{ - update_window_size(); - history_vec.push_back(""); - history_index = get_last_index(); - reset_editor(); - - T_position pos = get_cursor_position(); - prompt_pos = pos.col; - char c; - while (read(STDIN_FILENO, &c, 1) == 1) - { - if (iscntrl(c)) - { - //print("control: {}\r\n", static_cast<unsigned char>(c)); - //fflush(stdout); - if (c == 0x0A || c == 0x0D) // \n OR \r - { - //print("line: {}\r\n", line_buffer); - if (line_buffer.empty()) - { - history_index--; - history_vec.pop_back(); - } - else - { - history_vec[get_last_index()] = line_buffer; - } - return line_buffer; - } - - else if (c == 0x03) // control C - { - history_index--; - history_vec.pop_back(); - return ""; - } - - // ESC sequence - else if (c == '\x1b') - { - char seq[10]; - // eat [ - if (read(STDIN_FILENO, seq + 0, 1) != 1) - continue; - - if (read(STDIN_FILENO, seq + 1, 1) != 1) - continue; - - move_cursor(seq[1]); - } - } - - // normal char - else - { - cursor_input_char(c); - print_override(line_buffer); - } - } - - if (line_buffer.empty()) - { - history_index--; - history_vec.pop_back(); - } - else - { - history_vec[get_last_index()] = line_buffer; - } - return line_buffer; -} - void editor_exit(int code) { disable_raw(); |
