Skip to content

Commit 173c656

Browse files
alimpfardawesomekling
authored andcommitted
LibLine: Properly show and cleanup suggestions
Prior to this, we would display them and never clean then up.
1 parent 5022999 commit 173c656

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

Libraries/LibLine/Editor.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ Editor::Editor()
3737
{
3838
m_pending_chars = ByteBuffer::create_uninitialized(0);
3939
struct winsize ws;
40-
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
40+
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
4141
m_num_columns = 80;
42-
else
42+
m_num_lines = 25;
43+
} else {
4344
m_num_columns = ws.ws_col;
45+
m_num_lines = ws.ws_row;
46+
dbg() << m_num_lines;
47+
}
4448
}
4549

4650
Editor::~Editor()
@@ -68,18 +72,8 @@ void Editor::clear_line()
6872

6973
void Editor::insert(const String& string)
7074
{
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);
8377
}
8478

8579
void Editor::insert(const char ch)
@@ -302,33 +296,43 @@ String Editor::get_line(const String& prompt)
302296
longest_suggestion_length = max(longest_suggestion_length, suggestion.length());
303297

304298
size_t num_printed = 0;
299+
size_t lines_used { 1 };
305300
putchar('\n');
301+
// FIXME: what if we use more lines than the terminal has?
302+
// this would put the actual prompt out of view
306303
for (auto& suggestion : suggestions) {
307304
size_t next_column = num_printed + suggestion.length() + longest_suggestion_length + 2;
308305

309306
if (next_column > m_num_columns) {
307+
++lines_used;
310308
putchar('\n');
311309
num_printed = 0;
312310
}
313311

314312
num_printed += fprintf(stderr, "%-*s", static_cast<int>(longest_suggestion_length) + 2, suggestion.characters());
313+
m_lines_used_for_last_suggestions = lines_used;
315314
}
316315

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();
326321
}
327322

328323
suggestions.clear_with_capacity();
329324
continue;
330325
}
331326

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+
}
332336
m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB
333337

334338
auto do_backspace = [&] {
@@ -410,7 +414,7 @@ void Editor::refresh_display()
410414
auto current_line = cursor_line();
411415

412416
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);
414418
};
415419
auto has_cleaned_up = false;
416420
// someone changed the window size, figure it out
@@ -419,10 +423,13 @@ void Editor::refresh_display()
419423
auto previous_num_columns = m_num_columns;
420424

421425
struct winsize ws;
422-
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
426+
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
423427
m_num_columns = 80;
424-
else
428+
m_num_lines = 25;
429+
} else {
425430
m_num_columns = ws.ws_col;
431+
m_num_lines = ws.ws_row;
432+
}
426433

427434
if (previous_num_columns != m_num_columns) {
428435
// we need to cleanup and redo everything

Libraries/LibLine/Editor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ class Editor {
177177
size_t m_chars_inserted_in_the_middle { 0 };
178178
size_t m_times_tab_pressed { 0 };
179179
size_t m_num_columns { 0 };
180+
size_t m_num_lines { 1 };
180181
size_t m_cached_prompt_length { 0 };
181182
size_t m_old_prompt_length { 0 };
182183
size_t m_cached_buffer_size { 0 };
184+
size_t m_lines_used_for_last_suggestions { 0 };
183185
bool m_cached_prompt_valid { false };
184186

185187
// exact position before our prompt in the terminal

0 commit comments

Comments
 (0)