Skip to content

Commit

Permalink
feat: add basic cursor movement
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-go committed Sep 30, 2023
1 parent fd8fea7 commit 9826d03
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/io/include/qupp/io/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ namespace qupp::io {
struct TerminalInterface {
virtual ~TerminalInterface() = default;

virtual void print(const std::string &message) = 0;
virtual void print(const std::string &message, bool break_line) = 0;
virtual std::string read() = 0;
virtual void move_cursor_left(int n) = 0;
virtual void move_cursor_right(int n) = 0;
virtual void move_cursor_up(int n) = 0;
virtual void move_cursor_down(int n) = 0;
};

struct Terminal : public TerminalInterface {
static Terminal &get_instance();

void print(const std::string &message);
void print(const std::string &message, bool break_line);
std::string read();
void move_cursor_left(int n);
void move_cursor_right(int n);
void move_cursor_up(int n);
void move_cursor_down(int n);

private:
// This is a key part of the Singleton Pattern; it prevents the creation of
Expand Down
12 changes: 10 additions & 2 deletions src/io/unix/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ Terminal &Terminal::get_instance() {
return instance;
}

void Terminal::print(const std::string &message) {
std::cout << message << std::endl;
void Terminal::print(const std::string &message, bool break_line) {
std::cout << message;
if (break_line) {
std::cout << std::endl;
}
}

void Terminal::move_cursor_left(int n) { std::cout << "\033[" << n << "D"; }
void Terminal::move_cursor_right(int n) { std::cout << "\033[" << n << "C"; }
void Terminal::move_cursor_up(int n) { std::cout << "\033[" << n << "A"; }
void Terminal::move_cursor_down(int n) { std::cout << "\033[" << n << "B"; }

std::string Terminal::read() {
std::string input;
std::cin >> input;
Expand Down
3 changes: 2 additions & 1 deletion src/prompt/unix/prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ StringResult Prompt::ask_for_input(const std::string &question) {
if (question.empty()) {
return std::nullopt;
}
terminal_.print(question);
terminal_.print(question, false);
terminal_.move_cursor_right(1);
return terminal_.read();
}
} // namespace qupp::prompt
7 changes: 6 additions & 1 deletion test/test_prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
#include <vector>

struct MockTerminal : public qupp::io::TerminalInterface {
void print(const std::string &message) override {
void print(const std::string &message, bool) override {
messages_.push_back(message);
}

std::string read() override { return answer_; }

void move_cursor_left(int n) override {}
void move_cursor_right(int n) override {}
void move_cursor_up(int n) override {}
void move_cursor_down(int n) override {}

// Test helper function.

bool has_printed(const std::string &message) {
Expand Down

0 comments on commit 9826d03

Please sign in to comment.