mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:37:36 +00:00
Shell: Refactor append/insert procedure
This patch just factors out the procedure of adding characters at the cursor position. It makes tab completion code much nicer.
This commit is contained in:
parent
bb311b970f
commit
244e525c73
2 changed files with 47 additions and 21 deletions
|
@ -29,12 +29,48 @@ void LineEditor::clear_line()
|
||||||
m_cursor = 0;
|
m_cursor = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineEditor::append(const String& string)
|
void LineEditor::insert(const String& string)
|
||||||
{
|
{
|
||||||
m_buffer.append(string.characters(), (int)string.length());
|
|
||||||
fputs(string.characters(), stdout);
|
fputs(string.characters(), stdout);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
m_cursor = (size_t)m_buffer.size();
|
|
||||||
|
if (m_cursor == (size_t)m_buffer.size()) {
|
||||||
|
m_buffer.append(string.characters(), (int)string.length());
|
||||||
|
m_cursor = (size_t)m_buffer.size();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
vt_save_cursor();
|
||||||
|
vt_clear_to_end_of_line();
|
||||||
|
for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i)
|
||||||
|
fputc(m_buffer[(int)i], stdout);
|
||||||
|
vt_restore_cursor();
|
||||||
|
|
||||||
|
m_buffer.ensure_capacity(m_buffer.size() + (int)string.length());
|
||||||
|
for (size_t i = 0; i < string.length(); ++i)
|
||||||
|
m_buffer.insert((int)m_cursor + (int)i, string[i]);
|
||||||
|
m_cursor += string.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineEditor::insert(const char ch)
|
||||||
|
{
|
||||||
|
putchar(ch);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
if (m_cursor == (size_t)m_buffer.size()) {
|
||||||
|
m_buffer.append(ch);
|
||||||
|
m_cursor = (size_t)m_buffer.size();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
vt_save_cursor();
|
||||||
|
vt_clear_to_end_of_line();
|
||||||
|
for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i)
|
||||||
|
fputc(m_buffer[(int)i], stdout);
|
||||||
|
vt_restore_cursor();
|
||||||
|
|
||||||
|
m_buffer.insert((int)m_cursor, ch);
|
||||||
|
++m_cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineEditor::cache_path()
|
void LineEditor::cache_path()
|
||||||
|
@ -94,7 +130,7 @@ void LineEditor::tab_complete_first_token()
|
||||||
|
|
||||||
if (token.length() == completion.length())
|
if (token.length() == completion.length())
|
||||||
return;
|
return;
|
||||||
append(completion.substring(token.length(), completion.length() - token.length()).characters());
|
insert(completion.substring(token.length(), completion.length() - token.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
String LineEditor::get_line(const String& prompt)
|
String LineEditor::get_line(const String& prompt)
|
||||||
|
@ -169,7 +205,7 @@ String LineEditor::get_line(const String& prompt)
|
||||||
--m_history_cursor;
|
--m_history_cursor;
|
||||||
clear_line();
|
clear_line();
|
||||||
if (m_history_cursor < m_history.size())
|
if (m_history_cursor < m_history.size())
|
||||||
append(m_history[m_history_cursor]);
|
insert(m_history[m_history_cursor]);
|
||||||
m_state = InputState::Free;
|
m_state = InputState::Free;
|
||||||
continue;
|
continue;
|
||||||
case 'B': // down
|
case 'B': // down
|
||||||
|
@ -177,7 +213,7 @@ String LineEditor::get_line(const String& prompt)
|
||||||
++m_history_cursor;
|
++m_history_cursor;
|
||||||
clear_line();
|
clear_line();
|
||||||
if (m_history_cursor < m_history.size())
|
if (m_history_cursor < m_history.size())
|
||||||
append(m_history[m_history_cursor]);
|
insert(m_history[m_history_cursor]);
|
||||||
m_state = InputState::Free;
|
m_state = InputState::Free;
|
||||||
continue;
|
continue;
|
||||||
case 'D': // left
|
case 'D': // left
|
||||||
|
@ -323,26 +359,15 @@ String LineEditor::get_line(const String& prompt)
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
putchar(ch);
|
|
||||||
fflush(stdout);
|
|
||||||
if (ch == '\n') {
|
if (ch == '\n') {
|
||||||
|
putchar('\n');
|
||||||
|
fflush(stdout);
|
||||||
auto string = String::copy(m_buffer);
|
auto string = String::copy(m_buffer);
|
||||||
m_buffer.clear();
|
m_buffer.clear();
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_cursor == (size_t)m_buffer.size()) {
|
insert(ch);
|
||||||
m_buffer.append(ch);
|
|
||||||
++m_cursor;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
vt_save_cursor();
|
|
||||||
vt_clear_to_end_of_line();
|
|
||||||
for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i)
|
|
||||||
fputc(m_buffer[(int)i], stdout);
|
|
||||||
vt_restore_cursor();
|
|
||||||
m_buffer.insert((int)m_cursor, move(ch));
|
|
||||||
++m_cursor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void clear_line();
|
void clear_line();
|
||||||
void append(const String&);
|
void insert(const String&);
|
||||||
|
void insert(const char);
|
||||||
void cut_mismatching_chars(String& completion, const String& program, size_t token_length);
|
void cut_mismatching_chars(String& completion, const String& program, size_t token_length);
|
||||||
void tab_complete_first_token();
|
void tab_complete_first_token();
|
||||||
void vt_save_cursor();
|
void vt_save_cursor();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue