Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add G and GG keybinding. #24

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ unreleased:
-----------
- Roll new FTXUI version. This fixes Table horizontal separators.
- Fix component ordering in some tables.
- Support `G` and `gg` keybinding.
- Add option `-k`, `-key`, `-keybinding`. This displays the supported keys.

v1.3.0:
-------
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(FETCHCONTENT_QUIET FALSE)

FetchContent_Declare(ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
GIT_TAG 81b7207121e41c3b838eca6f54396e4f676fa3d8
GIT_TAG fab74f745d559ab6fe400fb0dd9c2d9255955ef3
GIT_PROGRESS TRUE
GIT_SHALLOW FALSE
)
Expand Down
43 changes: 43 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <args.hxx>
#include <cstdio>
#include <fstream>
#include <ftxui/dom/elements.hpp>
#include <ftxui/dom/table.hpp>
#include <ftxui/screen/screen.hpp>
#include <iostream>
#include <nlohmann/json.hpp>
#include "main_ui.hpp"
Expand All @@ -28,6 +31,8 @@ int main(int argument_count, const char** arguments) {
"A JSON file. Omit to read from stdin.");
args::Flag help(args, "help", "Display this help menu.", {'h', "help"});
args::Flag version(args, "version", "Print version.", {'v', "version"});
args::Flag keybinding(args, "keybinding", "Display key binding.",
{'k', "key", "keybinding"});
args::Flag fullscreen(
args, "fullscreen",
"Display the JSON in fullscreen, in an alternate buffer",
Expand All @@ -48,6 +53,44 @@ int main(int argument_count, const char** arguments) {
return EXIT_SUCCESS;
}

if (keybinding) {
using namespace ftxui;
auto table = Table(std::vector<std::vector<std::string>>{
{"keys", "action"},
//
{"Navigate", "← ↑ ↓ →"},
{"", "h j k l "},
{"", "Mouse::WheelUp"},
{"", "Mouse::WheelDown"},
//
{"Toggle", "enter"},
{"", "Mouse::Left"},
//
{"Exit", "Escape"},
{"", "q"},
//
{"Navigate", ""},
{" - first", "page-up"},
{" - last", "page-down"},
{" - top", "gg"},
{" - bottom", "G"},
//
});
table.SelectRows(0, 0).DecorateCells(color(Color::Cyan));
table.SelectRows(1, 4).Border(LIGHT);
table.SelectRows(5, 6).Border(LIGHT);
table.SelectRows(7, 8).Border(LIGHT);
table.SelectRows(9, 13).Border(LIGHT);
table.SelectAll().SeparatorVertical(LIGHT);
table.SelectAll().Border(LIGHT);
auto document = table.Render();
auto screen = Screen::Create(Dimension::Fit(document));
Render(screen, document);
screen.Print();
std::cout << std::endl;
return EXIT_SUCCESS;
}

std::stringstream ss;
if (file) {
auto file_stream = std::ifstream(args::get(file));
Expand Down
22 changes: 20 additions & 2 deletions src/main_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,25 @@ void DisplayMainUI(const JSON& json, bool fullscreen) {
component =
Renderer(component, [component] { return component->Render() | yframe; });

component = CatchEvent(component, [&](Event event) {
Event previous_event;
Event next_event;
auto wrapped_component = CatchEvent(component, [&](Event event) {
previous_event = next_event;
next_event = event;

// 'G' and 'gg -------------------------------------------------------------
if (event == Event::Character('G')) {
while (component->OnEvent(Event::ArrowUp))
;
return true;
}
if (previous_event == Event::Character('g') &&
next_event == Event::Character('g')) {
while (component->OnEvent(Event::ArrowDown))
;
return true;
}

// Allow the user to quit using 'q' or ESC ---------------------------------
if (event == Event::Character('q') || event == Event::Escape) {
screen.ExitLoopClosure()();
Expand All @@ -403,5 +421,5 @@ void DisplayMainUI(const JSON& json, bool fullscreen) {
return false;
});

screen.Loop(component);
screen.Loop(wrapped_component);
}