aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-09-22 02:52:22 +0000
committer_ov4 <ov4@ov4.org>2026-09-22 02:52:22 +0000
commitc2336a8a22a5ddf26b33a53f914bf0d5397a6553 (patch)
tree9ef3dbe23ab28ed000acfaadd2c46e15e0f09abc
parent74e96589df2a77735410c5c03454a195d7efeecb (diff)
add cursor moving limit pos
-rw-r--r--include/line/cursor.h28
-rw-r--r--include/line/mode.h12
-rw-r--r--module-test/line_test.cc2
-rw-r--r--src/line/cursor.cc93
-rw-r--r--src/line/mode.cc68
5 files changed, 126 insertions, 77 deletions
diff --git a/include/line/cursor.h b/include/line/cursor.h
new file mode 100644
index 0000000..7417201
--- /dev/null
+++ b/include/line/cursor.h
@@ -0,0 +1,28 @@
+#pragma once
+
+#include <string>
+
+using namespace std; // TODO: remove
+
+namespace ov4
+{
+
+struct T_position
+{
+ int row;
+ int col;
+};
+
+inline int cursor_row;
+inline int cursor_col;
+
+inline int line_end_pos;
+
+T_position get_cursor_position();
+
+void print_override(const std::string &s);
+
+void move_cursor(char c);
+
+}
+
diff --git a/include/line/mode.h b/include/line/mode.h
index c0c8ace..85836c7 100644
--- a/include/line/mode.h
+++ b/include/line/mode.h
@@ -8,15 +8,7 @@ using namespace std; // TODO: remove
namespace ov4
{
-struct T_position
-{
- int row;
- int col;
-};
-
inline termios attr;
-inline int cursor_row;
-inline int cursor_col;
void disable_raw();
void enable_raw();
@@ -25,8 +17,4 @@ void clear_below();
std::string readline();
-T_position get_cursor_position();
-
-void print_override(const std::string &s);
-
}
diff --git a/module-test/line_test.cc b/module-test/line_test.cc
index 207e2e8..a608a16 100644
--- a/module-test/line_test.cc
+++ b/module-test/line_test.cc
@@ -3,6 +3,7 @@
$ g++ \
module-test/line_test.cc \
src/line/mode.cc \
+ src/line/cursor.cc \
-std=c++26 -Iinclude -Ithird_party/magic_enum/include \
-O0 -ggdb3 -fno-omit-frame-pointer -fno-inline -D_GLIBCXX_ASSERTIONS \
-o module-test/line_test.out
@@ -15,6 +16,7 @@
#include <unistd.h>
#include "line/mode.h"
+#include "line/cursor.h"
using namespace std;
using namespace ov4;
diff --git a/src/line/cursor.cc b/src/line/cursor.cc
new file mode 100644
index 0000000..ce01466
--- /dev/null
+++ b/src/line/cursor.cc
@@ -0,0 +1,93 @@
+#include "line/cursor.h"
+
+#include <print>
+#include <iostream>
+#include <string>
+#include <unistd.h>
+#include <cstdio>
+#include <cstdlib>
+#include <termios.h>
+
+using namespace std;
+
+namespace ov4
+{
+
+T_position 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;
+}
+
+void print_override(const std::string &s)
+{
+ print("\r");
+ fflush(stdout);
+ print("{}", s);
+ fflush(stdout);
+ cursor_col = s.size()+1;
+ line_end_pos = cursor_col;
+}
+
+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 'C':
+ if (cursor_col < line_end_pos)
+ {
+ print("\x1b[C");
+ fflush(stdout);
+ cursor_col++;
+ }
+ break;
+ case 'D':
+ if (cursor_col > 1)
+ {
+ print("\x1b[D");
+ fflush(stdout);
+ cursor_col--;
+ }
+ break;
+ default:
+ return;
+ }
+}
+
+}
diff --git a/src/line/mode.cc b/src/line/mode.cc
index 3ddd067..c4b9972 100644
--- a/src/line/mode.cc
+++ b/src/line/mode.cc
@@ -5,6 +5,8 @@
#include <iostream>
#include <print>
+#include "line/cursor.h"
+
using namespace std;
namespace ov4
@@ -96,31 +98,7 @@ string readline()
if (read(STDIN_FILENO, seq + 1, 1) != 1)
continue;
- switch (seq[1])
- {
- case 'A':
- print("\x1b[A");
- fflush(stdout);
- cursor_row--;
- break;
- case 'B':
- print("\x1b[B");
- fflush(stdout);
- cursor_row++;
- break;
- case 'C':
- print("\x1b[C");
- fflush(stdout);
- cursor_col--;
- break;
- case 'D':
- print("\x1b[D");
- fflush(stdout);
- cursor_col++;
- break;
- default:
- return "ERROR";
- }
+ move_cursor(seq[1]);
}
}
@@ -134,45 +112,5 @@ string readline()
return buffer;
}
-T_position 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;
-}
-
-void print_override(const std::string &s)
-{
- print("\r");
- fflush(stdout);
- print("{}", s);
- fflush(stdout);
- cursor_col = s.size()+1;
-}
}