From c23e61b4455cee8669c7d594478a4238048f032f Mon Sep 17 00:00:00 2001 From: _ov4 Date: Mon, 19 Oct 2026 09:01:42 +0000 Subject: fix vt100 cursor pos race --- src/line/getter.cc | 62 +++++++++++++++++++++++++++--------------------------- src/line/input.cc | 5 ++--- src/main.cc | 8 ++----- src/util/prompt.cc | 43 +++++++++++++++++++++++++++---------- 4 files changed, 67 insertions(+), 51 deletions(-) (limited to 'src') 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); } -- cgit v1.3.1-4-g156e