Extend the tester to compile sources
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
#include "elna/interactive.hpp"
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/process.hpp>
|
||||
#include <termios.h>
|
||||
#include <filesystem>
|
||||
|
||||
#include "elna/interactive.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
static termios original_termios;
|
||||
@ -39,9 +40,8 @@ namespace elna
|
||||
break;
|
||||
}
|
||||
history.push(line.value().command_line());
|
||||
auto tokens = lex(line.value().command_line());
|
||||
|
||||
if (!execute(tokens.assume_value()))
|
||||
if (!execute(line.value().command_line()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -53,6 +53,7 @@ namespace elna
|
||||
{
|
||||
editor_state state;
|
||||
std::string buffer;
|
||||
std::string saved;
|
||||
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
@ -81,7 +82,7 @@ namespace elna
|
||||
break;
|
||||
case action::complete:
|
||||
write(STDOUT_FILENO, "\x1b[1E\x1b[2K", 8);
|
||||
state.insert(complete());
|
||||
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());
|
||||
@ -104,18 +105,26 @@ namespace elna
|
||||
|
||||
if (pressed_key == key(special_key::arrow_up, modifier::ctrl))
|
||||
{
|
||||
history_iterator = history.next();
|
||||
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.prev();
|
||||
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());
|
||||
}
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -137,7 +146,7 @@ namespace elna
|
||||
{
|
||||
return key();
|
||||
}
|
||||
auto [number, last_char] = read_number<unsigned char>();
|
||||
auto [number, last_char] = read_number<key::char_t>();
|
||||
std::uint8_t modifiers{ 0 };
|
||||
|
||||
if (last_char == ';')
|
||||
@ -185,35 +194,51 @@ namespace elna
|
||||
return key();
|
||||
}
|
||||
|
||||
bool execute(const std::vector<token>& tokens)
|
||||
void print_exception(const std::exception& exception)
|
||||
{
|
||||
if (tokens.empty())
|
||||
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;
|
||||
}
|
||||
std::string program = tokens.front().identifier();
|
||||
if (program == "exit")
|
||||
if (line == "exit")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (program == "cd")
|
||||
else if (boost::starts_with(line, "cd "))
|
||||
{
|
||||
std::filesystem::current_path(tokens[1].identifier());
|
||||
try
|
||||
{
|
||||
std::filesystem::current_path(line.substr(strlen("cd ")));
|
||||
}
|
||||
catch (const std::filesystem::filesystem_error& exception)
|
||||
{
|
||||
print_exception(exception);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
std::vector<std::string> arguments;
|
||||
for (auto argument = tokens.cbegin() + 1; argument != std::cend(tokens); ++argument)
|
||||
{
|
||||
arguments.push_back(argument->identifier());
|
||||
}
|
||||
launch(program, arguments);
|
||||
launch(line);
|
||||
return true;
|
||||
}
|
||||
|
||||
void launch(const std::string& program, const std::vector<std::string>& arguments)
|
||||
void launch(const std::string& program)
|
||||
{
|
||||
boost::process::system(boost::process::search_path(program), arguments);
|
||||
try
|
||||
{
|
||||
boost::process::system(program);
|
||||
}
|
||||
catch (const boost::process::process_error& exception)
|
||||
{
|
||||
print_exception(exception);
|
||||
}
|
||||
enable_raw_mode();
|
||||
}
|
||||
|
||||
bool enable_raw_mode()
|
||||
@ -240,10 +265,17 @@ namespace elna
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
|
||||
}
|
||||
|
||||
std::string complete()
|
||||
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("/usr/bin/fzf", "--height=10", "--layout=reverse",
|
||||
boost::process::system(program,
|
||||
"--height=10", "--layout=reverse", "-1", "--no-multi",
|
||||
boost::process::std_out > output);
|
||||
|
||||
std::string selections;
|
||||
|
Reference in New Issue
Block a user