aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-09-24 14:34:57 +0000
committer_ov4 <ov4@ov4.org>2026-09-24 14:34:57 +0000
commitd813a851ac9e642eff718882702d383bae2134b2 (patch)
tree1ccbe59640633aa5e379adb6fb1f40fd2f50696c
parent75270bdeaa993c3088667d5b3604fe9259bccadc (diff)
add index in buffer
-rw-r--r--include/line/cursor.h9
-rw-r--r--src/line/cursor.cc24
-rw-r--r--src/line/mode.cc2
3 files changed, 33 insertions, 2 deletions
diff --git a/include/line/cursor.h b/include/line/cursor.h
index 1641a28..cdea934 100644
--- a/include/line/cursor.h
+++ b/include/line/cursor.h
@@ -19,9 +19,14 @@ inline int cursor_col;
inline int line_end_pos;
inline int line_begin_pos;
+inline int window_size_row;
+inline int window_size_col;
+
// 0-based index
inline int line_buffer_index;
+inline int buffer_len;
+
T_position get_cursor_position();
void print_override(const std::string &s);
@@ -32,5 +37,9 @@ void cursor_input(char c, std::string &buffer);
int get_cursor_real_pos();
+void update_window_size();
+
+void update_line_end_pos();
+
}
diff --git a/src/line/cursor.cc b/src/line/cursor.cc
index 9630d34..64c3da8 100644
--- a/src/line/cursor.cc
+++ b/src/line/cursor.cc
@@ -7,6 +7,7 @@
#include <cstdio>
#include <cstdlib>
#include <termios.h>
+#include <sys/ioctl.h>
using namespace std;
@@ -101,12 +102,14 @@ void move_cursor(char c)
void cursor_input(char c, std::string &buffer)
{
+ // normal input
if (c != '\b')
{
buffer.insert(line_buffer_index, 1, c);
print("\x1b[C"); fflush(stdout);
line_buffer_index++;
- line_end_pos = line_begin_pos + buffer.size();
+ buffer_len = buffer.size();
+ update_line_end_pos();
return;
}
@@ -114,7 +117,8 @@ void cursor_input(char c, std::string &buffer)
buffer.erase(line_buffer_index, 1);
print("\x1b[D"); fflush(stdout);
line_buffer_index--;
- line_end_pos = line_begin_pos + buffer.size();
+ buffer_len = buffer.size();
+ update_line_end_pos();
}
int get_cursor_real_pos()
@@ -122,4 +126,20 @@ int get_cursor_real_pos()
return line_begin_pos + line_buffer_index;
}
+void update_window_size()
+{
+ winsize ws;
+ if (ioctl(1, TIOCGWINSZ, &ws) == -1)
+ exit(-1); // TODO: fix
+
+ window_size_row = ws.ws_row;
+ window_size_col = ws.ws_col;
+}
+
+void update_line_end_pos()
+{
+ line_end_pos = line_begin_pos + buffer_len;
+ line_end_pos = (line_end_pos > window_size_col) ? window_size_col : line_end_pos;
+}
+
}
diff --git a/src/line/mode.cc b/src/line/mode.cc
index 4122411..b61965d 100644
--- a/src/line/mode.cc
+++ b/src/line/mode.cc
@@ -68,6 +68,8 @@ void clear_below()
string readline()
{
+ update_window_size();
+
T_position pos = get_cursor_position();
line_begin_pos = pos.col;
string buffer;