Skip to content

Commit

Permalink
Shell: Ensure that the cursor starts on a new line
Browse files Browse the repository at this point in the history
And print an indicator when it doesn't, a la zsh.
  • Loading branch information
alimpfard authored and awesomekling committed Aug 17, 2020
1 parent c96b9fd commit 14a54a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,33 @@ Vector<Line::CompletionSuggestion> Shell::complete_option(const String& program_
return suggestions;
}

void Shell::bring_cursor_to_beginning_of_a_line() const
{
struct winsize ws;
if (editor) {
ws = editor->terminal_size();
} else {
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) < 0) {
// Very annoying assumptions.
ws.ws_col = 80;
ws.ws_row = 25;
}
}

Line::VT::apply_style(Line::Style { Line::Style::Background(Line::Style::XtermColor::Cyan), Line::Style::Foreground(Line::Style::XtermColor::Black) });
putc('%', stderr);
Line::VT::apply_style(Line::Style::reset_style());

for (auto i = 2; i < ws.ws_col; ++i)
putc(' ', stderr);

putc('\r', stderr);
}

bool Shell::read_single_line()
{
restore_ios();
bring_cursor_to_beginning_of_a_line();
auto line_result = editor->get_line(prompt());

if (line_result.is_error()) {
Expand Down
1 change: 1 addition & 0 deletions Shell/Shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class Shell : public Core::Object {

// ^Core::Object
virtual void save_to(JsonObject&) override;
void bring_cursor_to_beginning_of_a_line() const;

void cache_path();
void add_entry_to_cache(const String&);
Expand Down

0 comments on commit 14a54a7

Please sign in to comment.