aboutsummaryrefslogtreecommitdiff
path: root/src/line
diff options
context:
space:
mode:
Diffstat (limited to 'src/line')
-rw-r--r--src/line/cursor.cc24
-rw-r--r--src/line/mode.cc2
2 files changed, 24 insertions, 2 deletions
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;