2024-03-07 09:15:11 +01:00
|
|
|
#include "elna/shell/interactive.hpp"
|
2024-02-25 15:16:19 +01:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2024-02-22 21:29:25 +01:00
|
|
|
#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());
|
|
|
|
|
2024-02-25 15:16:19 +01:00
|
|
|
if (!execute(line.value().command_line()))
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<editor_state> read_line(editor_history& history)
|
|
|
|
{
|
|
|
|
editor_state state;
|
|
|
|
std::string buffer;
|
2024-02-25 15:16:19 +01:00
|
|
|
std::string saved;
|
2024-02-22 21:29:25 +01:00
|
|
|
|
|
|
|
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);
|
2024-02-25 15:16:19 +01:00
|
|
|
state.insert(complete(state.command_line(), state.relative_position()));
|
2024-02-22 21:29:25 +01:00
|
|
|
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))
|
|
|
|
{
|
2024-02-25 15:16:19 +01:00
|
|
|
if (history.current() == history.cend())
|
|
|
|
{
|
|
|
|
saved = state.command_line();
|
|
|
|
}
|
|
|
|
history_iterator = history.prev();
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
else if (pressed_key == key(special_key::arrow_down, modifier::ctrl))
|
|
|
|
{
|
2024-02-25 15:16:19 +01:00
|
|
|
history_iterator = history.next();
|
|
|
|
if (history_iterator == history.cend())
|
|
|
|
{
|
|
|
|
state.load(saved);
|
|
|
|
}
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
if (history_iterator != history.cend())
|
|
|
|
{
|
|
|
|
state.load(*history_iterator);
|
|
|
|
}
|
2024-02-25 15:16:19 +01:00
|
|
|
state.draw(std::back_inserter(buffer));
|
|
|
|
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
2024-02-22 21:29:25 +01:00
|
|
|
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();
|
|
|
|
}
|
2024-02-25 15:16:19 +01:00
|
|
|
auto [number, last_char] = read_number<key::char_t>();
|
2024-02-22 21:29:25 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-02-25 15:16:19 +01:00
|
|
|
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)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-02-25 15:16:19 +01:00
|
|
|
if (line.empty())
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2024-02-25 15:16:19 +01:00
|
|
|
if (line == "exit")
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-25 15:16:19 +01:00
|
|
|
else if (boost::starts_with(line, "cd "))
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-02-25 15:16:19 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::filesystem::current_path(line.substr(strlen("cd ")));
|
|
|
|
}
|
|
|
|
catch (const std::filesystem::filesystem_error& exception)
|
|
|
|
{
|
|
|
|
print_exception(exception);
|
|
|
|
}
|
2024-02-22 21:29:25 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-02-25 15:16:19 +01:00
|
|
|
launch(line);
|
2024-02-22 21:29:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-25 15:16:19 +01:00
|
|
|
void launch(const std::string& program)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-02-25 15:16:19 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::process::system(program);
|
|
|
|
}
|
|
|
|
catch (const boost::process::process_error& exception)
|
|
|
|
{
|
|
|
|
print_exception(exception);
|
|
|
|
}
|
|
|
|
enable_raw_mode();
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-25 15:16:19 +01:00
|
|
|
std::string complete(const std::string& input, const std::size_t position)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-02-25 15:16:19 +01:00
|
|
|
std::filesystem::path program = boost::process::search_path("fzf");
|
|
|
|
|
|
|
|
if (program.empty())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2024-02-22 21:29:25 +01:00
|
|
|
boost::process::ipstream output;
|
2024-02-25 15:16:19 +01:00
|
|
|
boost::process::system(program,
|
|
|
|
"--height=10", "--layout=reverse", "-1", "--no-multi",
|
2024-02-22 21:29:25 +01:00
|
|
|
boost::process::std_out > output);
|
|
|
|
|
|
|
|
std::string selections;
|
|
|
|
std::getline(output, selections, '\n');
|
|
|
|
|
|
|
|
return selections;
|
|
|
|
}
|
|
|
|
}
|