Print test summary

This commit is contained in:
2022-06-05 23:43:45 +02:00
parent 5490f6ce1c
commit f37700a02d
47 changed files with 4362 additions and 1184 deletions

54
shell/history.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "elna/history.hpp"
namespace elna
{
editor_history::editor_history()
: commands{}, current_pointer(commands.cend())
{
}
void editor_history::push(const std::string& entry)
{
commands.push_back(entry);
current_pointer = commands.cend();
}
void editor_history::clear()
{
commands.clear();
current_pointer = commands.cend();
}
editor_history::const_iterator editor_history::prev() noexcept
{
if (this->current_pointer != cbegin())
{
this->current_pointer = std::prev(this->current_pointer);
}
return this->current_pointer;
}
editor_history::const_iterator editor_history::next() noexcept
{
if (this->current_pointer != cend())
{
this->current_pointer = std::next(this->current_pointer);
}
return this->current_pointer;
}
editor_history::const_iterator editor_history::cbegin() const noexcept
{
return this->commands.cbegin();
}
editor_history::const_iterator editor_history::cend() const noexcept
{
return this->commands.cend();
}
editor_history::const_iterator editor_history::current() const noexcept
{
return this->current_pointer;
}
}

286
shell/interactive.cpp Normal file
View File

@ -0,0 +1,286 @@
#include "elna/interactive.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/process.hpp>
#include <termios.h>
#include <filesystem>
namespace elna
{
static termios original_termios;
interactive_exception::interactive_exception()
: runtime_error("read")
{
}
template<typename T>
static std::pair<T, unsigned char> read_number()
{
T position{ 0 };
unsigned char c{ 0 };
while (read(STDIN_FILENO, &c, 1) == 1 && std::isdigit(c))
{
position = position * 10 + (c - '0');
}
return std::make_pair(position, c);
}
void loop()
{
editor_history history;
do
{
auto line = read_line(history);
if (!line.has_value())
{
write(STDOUT_FILENO, "\r\n", 2);
break;
}
history.push(line.value().command_line());
if (!execute(line.value().command_line()))
{
break;
}
}
while (true);
}
std::optional<editor_state> read_line(editor_history& history)
{
editor_state state;
std::string buffer;
std::string saved;
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
while (true)
{
buffer.clear();
auto pressed_key = read_key();
if (pressed_key.empty())
{
continue;
}
switch (state.process_keypress(pressed_key))
{
case action::finalize:
if (pressed_key == key('d', modifier::ctrl))
{
return std::optional<editor_state>();
}
else if (pressed_key == '\r')
{
write(STDOUT_FILENO, "\r\n", 2);
return std::make_optional(state);
}
break;
case action::complete:
write(STDOUT_FILENO, "\x1b[1E\x1b[2K", 8);
state.insert(complete(state.command_line(), state.relative_position()));
write(STDOUT_FILENO, "\x1b[1A", 4);
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
case action::redraw:
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
case action::write:
write(STDOUT_FILENO, &pressed_key, 1);
break;
case action::move:
buffer = cursor_to_column(state.absolute_position());
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
case action::ignore:
break;
case action::history:
editor_history::const_iterator history_iterator = history.cend();
if (pressed_key == key(special_key::arrow_up, modifier::ctrl))
{
if (history.current() == history.cend())
{
saved = state.command_line();
}
history_iterator = history.prev();
}
else if (pressed_key == key(special_key::arrow_down, modifier::ctrl))
{
history_iterator = history.next();
if (history_iterator == history.cend())
{
state.load(saved);
}
}
if (history_iterator != history.cend())
{
state.load(*history_iterator);
}
state.draw(std::back_inserter(buffer));
write(STDOUT_FILENO, buffer.data(), buffer.size());
break;
}
}
}
key read_key()
{
char c{ 0 };
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN)
{
throw interactive_exception();
}
if (c != '\x1b')
{
return key(c);
}
if (read(STDIN_FILENO, &c, 1) != 1 || c != '[')
{
return key();
}
auto [number, last_char] = read_number<key::char_t>();
std::uint8_t modifiers{ 0 };
if (last_char == ';')
{
auto modifier_response = read_number<std::uint8_t>();
modifiers = modifier_response.first;
last_char = modifier_response.second;
}
if (number == 0 || number == 1)
{
switch (last_char)
{
case 'A':
return key(special_key::arrow_up, modifiers);
case 'B':
return key(special_key::arrow_down, modifiers);
case 'C':
return key(special_key::arrow_right, modifiers);
case 'D':
return key(special_key::arrow_left, modifiers);
case 'H':
return key(special_key::home_key, modifiers);
case 'F':
return key(special_key::end_key, modifiers);
}
}
else if (last_char == '~')
{
switch (number)
{
case 5:
return key(special_key::page_up, modifiers);
case 6:
return key(special_key::page_up, modifiers);
case 7:
return key(special_key::home_key, modifiers);
case 8:
return key(special_key::end_key, modifiers);
}
}
else if (last_char == 'u')
{
return key(number, modifiers);
}
return key();
}
void print_exception(const std::exception& exception)
{
std::string message{ exception.what() };
message += "\r\n";
write(STDERR_FILENO, message.data(), message.size());
}
bool execute(const std::string& line)
{
if (line.empty())
{
return true;
}
if (line == "exit")
{
return false;
}
else if (boost::starts_with(line, "cd "))
{
try
{
std::filesystem::current_path(line.substr(strlen("cd ")));
}
catch (const std::filesystem::filesystem_error& exception)
{
print_exception(exception);
}
return true;
}
launch(line);
return true;
}
void launch(const std::string& program)
{
try
{
boost::process::system(program);
}
catch (const boost::process::process_error& exception)
{
print_exception(exception);
}
enable_raw_mode();
}
bool enable_raw_mode()
{
if (tcgetattr(STDIN_FILENO, &original_termios) == -1)
{
return false;
}
termios raw = original_termios;
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= CS8;
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
write(STDOUT_FILENO, start_kitty_keybaord, strlen(start_kitty_keybaord));
return tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) != -1;
}
void disable_raw_mode()
{
write(STDOUT_FILENO, end_kitty_keybaord, strlen(end_kitty_keybaord));
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
}
std::string complete(const std::string& input, const std::size_t position)
{
std::filesystem::path program = boost::process::search_path("fzf");
if (program.empty())
{
return "";
}
boost::process::ipstream output;
boost::process::system(program,
"--height=10", "--layout=reverse", "-1", "--no-multi",
boost::process::std_out > output);
std::string selections;
std::getline(output, selections, '\n');
return selections;
}
}

16
shell/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <cstdlib>
#include <unistd.h>
#include "elna/interactive.hpp"
int main()
{
if (!elna::enable_raw_mode())
{
std::perror("tcsetattr");
return EXIT_FAILURE;
}
std::atexit(elna::disable_raw_mode);
elna::loop();
return EXIT_SUCCESS;
}

254
shell/state.cpp Normal file
View File

@ -0,0 +1,254 @@
#include <algorithm>
#include <boost/endian.hpp>
#include "elna/state.hpp"
namespace elna
{
std::string cursor_to_column(const std::uint16_t position)
{
std::string code = std::to_string(position);
code.insert(0, "\x1b[");
code.push_back('G');
return code;
}
key::key()
: store(0x1bu)
{
}
key::key(const char_t c, const std::uint8_t modifiers)
: store(c), modifiers(modifiers == 0 ? 0 : modifiers - 1)
{
}
key::key(const char_t c, const modifier modifiers)
: store(c), modifiers(static_cast<uint8_t>(modifiers))
{
}
key::key(const special_key control, const std::uint8_t modifiers)
: store(control), modifiers(modifiers == 0 ? 0 : modifiers - 1)
{
}
key::key(const special_key control, const modifier modifiers)
: store(control), modifiers(static_cast<uint8_t>(modifiers))
{
}
key::char_t key::character() const
{
return std::get<char_t>(this->store);
}
special_key key::control() const
{
return std::get<special_key>(this->store);
}
bool key::modified(const std::uint8_t modifiers) const noexcept
{
return this->modifiers == modifiers;
}
bool key::modified(const modifier modifiers) const noexcept
{
return this->modifiers == static_cast<std::uint8_t>(modifiers);
}
bool key::operator==(const char_t c) const
{
return std::holds_alternative<char_t>(this->store)
&& std::get<char_t>(this->store) == c;
}
bool key::operator==(const special_key control) const
{
return std::holds_alternative<special_key>(this->store)
&& std::get<special_key>(this->store) == control;
}
bool key::operator==(const key& that) const
{
return this->store == that.store;
}
bool key::operator!=(const char_t c) const
{
return !(*this == c);
}
bool key::operator!=(const special_key control) const
{
return !(*this == control);
}
bool key::operator!=(const key& that) const
{
return !(*this == that);
}
bool key::is_character() const noexcept
{
return std::holds_alternative<char_t>(this->store);
}
bool key::is_control() const noexcept
{
return std::holds_alternative<special_key>(this->store);
}
bool key::empty() const noexcept
{
return *this == '\x1b';
}
bool operator==(const special_key control, const key& that)
{
return that == control;
}
bool operator==(const char c, const key& that)
{
return that == c;
}
bool operator!=(const special_key control, const key& that)
{
return that != control;
}
bool operator!=(const char c, const key& that)
{
return that != c;
}
editor_state::editor_state()
{
this->input.reserve(1024);
}
void editor_state::load(const std::string& text)
{
this->input = text;
this->position = this->input.size() + 1;
}
const std::string& editor_state::command_line() const noexcept
{
return this->input;
}
std::size_t editor_state::absolute_position() const noexcept
{
return this->prompt.size() + this->position;
}
std::size_t editor_state::relative_position() const noexcept
{
return this->position;
}
void editor_state::insert(const std::string& text)
{
this->input.insert(this->position - 1, text);
this->position += text.size();
}
void editor_state::insert(const key::char_t text)
{
if (text == 0u)
{
this->input.insert(this->position - 1, '\0', 1);
++this->position;
return;
}
key::char_t buffer = boost::endian::native_to_big<key::char_t>(text);
const char *begin = reinterpret_cast<char *>(&buffer);
const char *end = begin + sizeof(key::char_t);
const char *significant = std::find_if(begin, end,
[](const char byte) {
return byte != 0;
});
this->input.insert(this->position - 1, significant, end - significant);
this->position += end - significant;
}
action editor_state::process_keypress(const key& press)
{
if (press.is_control())
{
switch (press.control())
{
case special_key::arrow_left:
if (this->position > 1)
{
--this->position;
}
return action::move;
case special_key::arrow_right:
if (this->position + 1 < this->input.size() + this->prompt.size())
{
++this->position;
}
return action::move;
case special_key::arrow_up:
case special_key::arrow_down:
return action::history;
case special_key::home_key:
this->position = 1;
return action::move;
case special_key::end_key:
this->position = this->input.size() + 1;
return action::move;
default:
return action::ignore;
}
}
else if (press.modified(modifier::ctrl))
{
switch (press.character())
{
case 'd':
return action::finalize;
case 't':
return action::complete;
default:
return action::ignore;
}
}
else
{
switch (press.character())
{
case 127: // Backspace.
if (this->position <= this->input.size() + 1 && this->position > 1)
{
--this->position;
this->input.erase(this->position - 1, 1);
}
return action::redraw;
case 9: // Tab.
return action::complete;
case '\r':
return action::finalize;
default:
if (this->position - 1 < input.size())
{
insert(press.character());
return action::redraw;
}
else
{
insert(press.character());
return action::write;
}
}
}
}
}