summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam McPherson <willmcpherson2@gmail.com>2019-12-10 21:27:31 +1100
committerAndreas Kling <awesomekling@gmail.com>2019-12-11 12:10:52 +0100
commit244e525c73a545e9532c9ae148d8ec180b37c494 (patch)
tree857995551c7f5ae98eaa67194a5e7fed9025cad2
parentbb311b970f4d5c7b7e1c40d45e9064790bee6c2c (diff)
downloadserenity-244e525c73a545e9532c9ae148d8ec180b37c494.zip
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.
-rw-r--r--Shell/LineEditor.cpp65
-rw-r--r--Shell/LineEditor.h3
2 files changed, 47 insertions, 21 deletions
diff --git a/Shell/LineEditor.cpp b/Shell/LineEditor.cpp
index 6f1267f04b..25cbd65bab 100644
--- a/Shell/LineEditor.cpp
+++ b/Shell/LineEditor.cpp
@@ -29,12 +29,48 @@ void LineEditor::clear_line()
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);
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()
@@ -94,7 +130,7 @@ void LineEditor::tab_complete_first_token()
if (token.length() == completion.length())
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)
@@ -169,7 +205,7 @@ String LineEditor::get_line(const String& prompt)
--m_history_cursor;
clear_line();
if (m_history_cursor < m_history.size())
- append(m_history[m_history_cursor]);
+ insert(m_history[m_history_cursor]);
m_state = InputState::Free;
continue;
case 'B': // down
@@ -177,7 +213,7 @@ String LineEditor::get_line(const String& prompt)
++m_history_cursor;
clear_line();
if (m_history_cursor < m_history.size())
- append(m_history[m_history_cursor]);
+ insert(m_history[m_history_cursor]);
m_state = InputState::Free;
continue;
case 'D': // left
@@ -323,26 +359,15 @@ String LineEditor::get_line(const String& prompt)
}
continue;
}
- putchar(ch);
- fflush(stdout);
if (ch == '\n') {
+ putchar('\n');
+ fflush(stdout);
auto string = String::copy(m_buffer);
m_buffer.clear();
return string;
}
- if (m_cursor == (size_t)m_buffer.size()) {
- 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;
+ insert(ch);
}
}
}
diff --git a/Shell/LineEditor.h b/Shell/LineEditor.h
index 6f4f4708fa..7af21e4d78 100644
--- a/Shell/LineEditor.h
+++ b/Shell/LineEditor.h
@@ -21,7 +21,8 @@ public:
private:
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 tab_complete_first_token();
void vt_save_cursor();