aboutsummaryrefslogtreecommitdiff
path: root/src/line
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-09 03:41:56 +0000
committer_ov4 <ov4@ov4.org>2026-10-09 03:41:56 +0000
commitc47cfb32f4ca9ed7bbb360053d90f1fb52a95708 (patch)
tree0d186530cbccb6f14f5230da1ad145b4c98aeac0 /src/line
parentd29590546054feabff2d7e559665811d1d28f538 (diff)
add line editor history
Diffstat (limited to 'src/line')
-rw-r--r--src/line/cursor.cc77
-rw-r--r--src/line/mode.cc15
2 files changed, 79 insertions, 13 deletions
diff --git a/src/line/cursor.cc b/src/line/cursor.cc
index 42b1685..bd2567e 100644
--- a/src/line/cursor.cc
+++ b/src/line/cursor.cc
@@ -10,6 +10,8 @@
#include <sys/ioctl.h>
#include "line/getter.h"
+#include "history.h"
+#include "line/mode.h"
using namespace std;
@@ -45,16 +47,12 @@ 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 'A':
+ switch_history(T_cursor_movement_direction::UP);
+ break;
+ case 'B':
+ switch_history(T_cursor_movement_direction::DOWN);
+ break;
case 'C':
// if (get_cursor_real_pos() < line_end_pos)
// {
@@ -82,6 +80,9 @@ void move_cursor(char c)
void cursor_input_char(char c)
{
+ // if receives any input
+ // it should move to the latest history
+ history_index = get_last_index();
// normal input
if (c != '\b')
{
@@ -115,7 +116,7 @@ void update_window_size()
{
winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == -1)
- exit(-1); // TODO: fix
+ editor_exit(-1); // TODO: fix
window_size_row = ws.ws_row;
window_size_col = ws.ws_col;
@@ -167,8 +168,60 @@ void move_cursor(T_cursor_movement_direction d)
break;
default:
- exit(-1); // TODO: fix
+ editor_exit(-1); // TODO: fix
+ }
+}
+
+void switch_history(T_cursor_movement_direction d)
+{
+ switch (d)
+ {
+ case T_cursor_movement_direction::UP:
+ if (history_index == 0)
+ {
+ print("\a"); fflush(stdout);
+ }
+ else
+ {
+ history_index--;
+ reset_editor();
+ line_buffer = history_vec[history_index];
+ // move to the "start" of the zone
+ // should skip prompt zone
+ print("\r"); fflush(stdout);
+ print("\x1b[{}C", prompt_pos-1); fflush(stdout);
+ print_override(line_buffer);
+ }
+ break;
+
+ case T_cursor_movement_direction::DOWN:
+ if (history_index == get_last_index())
+ {
+ print("\a"); fflush(stdout);
+ }
+ else
+ {
+ history_index++;
+ reset_editor();
+ line_buffer = history_vec[history_index];
+ // move to the "start" of the zone
+ // should skip prompt zone
+ print("\r"); fflush(stdout);
+ print("\x1b[{}C", prompt_pos-1); fflush(stdout);
+ print_override(line_buffer);
+ }
+ break;
+
+ default:
+ editor_exit(-1); // TODO: fix
}
}
+void reset_editor()
+{
+ line_buffer = "";
+ buffer_index_cursor = 0;
+ _buffer_index_display_begin = 0;
+}
+
}
diff --git a/src/line/mode.cc b/src/line/mode.cc
index e8024fa..dc9291d 100644
--- a/src/line/mode.cc
+++ b/src/line/mode.cc
@@ -7,6 +7,7 @@
#include "line/cursor.h"
#include "line/getter.h"
+#include "history.h"
using namespace std;
@@ -70,13 +71,16 @@ void clear_below()
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 (c == 'Q') break;
+ if (c == 'Q') editor_exit(-1); // TODO: remove
else if (c == 'C')
{
clear_below();
@@ -92,6 +96,7 @@ string readline()
if (c == 13) // \n
{
//print("line: {}\r\n", line_buffer);
+ history_vec[get_last_index()] = line_buffer;
return line_buffer;
}
@@ -117,8 +122,16 @@ string readline()
print_override(line_buffer);
}
}
+
+ history_vec[get_last_index()] = line_buffer;
return line_buffer;
}
+void editor_exit(int code)
+{
+ disable_raw();
+ exit(code);
+}
+
}