Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Apr 6, 2024
1 parent df414da commit 2b5f6d8
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 116 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ current (development)
---------------------

### Component
- Feature: Add support for raw input. Allowing more keys to be detected.
- Feature: Add `Mouse::WeelLeft` and `Mouse::WeelRight` events on supported
terminals.
- Feature: Add `Event::DebugString()`.
- Feature: Add support for `Input`'s insert mode. Add `InputOption::insert`
option. Added by @mingsheng13.
- Bugfix/Breaking change: `Mouse transition`:
Expand Down
120 changes: 24 additions & 96 deletions examples/component/print_key_press.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,98 +19,12 @@

using namespace ftxui;

std::map<Event, std::string> event_to_string = {
{Event::ArrowLeft, "Event::ArrowLeft"},
{Event::ArrowRight, "Event::ArrowRight"},
{Event::ArrowUp, "Event::ArrowUp"},
{Event::ArrowDown, "Event::ArrowDown"},
{Event::ArrowLeftCtrl, "Event::ArrowLeftCtrl"},
{Event::ArrowRightCtrl, "Event::ArrowRightCtrl"},
{Event::ArrowUpCtrl, "Event::ArrowUpCtrl"},
{Event::ArrowDownCtrl, "Event::ArrowDownCtrl"},
{Event::Backspace, "Event::Backspace"},
{Event::Delete, "Event::Delete"},
{Event::Escape, "Event::Escape"},
{Event::Return, "Event::Return"},
{Event::Tab, "Event::Tab"},
{Event::TabReverse, "Event::TabReverse"},
{Event::F1, "Event::F1"},
{Event::F2, "Event::F2"},
{Event::F3, "Event::F3"},
{Event::F4, "Event::F4"},
{Event::F5, "Event::F5"},
{Event::F6, "Event::F6"},
{Event::F7, "Event::F7"},
{Event::F8, "Event::F8"},
{Event::F9, "Event::F9"},
{Event::F10, "Event::F10"},
{Event::F11, "Event::F11"},
{Event::F12, "Event::F12"},
{Event::Home, "Event::Home"},
{Event::End, "Event::End"},
{Event::PageUp, "Event::PageUp"},
{Event::PageDown, "Event::PageDown"},
{Event::Custom, "Custom"},
};

std::string Stringify(Event event) {
std::string out;
for (auto& it : event.input())
out += " " + std::to_string((unsigned int)it);

out = "(" + out + " ) -> ";

if (event_to_string.count(event)) {
out += event_to_string[event];
} else if (event.is_character()) {
out += "Event::Character(\"" + event.character() + "\")";
} else if (event.is_mouse()) {
out += "mouse";
switch (event.mouse().button) {
case Mouse::Left:
out += "_left";
break;
case Mouse::Middle:
out += "_middle";
break;
case Mouse::Right:
out += "_right";
break;
case Mouse::None:
out += "_none";
break;
case Mouse::WheelUp:
out += "_wheel_up";
break;
case Mouse::WheelDown:
out += "_wheel_down";
break;
}
switch (event.mouse().motion) {
case Mouse::Pressed:
out += "_pressed";
break;
case Mouse::Released:
out += "_released";
break;
case Mouse::Moved:
out += "_moved";
break;
}
if (event.mouse().control)
out += "_control";
if (event.mouse().shift)
out += "_shift";
if (event.mouse().meta)
out += "_meta";

out += "(" + //
std::to_string(event.mouse().x) + "," +
std::to_string(event.mouse().y) + ")";
} else {
out += "(special)";
std::string Code(Event event) {
std::string codes;
for (auto& it : event.input()) {
codes += " " + std::to_string((unsigned int)it);
}
return out;
return codes;
}

int main() {
Expand All @@ -119,15 +33,29 @@ int main() {
std::vector<Event> keys;

auto component = Renderer([&] {
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i)
children.push_back(text(Stringify(keys[i])));
return window(text("keys"), vbox(std::move(children)));
Elements left_column = {
text("Codes"),
separator(),
};
Elements right_column {
text("Event"),
separator(),
};
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
left_column.push_back(text(Code(keys[i])));
right_column.push_back(text(keys[i].DebugString()));
}
return window(text("keys"), hbox({
vbox(left_column) | flex,
separator(),
vbox(right_column) | flex,
}));
});

component |= CatchEvent([&](Event event) {
keys.push_back(event);
return true;

return event != Event::CtrlC;
});

screen.Loop(component);
Expand Down
112 changes: 101 additions & 11 deletions include/ftxui/component/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,111 @@ struct Event {
static const Event Escape;
static const Event Tab;
static const Event TabReverse;
static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;

// --- Function keys ---
static const Event F1;
static const Event F2;
static const Event F3;
static const Event F4;
static const Event F5;
static const Event F6;
static const Event F7;
static const Event F8;
static const Event F9;
static const Event F10;
static const Event F11;
static const Event F12;

// --- Navigation keys ---
static const Event Insert;
static const Event Home;
static const Event End;

static const Event PageUp;
static const Event PageDown;

static const Event CtrlA, CtrlB, CtrlC, CtrlD, CtrlE, CtrlF, CtrlG, CtrlH, CtrlI, CtrlJ, CtrlK, CtrlL, CtrlM, CtrlN, CtrlO;
static const Event CtrlP, CtrlQ, CtrlR, CtrlS, CtrlT, CtrlU, CtrlV, CtrlW, CtrlX, CtrlY, CtrlZ;

static const Event AltA, AltB, AltC, AltD, AltE, AltF, AltG, AltH, AltI, AltJ, AltK, AltL, AltM, AltN, AltO, AltP, AltQ;
static const Event AltR, AltS, AltT, AltU, AltV, AltW, AltX, AltY, AltZ;

static const Event CtrlAltA, CtrlAltB, CtrlAltC, CtrlAltD, CtrlAltE, CtrlAltF, CtrlAltG, CtrlAltH, CtrlAltI, CtrlAltJ, CtrlAltK;
static const Event CtrlAltL, CtrlAltM, CtrlAltN, CtrlAltO, CtrlAltP, CtrlAltQ, CtrlAltR, CtrlAltS, CtrlAltT, CtrlAltU, CtrlAltV;
static const Event CtrlAltW, CtrlAltX, CtrlAltY, CtrlAltZ;
// --- Control keys ---
static const Event CtrlA;
static const Event CtrlB;
static const Event CtrlC;
static const Event CtrlD;
static const Event CtrlE;
static const Event CtrlF;
static const Event CtrlG;
static const Event CtrlH;
static const Event CtrlI;
static const Event CtrlJ;
static const Event CtrlK;
static const Event CtrlL;
static const Event CtrlM;
static const Event CtrlN;
static const Event CtrlO;
static const Event CtrlP;
static const Event CtrlQ;
static const Event CtrlR;
static const Event CtrlS;
static const Event CtrlT;
static const Event CtrlU;
static const Event CtrlV;
static const Event CtrlW;
static const Event CtrlX;
static const Event CtrlY;
static const Event CtrlZ;

// --- Alt keys ---
static const Event AltA;
static const Event AltB;
static const Event AltC;
static const Event AltD;
static const Event AltE;
static const Event AltF;
static const Event AltG;
static const Event AltH;
static const Event AltI;
static const Event AltJ;
static const Event AltK;
static const Event AltL;
static const Event AltM;
static const Event AltN;
static const Event AltO;
static const Event AltP;
static const Event AltQ;
static const Event AltR;
static const Event AltS;
static const Event AltT;
static const Event AltU;
static const Event AltV;
static const Event AltW;
static const Event AltX;
static const Event AltY;
static const Event AltZ;

// --- Ctrl+Alt keys ---
static const Event CtrlAltA;
static const Event CtrlAltB;
static const Event CtrlAltC;
static const Event CtrlAltD;
static const Event CtrlAltE;
static const Event CtrlAltF;
static const Event CtrlAltG;
static const Event CtrlAltH;
static const Event CtrlAltI;
static const Event CtrlAltJ;
static const Event CtrlAltK;
static const Event CtrlAltL;
static const Event CtrlAltM;
static const Event CtrlAltN;
static const Event CtrlAltO;
static const Event CtrlAltP;
static const Event CtrlAltQ;
static const Event CtrlAltR;
static const Event CtrlAltS;
static const Event CtrlAltT;
static const Event CtrlAltU;
static const Event CtrlAltV;
static const Event CtrlAltW;
static const Event CtrlAltX;
static const Event CtrlAltY;
static const Event CtrlAltZ;

// --- Custom ---
static const Event Custom;
Expand All @@ -97,6 +184,9 @@ struct Event {
bool is_cursor_shape() const { return type_ == Type::CursorShape; }
int cursor_shape() const { return data_.cursor_shape; }

// Debug
std::string DebugString() const;

//--- State section ----------------------------------------------------------
ScreenInteractive* screen_ = nullptr;

Expand Down
2 changes: 2 additions & 0 deletions include/ftxui/component/mouse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct Mouse {
None = 3,
WheelUp = 4,
WheelDown = 5,
WheelLeft = 6, /// Supported terminal only.
WheelRight = 7, /// Supported terminal only.
};

enum Motion {
Expand Down
Loading

0 comments on commit 2b5f6d8

Please sign in to comment.