2019-05-06 23:39:10 +00:00
|
|
|
#include "LineEditor.h"
|
|
|
|
#include "GlobalState.h"
|
2019-06-07 09:49:21 +00:00
|
|
|
#include <ctype.h>
|
2019-05-06 23:39:10 +00:00
|
|
|
#include <stdio.h>
|
2019-12-13 08:36:15 +00:00
|
|
|
#include <sys/ioctl.h>
|
2019-05-06 23:39:10 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
LineEditor::LineEditor()
|
|
|
|
{
|
2019-12-13 08:36:15 +00:00
|
|
|
struct winsize ws;
|
|
|
|
int rc = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
m_num_columns = ws.ws_col;
|
2019-05-06 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LineEditor::~LineEditor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEditor::add_to_history(const String& line)
|
|
|
|
{
|
|
|
|
if ((m_history.size() + 1) > m_history_capacity)
|
|
|
|
m_history.take_first();
|
|
|
|
m_history.append(line);
|
|
|
|
}
|
|
|
|
|
2019-05-07 00:50:15 +00:00
|
|
|
void LineEditor::clear_line()
|
|
|
|
{
|
2019-12-09 16:45:40 +00:00
|
|
|
for (size_t i = 0; i < m_cursor; ++i)
|
2019-05-07 00:50:15 +00:00
|
|
|
fputc(0x8, stdout);
|
2019-05-07 03:03:21 +00:00
|
|
|
fputs("\033[K", stdout);
|
2019-05-07 00:50:15 +00:00
|
|
|
fflush(stdout);
|
|
|
|
m_buffer.clear();
|
2019-05-07 02:17:05 +00:00
|
|
|
m_cursor = 0;
|
2019-05-07 00:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 10:27:31 +00:00
|
|
|
void LineEditor::insert(const String& string)
|
2019-05-07 00:50:15 +00:00
|
|
|
{
|
|
|
|
fputs(string.characters(), stdout);
|
|
|
|
fflush(stdout);
|
2019-12-10 10:27:31 +00:00
|
|
|
|
|
|
|
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;
|
2019-05-07 00:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 06:36:46 +00:00
|
|
|
void LineEditor::cache_path()
|
2019-09-15 13:12:43 +00:00
|
|
|
{
|
2019-12-03 06:36:46 +00:00
|
|
|
if (!m_path.is_empty())
|
|
|
|
m_path.clear_with_capacity();
|
2019-09-15 13:12:43 +00:00
|
|
|
|
|
|
|
String path = getenv("PATH");
|
|
|
|
if (path.is_empty())
|
|
|
|
return;
|
|
|
|
|
2019-12-03 06:36:46 +00:00
|
|
|
auto directories = path.split(':');
|
2019-09-15 13:12:43 +00:00
|
|
|
for (const auto& directory : directories) {
|
|
|
|
CDirIterator programs(directory.characters(), CDirIterator::SkipDots);
|
|
|
|
while (programs.has_next()) {
|
2019-12-03 06:36:46 +00:00
|
|
|
auto program = programs.next_path();
|
2019-12-10 07:02:43 +00:00
|
|
|
String program_path = String::format("%s/%s", directory.characters(), program.characters());
|
2019-12-03 06:36:46 +00:00
|
|
|
struct stat program_status;
|
2019-12-10 07:02:43 +00:00
|
|
|
int stat_error = stat(program_path.characters(), &program_status);
|
|
|
|
if (!stat_error && (program_status.st_mode & S_IXUSR))
|
|
|
|
m_path.append(program.characters());
|
2019-09-15 13:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 06:36:46 +00:00
|
|
|
quick_sort(m_path.begin(), m_path.end(), AK::is_less_than<String>);
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:14:45 +00:00
|
|
|
void LineEditor::cut_mismatching_chars(String& completion, const String& other, size_t start_compare)
|
2019-12-03 06:36:46 +00:00
|
|
|
{
|
2019-12-10 12:14:45 +00:00
|
|
|
size_t i = start_compare;
|
|
|
|
while (i < completion.length() && i < other.length() && completion[i] == other[i])
|
2019-12-03 06:36:46 +00:00
|
|
|
++i;
|
|
|
|
completion = completion.substring(0, i);
|
|
|
|
}
|
|
|
|
|
2019-12-13 08:36:15 +00:00
|
|
|
// Function returns Vector<String> as assignment is made from return value at callsite
|
|
|
|
// (instead of StringView)
|
|
|
|
Vector<String> LineEditor::tab_complete_first_token(const String& token)
|
2019-12-03 06:36:46 +00:00
|
|
|
{
|
|
|
|
auto match = binary_search(m_path.data(), m_path.size(), token, [](const String& token, const String& program) -> int {
|
|
|
|
return strncmp(token.characters(), program.characters(), token.length());
|
|
|
|
});
|
|
|
|
if (!match)
|
2019-12-13 08:36:15 +00:00
|
|
|
return Vector<String>();
|
2019-09-15 13:12:43 +00:00
|
|
|
|
2019-12-03 06:36:46 +00:00
|
|
|
String completion = *match;
|
2019-12-13 08:36:15 +00:00
|
|
|
Vector<String> suggestions;
|
2019-12-03 06:36:46 +00:00
|
|
|
|
|
|
|
// Now that we have a program name starting with our token, we look at
|
|
|
|
// other program names starting with our token and cut off any mismatching
|
|
|
|
// characters.
|
|
|
|
|
2019-12-10 11:25:54 +00:00
|
|
|
bool seen_others = false;
|
2019-12-03 06:36:46 +00:00
|
|
|
int index = match - m_path.data();
|
2019-12-10 11:25:54 +00:00
|
|
|
for (int i = index - 1; i >= 0 && m_path[i].starts_with(token); --i) {
|
2019-12-13 08:36:15 +00:00
|
|
|
suggestions.append(m_path[i]);
|
2019-12-03 06:36:46 +00:00
|
|
|
cut_mismatching_chars(completion, m_path[i], token.length());
|
2019-12-10 11:25:54 +00:00
|
|
|
seen_others = true;
|
|
|
|
}
|
|
|
|
for (int i = index + 1; i < m_path.size() && m_path[i].starts_with(token); ++i) {
|
2019-12-03 06:36:46 +00:00
|
|
|
cut_mismatching_chars(completion, m_path[i], token.length());
|
2019-12-13 08:36:15 +00:00
|
|
|
suggestions.append(m_path[i]);
|
2019-12-10 11:25:54 +00:00
|
|
|
seen_others = true;
|
|
|
|
}
|
2019-12-13 08:36:15 +00:00
|
|
|
suggestions.append(m_path[index]);
|
2019-12-03 06:36:46 +00:00
|
|
|
|
2019-12-10 11:25:54 +00:00
|
|
|
// If we have characters to add, add them.
|
|
|
|
if (completion.length() > token.length())
|
|
|
|
insert(completion.substring(token.length(), completion.length() - token.length()));
|
|
|
|
// If we have a single match, we add a space, unless we already have one.
|
|
|
|
if (!seen_others && (m_cursor == (size_t)m_buffer.size() || m_buffer[(int)m_cursor] != ' '))
|
|
|
|
insert(' ');
|
2019-12-13 08:36:15 +00:00
|
|
|
|
|
|
|
return suggestions;
|
2019-09-15 13:12:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 08:36:15 +00:00
|
|
|
Vector<String> LineEditor::tab_complete_other_token(String& token)
|
2019-12-10 12:14:45 +00:00
|
|
|
{
|
|
|
|
String path;
|
2019-12-13 08:36:15 +00:00
|
|
|
Vector<String> suggestions;
|
2019-12-10 12:14:45 +00:00
|
|
|
|
|
|
|
int last_slash = (int)token.length() - 1;
|
|
|
|
while (last_slash >= 0 && token[last_slash] != '/')
|
|
|
|
--last_slash;
|
|
|
|
|
|
|
|
if (last_slash >= 0) {
|
|
|
|
// Split on the last slash. We'll use the first part as the directory
|
|
|
|
// to search and the second part as the token to complete.
|
|
|
|
path = token.substring(0, (size_t)last_slash + 1);
|
|
|
|
if (path[0] != '/')
|
|
|
|
path = String::format("%s/%s", g.cwd.characters(), path.characters());
|
|
|
|
path = canonicalized_path(path);
|
|
|
|
token = token.substring((size_t)last_slash + 1, token.length() - (size_t)last_slash - 1);
|
|
|
|
} else {
|
|
|
|
// We have no slashes, so the directory to search is the current
|
|
|
|
// directory and the token to complete is just the original token.
|
|
|
|
path = g.cwd;
|
|
|
|
}
|
|
|
|
|
2019-12-13 08:36:15 +00:00
|
|
|
// This is a bit naughty, but necessary without reordering the loop
|
|
|
|
// below. The loop terminates early, meaning that
|
|
|
|
// the suggestions list is incomplete.
|
|
|
|
// We only do this if the token is empty though.
|
|
|
|
if (token.is_empty()) {
|
|
|
|
CDirIterator suggested_files(path, CDirIterator::SkipDots);
|
|
|
|
while (suggested_files.has_next()) {
|
|
|
|
suggestions.append(suggested_files.next_path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:14:45 +00:00
|
|
|
String completion;
|
|
|
|
|
|
|
|
bool seen_others = false;
|
|
|
|
CDirIterator files(path, CDirIterator::SkipDots);
|
|
|
|
while (files.has_next()) {
|
|
|
|
auto file = files.next_path();
|
|
|
|
if (file.starts_with(token)) {
|
2019-12-13 08:36:15 +00:00
|
|
|
if (!token.is_empty())
|
|
|
|
suggestions.append(file);
|
2019-12-10 12:14:45 +00:00
|
|
|
if (completion.is_empty()) {
|
|
|
|
completion = file; // Will only be set once.
|
|
|
|
} else {
|
|
|
|
cut_mismatching_chars(completion, file, token.length());
|
|
|
|
if (completion.is_empty()) // We cut everything off!
|
2019-12-13 08:36:15 +00:00
|
|
|
return suggestions;
|
2019-12-10 12:14:45 +00:00
|
|
|
seen_others = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (completion.is_empty())
|
2019-12-13 08:36:15 +00:00
|
|
|
return suggestions;
|
2019-12-10 12:14:45 +00:00
|
|
|
|
|
|
|
// If we have characters to add, add them.
|
|
|
|
if (completion.length() > token.length())
|
|
|
|
insert(completion.substring(token.length(), completion.length() - token.length()));
|
|
|
|
// If we have a single match and it's a directory, we add a slash. If it's
|
|
|
|
// a regular file, we add a space, unless we already have one.
|
|
|
|
if (!seen_others) {
|
|
|
|
String file_path = String::format("%s/%s", path.characters(), completion.characters());
|
|
|
|
struct stat program_status;
|
|
|
|
int stat_error = stat(file_path.characters(), &program_status);
|
|
|
|
if (!stat_error) {
|
|
|
|
if (S_ISDIR(program_status.st_mode))
|
|
|
|
insert('/');
|
|
|
|
else if (m_cursor == (size_t)m_buffer.size() || m_buffer[(int)m_cursor] != ' ')
|
|
|
|
insert(' ');
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 08:36:15 +00:00
|
|
|
|
|
|
|
return {}; // Return an empty vector
|
2019-12-10 12:14:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 18:01:46 +00:00
|
|
|
String LineEditor::get_line(const String& prompt)
|
2019-05-06 23:39:10 +00:00
|
|
|
{
|
2019-07-19 18:01:46 +00:00
|
|
|
fputs(prompt.characters(), stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2019-05-07 00:50:15 +00:00
|
|
|
m_history_cursor = m_history.size();
|
2019-05-07 02:17:05 +00:00
|
|
|
m_cursor = 0;
|
2019-05-06 23:39:10 +00:00
|
|
|
for (;;) {
|
|
|
|
char keybuf[16];
|
|
|
|
ssize_t nread = read(0, keybuf, sizeof(keybuf));
|
|
|
|
// FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
|
|
|
|
if (nread == 0)
|
|
|
|
exit(0);
|
|
|
|
if (nread < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
if (g.was_interrupted) {
|
2019-07-08 17:04:13 +00:00
|
|
|
g.was_interrupted = false;
|
2019-05-06 23:39:10 +00:00
|
|
|
if (!m_buffer.is_empty())
|
|
|
|
printf("^C");
|
|
|
|
}
|
2019-07-08 17:04:13 +00:00
|
|
|
if (g.was_resized) {
|
|
|
|
g.was_resized = false;
|
|
|
|
printf("\033[2K\r");
|
|
|
|
m_buffer.clear();
|
2019-12-13 08:36:15 +00:00
|
|
|
|
|
|
|
struct winsize ws;
|
|
|
|
int rc = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
m_num_columns = ws.ws_col;
|
|
|
|
|
2019-07-08 17:04:13 +00:00
|
|
|
return String::empty();
|
|
|
|
}
|
2019-05-06 23:39:10 +00:00
|
|
|
m_buffer.clear();
|
|
|
|
putchar('\n');
|
|
|
|
return String::empty();
|
|
|
|
}
|
2019-05-07 00:50:15 +00:00
|
|
|
perror("read failed");
|
|
|
|
// FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
|
|
|
|
exit(2);
|
2019-05-06 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 01:57:10 +00:00
|
|
|
auto do_delete = [&] {
|
2019-12-09 16:45:40 +00:00
|
|
|
if (m_cursor == (size_t)m_buffer.size()) {
|
2019-08-18 01:57:10 +00:00
|
|
|
fputc('\a', stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-09 16:45:40 +00:00
|
|
|
m_buffer.remove((int)m_cursor - 1);
|
2019-08-18 01:57:10 +00:00
|
|
|
fputs("\033[3~", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
vt_save_cursor();
|
|
|
|
vt_clear_to_end_of_line();
|
2019-12-09 16:45:40 +00:00
|
|
|
for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i)
|
2019-08-18 01:57:10 +00:00
|
|
|
fputc(m_buffer[i], stdout);
|
|
|
|
vt_restore_cursor();
|
|
|
|
};
|
|
|
|
|
2019-05-06 23:39:10 +00:00
|
|
|
for (ssize_t i = 0; i < nread; ++i) {
|
|
|
|
char ch = keybuf[i];
|
|
|
|
if (ch == 0)
|
|
|
|
continue;
|
2019-05-07 00:50:15 +00:00
|
|
|
|
|
|
|
switch (m_state) {
|
|
|
|
case InputState::ExpectBracket:
|
|
|
|
if (ch == '[') {
|
|
|
|
m_state = InputState::ExpectFinal;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
m_state = InputState::Free;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InputState::ExpectFinal:
|
|
|
|
switch (ch) {
|
|
|
|
case 'A': // up
|
|
|
|
if (m_history_cursor > 0)
|
|
|
|
--m_history_cursor;
|
|
|
|
clear_line();
|
|
|
|
if (m_history_cursor < m_history.size())
|
2019-12-10 10:27:31 +00:00
|
|
|
insert(m_history[m_history_cursor]);
|
2019-05-07 00:50:15 +00:00
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
|
|
|
case 'B': // down
|
|
|
|
if (m_history_cursor < m_history.size())
|
|
|
|
++m_history_cursor;
|
|
|
|
clear_line();
|
|
|
|
if (m_history_cursor < m_history.size())
|
2019-12-10 10:27:31 +00:00
|
|
|
insert(m_history[m_history_cursor]);
|
2019-05-07 00:50:15 +00:00
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
|
|
|
case 'D': // left
|
2019-05-07 02:17:05 +00:00
|
|
|
if (m_cursor > 0) {
|
|
|
|
--m_cursor;
|
|
|
|
fputs("\033[D", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2019-05-07 00:50:15 +00:00
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
|
|
|
case 'C': // right
|
2019-12-09 16:45:40 +00:00
|
|
|
if (m_cursor < (size_t)m_buffer.size()) {
|
2019-05-07 02:17:05 +00:00
|
|
|
++m_cursor;
|
|
|
|
fputs("\033[C", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2019-05-07 00:50:15 +00:00
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
2019-05-07 03:03:21 +00:00
|
|
|
case 'H':
|
|
|
|
if (m_cursor > 0) {
|
2019-12-09 16:45:40 +00:00
|
|
|
fprintf(stdout, "\033[%zuD", m_cursor);
|
2019-05-07 03:03:21 +00:00
|
|
|
fflush(stdout);
|
|
|
|
m_cursor = 0;
|
|
|
|
}
|
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
|
|
|
case 'F':
|
2019-12-09 16:45:40 +00:00
|
|
|
if (m_cursor < (size_t)m_buffer.size()) {
|
|
|
|
fprintf(stdout, "\033[%zuC", (size_t)m_buffer.size() - m_cursor);
|
2019-05-07 03:03:21 +00:00
|
|
|
fflush(stdout);
|
2019-12-09 16:45:40 +00:00
|
|
|
m_cursor = (size_t)m_buffer.size();
|
2019-05-07 03:03:21 +00:00
|
|
|
}
|
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
2019-08-18 01:57:10 +00:00
|
|
|
case '3':
|
|
|
|
do_delete();
|
|
|
|
m_state = InputState::ExpectTerminator;
|
|
|
|
continue;
|
2019-05-07 00:50:15 +00:00
|
|
|
default:
|
|
|
|
dbgprintf("Shell: Unhandled final: %b (%c)\n", ch, ch);
|
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2019-08-18 01:57:10 +00:00
|
|
|
case InputState::ExpectTerminator:
|
|
|
|
m_state = InputState::Free;
|
|
|
|
continue;
|
2019-05-07 00:50:15 +00:00
|
|
|
case InputState::Free:
|
|
|
|
if (ch == 27) {
|
|
|
|
m_state = InputState::ExpectBracket;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-07 00:55:56 +00:00
|
|
|
if (ch == '\t') {
|
2019-12-10 11:25:54 +00:00
|
|
|
bool is_empty_token = m_cursor == 0 || m_buffer[(int)m_cursor - 1] == ' ';
|
2019-12-13 08:36:15 +00:00
|
|
|
m_times_tab_pressed++;
|
2019-12-10 11:25:54 +00:00
|
|
|
|
|
|
|
int token_start = (int)m_cursor - 1;
|
|
|
|
if (!is_empty_token) {
|
|
|
|
while (token_start >= 0 && m_buffer[token_start] != ' ')
|
|
|
|
--token_start;
|
|
|
|
++token_start;
|
|
|
|
}
|
2019-09-15 13:12:43 +00:00
|
|
|
|
|
|
|
bool is_first_token = true;
|
2019-12-10 11:25:54 +00:00
|
|
|
for (int i = token_start - 1; i >= 0; --i) {
|
|
|
|
if (m_buffer[i] != ' ') {
|
2019-09-15 13:12:43 +00:00
|
|
|
is_first_token = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 11:25:54 +00:00
|
|
|
String token = is_empty_token ? String() : String(&m_buffer[token_start], m_cursor - (size_t)token_start);
|
2019-12-13 08:36:15 +00:00
|
|
|
Vector<String> suggestions;
|
2019-12-10 11:25:54 +00:00
|
|
|
|
2019-09-15 13:12:43 +00:00
|
|
|
if (is_first_token)
|
2019-12-13 08:36:15 +00:00
|
|
|
suggestions = tab_complete_first_token(token);
|
2019-12-10 12:14:45 +00:00
|
|
|
else
|
2019-12-13 08:36:15 +00:00
|
|
|
suggestions = tab_complete_other_token(token);
|
2019-09-15 13:12:43 +00:00
|
|
|
|
2019-12-13 08:36:15 +00:00
|
|
|
if (m_times_tab_pressed > 1 && !suggestions.is_empty()) {
|
|
|
|
size_t longest_suggestion_length = 0;
|
|
|
|
|
|
|
|
for (auto& suggestion : suggestions)
|
|
|
|
longest_suggestion_length = max(longest_suggestion_length, suggestion.length());
|
|
|
|
|
|
|
|
size_t num_printed = 0;
|
|
|
|
putchar('\n');
|
|
|
|
for (auto& suggestion : suggestions) {
|
|
|
|
int next_column = num_printed + suggestion.length() + longest_suggestion_length + 2;
|
|
|
|
|
|
|
|
if (next_column > m_num_columns) {
|
|
|
|
putchar('\n');
|
|
|
|
num_printed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_printed += fprintf(stderr, "%-*s", static_cast<int>(longest_suggestion_length) + 2, suggestion.characters());
|
|
|
|
}
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
write(STDOUT_FILENO, prompt.characters(), prompt.length());
|
|
|
|
write(STDOUT_FILENO, m_buffer.data(), m_cursor);
|
|
|
|
// Prevent not printing characters in case the user has moved the cursor and then pressed tab
|
|
|
|
write(STDOUT_FILENO, m_buffer.data() + m_cursor, m_buffer.size() - m_cursor);
|
|
|
|
m_cursor = m_buffer.size(); // bash doesn't do this, but it makes a little bit more sense
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestions.clear_with_capacity();
|
2019-05-07 00:55:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-13 08:36:15 +00:00
|
|
|
m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB
|
|
|
|
|
2019-05-07 03:29:07 +00:00
|
|
|
auto do_backspace = [&] {
|
2019-05-15 19:40:41 +00:00
|
|
|
if (m_cursor == 0) {
|
|
|
|
fputc('\a', stdout);
|
|
|
|
fflush(stdout);
|
2019-05-07 03:29:07 +00:00
|
|
|
return;
|
2019-05-15 19:40:41 +00:00
|
|
|
}
|
2019-12-09 16:45:40 +00:00
|
|
|
m_buffer.remove((int)m_cursor - 1);
|
2019-05-07 02:17:05 +00:00
|
|
|
--m_cursor;
|
2019-05-06 23:39:10 +00:00
|
|
|
putchar(8);
|
2019-05-07 03:29:07 +00:00
|
|
|
vt_save_cursor();
|
|
|
|
vt_clear_to_end_of_line();
|
2019-12-09 16:45:40 +00:00
|
|
|
for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i)
|
|
|
|
fputc(m_buffer[(int)i], stdout);
|
2019-05-07 03:29:07 +00:00
|
|
|
vt_restore_cursor();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ch == 8 || ch == g.termios.c_cc[VERASE]) {
|
|
|
|
do_backspace();
|
2019-05-06 23:39:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == g.termios.c_cc[VWERASE]) {
|
|
|
|
bool has_seen_nonspace = false;
|
2019-05-07 03:29:07 +00:00
|
|
|
while (m_cursor > 0) {
|
2019-12-09 16:45:40 +00:00
|
|
|
if (isspace(m_buffer[(int)m_cursor - 1])) {
|
2019-05-06 23:39:10 +00:00
|
|
|
if (has_seen_nonspace)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
has_seen_nonspace = true;
|
|
|
|
}
|
2019-05-07 03:29:07 +00:00
|
|
|
do_backspace();
|
2019-05-06 23:39:10 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == g.termios.c_cc[VKILL]) {
|
2019-05-07 03:29:07 +00:00
|
|
|
while (m_cursor > 0)
|
|
|
|
do_backspace();
|
2019-05-06 23:39:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-13 08:36:15 +00:00
|
|
|
if (ch == 0xc) { // ^L
|
2019-07-19 18:01:46 +00:00
|
|
|
printf("\033[3J\033[H\033[2J"); // Clear screen.
|
|
|
|
fputs(prompt.characters(), stdout);
|
|
|
|
for (int i = 0; i < m_buffer.size(); ++i)
|
|
|
|
fputc(m_buffer[i], stdout);
|
2019-12-09 16:45:40 +00:00
|
|
|
if (m_cursor < (size_t)m_buffer.size())
|
|
|
|
printf("\033[%zuD", (size_t)m_buffer.size() - m_cursor); // Move cursor N steps left.
|
2019-07-19 18:01:46 +00:00
|
|
|
fflush(stdout);
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-01 08:47:35 +00:00
|
|
|
if (ch == 0x01) { // ^A
|
|
|
|
if (m_cursor > 0) {
|
2019-12-09 16:45:40 +00:00
|
|
|
printf("\033[%zuD", m_cursor);
|
2019-09-01 08:47:35 +00:00
|
|
|
fflush(stdout);
|
|
|
|
m_cursor = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-01 20:43:05 +00:00
|
|
|
if (ch == g.termios.c_cc[VEOF]) { // Normally ^D
|
|
|
|
if (m_buffer.is_empty()) {
|
|
|
|
printf("<EOF>\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-01 08:47:35 +00:00
|
|
|
if (ch == 0x05) { // ^E
|
2019-12-09 16:45:40 +00:00
|
|
|
if (m_cursor < (size_t)m_buffer.size()) {
|
|
|
|
printf("\033[%zuC", (size_t)m_buffer.size() - m_cursor);
|
2019-09-01 08:47:35 +00:00
|
|
|
fflush(stdout);
|
2019-12-09 16:45:40 +00:00
|
|
|
m_cursor = (size_t)m_buffer.size();
|
2019-09-01 08:47:35 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-07 02:17:05 +00:00
|
|
|
if (ch == '\n') {
|
2019-12-10 10:27:31 +00:00
|
|
|
putchar('\n');
|
|
|
|
fflush(stdout);
|
2019-05-06 23:39:10 +00:00
|
|
|
auto string = String::copy(m_buffer);
|
|
|
|
m_buffer.clear();
|
|
|
|
return string;
|
|
|
|
}
|
2019-05-07 02:17:05 +00:00
|
|
|
|
2019-12-10 10:27:31 +00:00
|
|
|
insert(ch);
|
2019-05-06 23:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 03:29:07 +00:00
|
|
|
|
|
|
|
void LineEditor::vt_save_cursor()
|
|
|
|
{
|
|
|
|
fputs("\033[s", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEditor::vt_restore_cursor()
|
|
|
|
{
|
|
|
|
fputs("\033[u", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEditor::vt_clear_to_end_of_line()
|
|
|
|
{
|
|
|
|
fputs("\033[K", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|