diff options
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | include/line/line_editor.h | 2 | ||||
| -rw-r--r-- | include/util/prompt.h | 4 | ||||
| -rw-r--r-- | src/line/getter.cc | 62 | ||||
| -rw-r--r-- | src/line/input.cc | 5 | ||||
| -rw-r--r-- | src/main.cc | 8 | ||||
| -rw-r--r-- | src/util/prompt.cc | 43 |
7 files changed, 72 insertions, 55 deletions
@@ -22,8 +22,9 @@ Support very complex syntax with a systematic language analysis system (it is im - brackets - multiple commands - TODO: pipeline, expansion, etc. +- Limit: terminal reflow is being done, but it may take some time -A fantastic line editor has been DONE, and it will be adapted very SOON! 😘 +A fantastic line editor has been DONE, and the only things left are pipeline and async! 😘 ## Build diff --git a/include/line/line_editor.h b/include/line/line_editor.h index 8fa3841..558bd83 100644 --- a/include/line/line_editor.h +++ b/include/line/line_editor.h @@ -63,7 +63,7 @@ public: // input void clear_below(); - std::string readline(); + std::string readline(int prompt_len); }; }
\ No newline at end of file diff --git a/include/util/prompt.h b/include/util/prompt.h index a99e933..28ec106 100644 --- a/include/util/prompt.h +++ b/include/util/prompt.h @@ -11,7 +11,7 @@ inline constexpr std::string_view prompt_normal = "$"; inline constexpr std::string_view prompt_root = "#"; extern bool emit_prompt; -void print_prompt(); -std::string get_prompt(); +int print_prompt(); +std::string get_prompt_symbol(); } diff --git a/src/line/getter.cc b/src/line/getter.cc index 432ddca..aa05d6b 100644 --- a/src/line/getter.cc +++ b/src/line/getter.cc @@ -38,36 +38,36 @@ int T_line_editor::get_terminal_pos_line_end() } -T_position T_line_editor::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; -} +// T_position T_line_editor::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; +// } }
\ No newline at end of file diff --git a/src/line/input.cc b/src/line/input.cc index 0a01760..1b9520b 100644 --- a/src/line/input.cc +++ b/src/line/input.cc @@ -18,15 +18,14 @@ void T_line_editor::clear_below() print("\x1b[0J"); } -string T_line_editor::readline() +string T_line_editor::readline(int prompt_len) { + prompt_pos = prompt_len+1; 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; int src; while (non_block_read_char(&c, &src) == 1) { diff --git a/src/main.cc b/src/main.cc index 55f669a..6fe48a4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -48,16 +48,12 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) while (1) { /* Read command line */ - print_prompt(); + int len = print_prompt(); T_line_editor line_editor_instance; enable_raw(); - T_position pos = line_editor_instance.get_cursor_position(); - line_editor_instance.cursor_row = pos.row; - line_editor_instance.cursor_col = pos.col; - line_editor_instance.prompt_pos = line_editor_instance.cursor_col; - string s = line_editor_instance.readline(); + string s = line_editor_instance.readline(len); disable_raw(); cout << "\n\n\n\n" << endl; cout << s << endl; diff --git a/src/util/prompt.cc b/src/util/prompt.cc index 92ce2ce..0046b0f 100644 --- a/src/util/prompt.cc +++ b/src/util/prompt.cc @@ -7,29 +7,50 @@ namespace ov4 bool emit_prompt = true; /* emit prompt (default) */ - void print_prompt() + int print_prompt() { if (emit_prompt) { char hostname[MAXLINE]; gethostname(hostname, MAXLINE); + string _getlogin = safe_str(getlogin()); + string _gethostname = safe_str(hostname); + string _get_current_dir = get_current_dir(); + string _get_prompt = get_prompt_symbol(); + string _at_symbol = "@"; + string _space = " "; + string _semicolon = ":"; + cout << make_color((string)name, ov4::color::bright_green) - << " " - << make_color(safe_str(getlogin()) , ov4::color::bright_blue) - << make_color("@", ov4::color::bright_blue) - << make_color(hostname, ov4::color::bright_blue) - << ":" - << make_color(get_current_dir(), ov4::color::bright_purple) - << " " - << make_color(get_prompt(), ov4::color::bright_yellow) - << " " + << _space + << make_color(_getlogin , ov4::color::bright_blue) + << make_color(_at_symbol, ov4::color::bright_blue) + << make_color(_gethostname, ov4::color::bright_blue) + << _semicolon + << make_color(_get_current_dir, ov4::color::bright_purple) + << _space + << make_color(_get_prompt, ov4::color::bright_yellow) + << _space << flush; + + return + name.size() + + _space.size() + + _getlogin.size() + + _at_symbol.size() + + _gethostname.size() + + _semicolon.size() + + _get_current_dir.size() + + _space.size() + + _get_prompt.size() + + _space.size(); } + return 0; } - string get_prompt() + string get_prompt_symbol() { return (string)(safe_str(getlogin()) == "root" ? prompt_root : prompt_normal); } |
