@@ -37,10 +37,14 @@ Editor::Editor()
37
37
{
38
38
m_pending_chars = ByteBuffer::create_uninitialized (0 );
39
39
struct winsize ws;
40
- if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 )
40
+ if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 ) {
41
41
m_num_columns = 80 ;
42
- else
42
+ m_num_lines = 25 ;
43
+ } else {
43
44
m_num_columns = ws.ws_col ;
45
+ m_num_lines = ws.ws_row ;
46
+ dbg () << m_num_lines;
47
+ }
44
48
}
45
49
46
50
Editor::~Editor ()
@@ -68,18 +72,8 @@ void Editor::clear_line()
68
72
69
73
void Editor::insert (const String& string)
70
74
{
71
- m_pending_chars.append (string.characters (), string.length ());
72
- if (m_cursor == m_buffer.size ()) {
73
- m_buffer.append (string.characters (), string.length ());
74
- m_cursor = m_buffer.size ();
75
- return ;
76
- }
77
-
78
- m_buffer.ensure_capacity (m_buffer.size () + string.length ());
79
- m_chars_inserted_in_the_middle += string.length ();
80
- for (size_t i = 0 ; i < string.length (); ++i)
81
- m_buffer.insert (m_cursor + i, string[i]);
82
- m_cursor += string.length ();
75
+ for (auto ch : string)
76
+ insert (ch);
83
77
}
84
78
85
79
void Editor::insert (const char ch)
@@ -302,33 +296,43 @@ String Editor::get_line(const String& prompt)
302
296
longest_suggestion_length = max (longest_suggestion_length, suggestion.length ());
303
297
304
298
size_t num_printed = 0 ;
299
+ size_t lines_used { 1 };
305
300
putchar (' \n ' );
301
+ // FIXME: what if we use more lines than the terminal has?
302
+ // this would put the actual prompt out of view
306
303
for (auto & suggestion : suggestions) {
307
304
size_t next_column = num_printed + suggestion.length () + longest_suggestion_length + 2 ;
308
305
309
306
if (next_column > m_num_columns) {
307
+ ++lines_used;
310
308
putchar (' \n ' );
311
309
num_printed = 0 ;
312
310
}
313
311
314
312
num_printed += fprintf (stderr, " %-*s" , static_cast <int >(longest_suggestion_length) + 2 , suggestion.characters ());
313
+ m_lines_used_for_last_suggestions = lines_used;
315
314
}
316
315
317
- StringBuilder builder;
318
- builder.append (' \n ' );
319
- builder.append (prompt);
320
- builder.append (m_buffer.data (), m_cursor);
321
- builder.append (m_buffer.data () + m_cursor, m_buffer.size () - m_cursor);
322
- fputs (builder.to_string ().characters (), stdout);
323
- fflush (stdout);
324
-
325
- m_cursor = m_buffer.size ();
316
+ // adjust for the case that we scroll up after writing the suggestions
317
+ if (m_origin_x + lines_used >= m_num_lines) {
318
+ m_origin_x = m_num_lines - lines_used;
319
+ }
320
+ reposition_cursor ();
326
321
}
327
322
328
323
suggestions.clear_with_capacity ();
329
324
continue ;
330
325
}
331
326
327
+ if (m_times_tab_pressed) {
328
+ // we probably have some suggestions drawn
329
+ // let's clean them up
330
+ if (m_lines_used_for_last_suggestions) {
331
+ vt_clear_lines (1 , m_lines_used_for_last_suggestions);
332
+ m_refresh_needed = true ;
333
+ m_lines_used_for_last_suggestions = 0 ;
334
+ }
335
+ }
332
336
m_times_tab_pressed = 0 ; // Safe to say if we get here, the user didn't press TAB
333
337
334
338
auto do_backspace = [&] {
@@ -410,7 +414,7 @@ void Editor::refresh_display()
410
414
auto current_line = cursor_line ();
411
415
412
416
vt_clear_lines (current_line - 1 , num_lines () - current_line);
413
- vt_move_relative (-num_lines () + 1 , -offset_in_line () - m_old_prompt_length + m_chars_inserted_in_the_middle);
417
+ vt_move_relative (-num_lines () + 1 , -offset_in_line () - m_old_prompt_length - m_pending_chars. size () + m_chars_inserted_in_the_middle);
414
418
};
415
419
auto has_cleaned_up = false ;
416
420
// someone changed the window size, figure it out
@@ -419,10 +423,13 @@ void Editor::refresh_display()
419
423
auto previous_num_columns = m_num_columns;
420
424
421
425
struct winsize ws;
422
- if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 )
426
+ if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 ) {
423
427
m_num_columns = 80 ;
424
- else
428
+ m_num_lines = 25 ;
429
+ } else {
425
430
m_num_columns = ws.ws_col ;
431
+ m_num_lines = ws.ws_row ;
432
+ }
426
433
427
434
if (previous_num_columns != m_num_columns) {
428
435
// we need to cleanup and redo everything
0 commit comments