aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/line/cursor.h29
-rw-r--r--include/line/getter.h14
-rw-r--r--include/line/mode.h2
-rw-r--r--include/line/type.h18
-rw-r--r--src/line/cursor.cc85
-rw-r--r--src/line/getter.cc69
-rw-r--r--src/line/mode.cc2
7 files changed, 136 insertions, 83 deletions
diff --git a/include/line/cursor.h b/include/line/cursor.h
index 7cae165..6e62bce 100644
--- a/include/line/cursor.h
+++ b/include/line/cursor.h
@@ -2,53 +2,38 @@
#include <string>
+#include "line/type.h"
+
using namespace std; // TODO: remove
namespace ov4
{
-struct T_position
-{
- int row;
- int col;
-};
-
-enum class T_cursor_movement_direction
-{
- LEFT,
- RIGHT,
-};
-
// 1-based
inline int cursor_row;
inline int cursor_col;
inline int line_end_pos;
-inline int line_begin_pos;
+
+inline int prompt_pos;
inline int window_size_row;
inline int window_size_col;
// 0-based index
-inline int line_buffer_index_cursor;
+inline int buffer_index_cursor;
// index [begin, end] is displayed
// NB: CLOSE interval
-inline int line_buffer_index_begin;
-inline int line_buffer_index_end;
-
-inline int buffer_len;
+inline int _buffer_index_display_begin;
+//inline int buffer_index_display_end;
inline std::string line_buffer;
-T_position get_cursor_position();
-
void print_override(const std::string &s);
void move_cursor(char c);
void cursor_input_char(char c);
-int get_cursor_real_pos();
-
void update_window_size();
void update_line_end_pos();
diff --git a/include/line/getter.h b/include/line/getter.h
new file mode 100644
index 0000000..a1c3765
--- /dev/null
+++ b/include/line/getter.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "line/type.h"
+
+namespace ov4
+{
+
+int get_terminal_pos_cursor();
+int get_buffer_index_display_begin();
+int get_buffer_index_display_end();
+
+T_position get_cursor_position();
+
+} \ No newline at end of file
diff --git a/include/line/mode.h b/include/line/mode.h
index be0a453..d080fc3 100644
--- a/include/line/mode.h
+++ b/include/line/mode.h
@@ -3,6 +3,8 @@
#include <termios.h>
#include <string>
+#include "line/type.h"
+
using namespace std; // TODO: remove
namespace ov4
diff --git a/include/line/type.h b/include/line/type.h
new file mode 100644
index 0000000..b85a81c
--- /dev/null
+++ b/include/line/type.h
@@ -0,0 +1,18 @@
+#pragma once
+
+namespace ov4
+{
+
+struct T_position
+{
+ int row;
+ int col;
+};
+
+enum class T_cursor_movement_direction
+{
+ LEFT,
+ RIGHT,
+};
+
+} \ No newline at end of file
diff --git a/src/line/cursor.cc b/src/line/cursor.cc
index 1489d61..05311ad 100644
--- a/src/line/cursor.cc
+++ b/src/line/cursor.cc
@@ -14,38 +14,6 @@ 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)
{
// save current cursor position
@@ -54,12 +22,12 @@ void print_override(const std::string &s)
// move to the "start" of the zone
// should skip prompt zone
print("\r"); fflush(stdout);
- print("\x1b[{}C", line_begin_pos-1); fflush(stdout);
+ print("\x1b[{}C", prompt_pos-1); fflush(stdout);
// erase rest of the line
print("\x1b[K"); fflush(stdout);
- print("{}", s.substr(line_buffer_index_begin, line_buffer_index_end-line_buffer_index_begin+1)); fflush(stdout);
+ print("{}", s.substr(buffer_index_display_begin, buffer_index_display_end-buffer_index_display_begin+1)); fflush(stdout);
//line_end_pos = s.size()+1;
// restore cursor position
@@ -85,17 +53,17 @@ void move_cursor(char c)
// {
// // print("\x1b[C");
// // fflush(stdout);
- // // line_buffer_index_cursor++;
+ // // buffer_index_cursor++;
// move_cursor(T_cursor_movement_direction::RIGHT);
// }
move_cursor(T_cursor_movement_direction::RIGHT);
break;
case 'D':
- // if (get_cursor_real_pos() > line_begin_pos)
+ // if (get_cursor_real_pos() > prompt_pos)
// {
// // print("\x1b[D");
// // fflush(stdout);
- // // line_buffer_index_cursor--;
+ // // buffer_index_cursor--;
// move_cursor(T_cursor_movement_direction::LEFT);
// }
move_cursor(T_cursor_movement_direction::LEFT);
@@ -110,14 +78,14 @@ void cursor_input_char(char c)
// normal input
if (c != '\b')
{
- line_buffer.insert(line_buffer_index_cursor, 1, c);
+ line_buffer.insert(buffer_index_cursor, 1, c);
// print("\x1b[C"); fflush(stdout); // move cursor to the right
- // line_buffer_index_cursor++;
+ // buffer_index_cursor++;
update_line_end_pos();
- int end_possible_buffer_index = window_size_col-line_begin_pos-1;
- line_buffer_index_end =
- (end_possible_buffer_index > (line_buffer_index_end)+1)
- ? (line_buffer_index_end)+1
+ int end_possible_buffer_index = window_size_col-prompt_pos-1;
+ buffer_index_display_end =
+ (end_possible_buffer_index > (buffer_index_display_end)+1)
+ ? (buffer_index_display_end)+1
: end_possible_buffer_index;
move_cursor(T_cursor_movement_direction::RIGHT);
@@ -125,19 +93,16 @@ void cursor_input_char(char c)
}
// BACKSPACE
- line_buffer.erase(line_buffer_index_cursor, 1);
+ line_buffer.erase(buffer_index_cursor, 1);
// print("\x1b[D"); fflush(stdout); // move cursor to the left
- // line_buffer_index_cursor--;
+ // buffer_index_cursor--;
update_line_end_pos();
// TODO: backspace page
move_cursor(T_cursor_movement_direction::LEFT);
}
-int get_cursor_real_pos()
-{
- return line_begin_pos + line_buffer_index_cursor;
-}
+
void update_window_size()
{
@@ -151,7 +116,7 @@ void update_window_size()
void update_line_end_pos()
{
- line_end_pos = line_begin_pos + line_buffer.size();
+ line_end_pos = prompt_pos + line_buffer.size();
line_end_pos = (line_end_pos > window_size_col) ? window_size_col : line_end_pos;
}
@@ -161,33 +126,33 @@ void move_cursor(T_cursor_movement_direction d)
{
case T_cursor_movement_direction::LEFT:
// if cursor is not at left margin
- if (line_begin_pos < (line_buffer_index_cursor+1))
+ if (prompt_pos < (buffer_index_cursor+1))
{
- line_buffer_index_cursor--;
+ buffer_index_cursor--;
print("\x1b[D"); fflush(stdout); // move cursor to the left
}
// margin
else
{
- line_buffer_index_cursor--;
- line_buffer_index_begin--;
- line_buffer_index_end--;
+ buffer_index_cursor--;
+ buffer_index_display_begin--;
+ buffer_index_display_end--;
}
break;
case T_cursor_movement_direction::RIGHT:
// if cursor is not at right margin
- if (line_end_pos > (line_buffer_index_cursor+1))
+ if (line_end_pos > (buffer_index_cursor+1))
{
- line_buffer_index_cursor++;
+ buffer_index_cursor++;
print("\x1b[C"); fflush(stdout); // move cursor to the right
}
// margin
else
{
- line_buffer_index_cursor++;
- line_buffer_index_begin++;
- line_buffer_index_end++;
+ buffer_index_cursor++;
+ buffer_index_display_begin++;
+ buffer_index_display_end++;
}
break;
diff --git a/src/line/getter.cc b/src/line/getter.cc
new file mode 100644
index 0000000..2ef9723
--- /dev/null
+++ b/src/line/getter.cc
@@ -0,0 +1,69 @@
+#include "line/getter.h"
+
+#include <cstdio>
+#include <string>
+#include <termios.h>
+#include <sys/ioctl.h>
+#include <iostream>
+#include <unistd.h>
+#include <cmath>
+
+#include "line/cursor.h"
+
+
+using namespace std;
+
+namespace ov4
+{
+
+int get_terminal_pos_cursor()
+{
+ return prompt_pos + buffer_index_cursor;
+}
+
+int get_buffer_index_display_begin()
+{
+ return _buffer_index_display_begin;
+}
+
+int get_buffer_index_display_end()
+{
+ int max_possible_end = get_buffer_index_display_begin() + line_buffer.size() - 1;
+ int window_possible_end = get_buffer_index_display_begin() + (window_size_col - prompt_pos) -1;
+ return min(max_possible_end, window_possible_end);
+}
+
+
+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;
+}
+
+} \ No newline at end of file
diff --git a/src/line/mode.cc b/src/line/mode.cc
index 7a91528..e3f071c 100644
--- a/src/line/mode.cc
+++ b/src/line/mode.cc
@@ -71,7 +71,7 @@ string readline()
update_window_size();
T_position pos = get_cursor_position();
- line_begin_pos = pos.col;
+ prompt_pos = pos.col;
char c;
while (read(STDIN_FILENO, &c, 1) == 1)
{