Skip to content

Commit

Permalink
Shell: Tab completion now gives suggestions
Browse files Browse the repository at this point in the history
Pushing the TAB key in the shell now prints suggestions to terminal.
This makes it easier to the user to actually see what files are
available before executing the command they currently have typed.
  • Loading branch information
Quaker762 authored and awesomekling committed Dec 17, 2019
1 parent 3809da4 commit cdb0053
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
83 changes: 75 additions & 8 deletions Shell/LineEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#include "GlobalState.h"
#include <ctype.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>

LineEditor::LineEditor()
{
struct winsize ws;
int rc = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
ASSERT(rc == 0);
m_num_columns = ws.ws_col;
}

LineEditor::~LineEditor()
Expand Down Expand Up @@ -106,15 +111,18 @@ void LineEditor::cut_mismatching_chars(String& completion, const String& other,
completion = completion.substring(0, i);
}

void LineEditor::tab_complete_first_token(const String& token)
// 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)
{
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)
return;
return Vector<String>();

String completion = *match;
Vector<String> suggestions;

// 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
Expand All @@ -123,25 +131,31 @@ void LineEditor::tab_complete_first_token(const String& token)
bool seen_others = false;
int index = match - m_path.data();
for (int i = index - 1; i >= 0 && m_path[i].starts_with(token); --i) {
suggestions.append(m_path[i]);
cut_mismatching_chars(completion, m_path[i], token.length());
seen_others = true;
}
for (int i = index + 1; i < m_path.size() && m_path[i].starts_with(token); ++i) {
cut_mismatching_chars(completion, m_path[i], token.length());
suggestions.append(m_path[i]);
seen_others = true;
}
suggestions.append(m_path[index]);

// 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(' ');

return suggestions;
}

void LineEditor::tab_complete_other_token(String& token)
Vector<String> LineEditor::tab_complete_other_token(String& token)
{
String path;
Vector<String> suggestions;

int last_slash = (int)token.length() - 1;
while (last_slash >= 0 && token[last_slash] != '/')
Expand All @@ -161,25 +175,38 @@ void LineEditor::tab_complete_other_token(String& token)
path = g.cwd;
}

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

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)) {
if (!token.is_empty())
suggestions.append(file);
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!
return;
return suggestions;
seen_others = true;
}
}
}
if (completion.is_empty())
return;
return suggestions;

// If we have characters to add, add them.
if (completion.length() > token.length())
Expand All @@ -197,6 +224,8 @@ void LineEditor::tab_complete_other_token(String& token)
insert(' ');
}
}

return {}; // Return an empty vector
}

String LineEditor::get_line(const String& prompt)
Expand All @@ -223,6 +252,12 @@ String LineEditor::get_line(const String& prompt)
g.was_resized = false;
printf("\033[2K\r");
m_buffer.clear();

struct winsize ws;
int rc = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
ASSERT(rc == 0);
m_num_columns = ws.ws_col;

return String::empty();
}
m_buffer.clear();
Expand Down Expand Up @@ -337,6 +372,7 @@ String LineEditor::get_line(const String& prompt)

if (ch == '\t') {
bool is_empty_token = m_cursor == 0 || m_buffer[(int)m_cursor - 1] == ' ';
m_times_tab_pressed++;

int token_start = (int)m_cursor - 1;
if (!is_empty_token) {
Expand All @@ -354,15 +390,46 @@ String LineEditor::get_line(const String& prompt)
}

String token = is_empty_token ? String() : String(&m_buffer[token_start], m_cursor - (size_t)token_start);
Vector<String> suggestions;

if (is_first_token)
tab_complete_first_token(token);
suggestions = tab_complete_first_token(token);
else
tab_complete_other_token(token);
suggestions = tab_complete_other_token(token);

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

m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB

auto do_backspace = [&] {
if (m_cursor == 0) {
fputc('\a', stdout);
Expand Down Expand Up @@ -401,7 +468,7 @@ String LineEditor::get_line(const String& prompt)
do_backspace();
continue;
}
if (ch == 0xc) { // ^L
if (ch == 0xc) { // ^L
printf("\033[3J\033[H\033[2J"); // Clear screen.
fputs(prompt.characters(), stdout);
for (int i = 0; i < m_buffer.size(); ++i)
Expand Down
6 changes: 4 additions & 2 deletions Shell/LineEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ class LineEditor {
void insert(const String&);
void insert(const char);
void cut_mismatching_chars(String& completion, const String& other, size_t start_compare);
void tab_complete_first_token(const String&);
void tab_complete_other_token(String&);
Vector<String> tab_complete_first_token(const String&);
Vector<String> tab_complete_other_token(String&);
void vt_save_cursor();
void vt_restore_cursor();
void vt_clear_to_end_of_line();

Vector<char, 1024> m_buffer;
size_t m_cursor { 0 };
int m_times_tab_pressed { 0 };
int m_num_columns { 0 };

// FIXME: This should be something more take_first()-friendly.
Vector<String> m_history;
Expand Down

0 comments on commit cdb0053

Please sign in to comment.