Extend the tester to compile sources
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <boost/endian.hpp>
|
||||
|
||||
#include "elna/state.hpp"
|
||||
|
||||
namespace elna
|
||||
@ -12,16 +15,16 @@ namespace elna
|
||||
}
|
||||
|
||||
key::key()
|
||||
: store(static_cast<unsigned char>(0x1b))
|
||||
: store(0x1bu)
|
||||
{
|
||||
}
|
||||
|
||||
key::key(const unsigned char c, const std::uint8_t modifiers)
|
||||
key::key(const char_t c, const std::uint8_t modifiers)
|
||||
: store(c), modifiers(modifiers == 0 ? 0 : modifiers - 1)
|
||||
{
|
||||
}
|
||||
|
||||
key::key(const unsigned char c, const modifier modifiers)
|
||||
key::key(const char_t c, const modifier modifiers)
|
||||
: store(c), modifiers(static_cast<uint8_t>(modifiers))
|
||||
{
|
||||
}
|
||||
@ -36,9 +39,9 @@ namespace elna
|
||||
{
|
||||
}
|
||||
|
||||
unsigned char key::character() const
|
||||
key::char_t key::character() const
|
||||
{
|
||||
return std::get<unsigned char>(this->store);
|
||||
return std::get<char_t>(this->store);
|
||||
}
|
||||
|
||||
special_key key::control() const
|
||||
@ -56,10 +59,10 @@ namespace elna
|
||||
return this->modifiers == static_cast<std::uint8_t>(modifiers);
|
||||
}
|
||||
|
||||
bool key::operator==(const unsigned char c) const
|
||||
bool key::operator==(const char_t c) const
|
||||
{
|
||||
return std::holds_alternative<unsigned char>(this->store)
|
||||
&& std::get<unsigned char>(this->store) == c;
|
||||
return std::holds_alternative<char_t>(this->store)
|
||||
&& std::get<char_t>(this->store) == c;
|
||||
}
|
||||
|
||||
bool key::operator==(const special_key control) const
|
||||
@ -73,7 +76,7 @@ namespace elna
|
||||
return this->store == that.store;
|
||||
}
|
||||
|
||||
bool key::operator!=(const unsigned char c) const
|
||||
bool key::operator!=(const char_t c) const
|
||||
{
|
||||
return !(*this == c);
|
||||
}
|
||||
@ -90,7 +93,7 @@ namespace elna
|
||||
|
||||
bool key::is_character() const noexcept
|
||||
{
|
||||
return std::holds_alternative<unsigned char>(this->store);
|
||||
return std::holds_alternative<char_t>(this->store);
|
||||
}
|
||||
|
||||
bool key::is_control() const noexcept
|
||||
@ -131,6 +134,7 @@ namespace elna
|
||||
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
|
||||
@ -143,9 +147,36 @@ namespace elna
|
||||
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(std::end(this->input), std::cbegin(text), std::cend(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)
|
||||
@ -202,12 +233,21 @@ namespace elna
|
||||
this->input.erase(this->position - 1, 1);
|
||||
}
|
||||
return action::redraw;
|
||||
case 9: // Tab.
|
||||
return action::complete;
|
||||
case '\r':
|
||||
return action::finalize;
|
||||
default:
|
||||
++this->position;
|
||||
this->input.push_back(press.character());
|
||||
return action::write;
|
||||
if (this->position - 1 < input.size())
|
||||
{
|
||||
insert(press.character());
|
||||
return action::redraw;
|
||||
}
|
||||
else
|
||||
{
|
||||
insert(press.character());
|
||||
return action::write;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user