Implement a lot of new features.

This commit deserve to be cut into at least 8 sub commit. Sorry, I
acknowledge this is bad... Here are the new features:

 * dom decorator: bold, dim, underlined, inverted.
 * component mechanism
 * components
   * menu
   * toogle
This commit is contained in:
Arthur Sonzogni
2018-10-09 19:06:03 +02:00
parent dd92b89611
commit 711b71688e
63 changed files with 1590 additions and 260 deletions

View File

@@ -1,4 +1,8 @@
add_subdirectory(frame)
add_subdirectory(gauge)
add_subdirectory(menu)
add_subdirectory(menu2)
add_subdirectory(print_key_press)
add_subdirectory(separator)
add_subdirectory(vbox_hbox)
add_subdirectory(toggle)

View File

@@ -2,33 +2,47 @@
#include <iostream>
#include <thread>
#include "ftxui/core/screen.hpp"
#include "ftxui/core/dom/elements.hpp"
#include "ftxui/screen.hpp"
#include "ftxui/dom/elements.hpp"
int main(int argc, const char *argv[])
{
using namespace ftxui::dom;
auto document =
hbox(
frame(
vbox(
text(L"Line 1"),
text(L"Line 2"),
text(L"Line 3"),
frame(
vbox(
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6")
)
),
text(L"Line 7"),
text(L"Line 8"),
text(L"Line 9")
)
),
flex()
);
auto document =
hbox(
frame(hcenter(text(L" main frame ")),
vbox(
text(L"Line 1"),
text(L"Line 2"),
text(L"Line 3"),
frame(
vbox(
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6")
)
),
hbox(
frame(text(L"frame 2"),
vbox(
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6")
)
),
frame(text(L"frame 3"),
vbox(
text(L"Line 7"),
text(L"Line 8"),
text(L"Line 9")
)
)
),
text(L"footer footer footer footer footer")
)
),
flex()
);
auto screen = ftxui::Screen::TerminalOutput(document);
Render(screen, document.get());
std::cout << screen.ToString() << std::endl;

View File

@@ -2,15 +2,21 @@
#include <iostream>
#include <thread>
#include "ftxui/core/screen.hpp"
#include "ftxui/core/dom/elements.hpp"
#include "ftxui/screen.hpp"
#include "ftxui/dom/elements.hpp"
int main(int argc, const char *argv[])
{
for(float percentage = 0; percentage <= 1.0; percentage+=0.001) {
std::wstring data_downloaded =
std::to_wstring(int(percentage * 44100)) + L"/44100";
using namespace ftxui::dom;
auto document =
hbox(text(L"gauge = -"), flex(gauge(percentage)), text(L"-"));
hbox(
text(L"downloading:"),
flex(gauge(percentage)),
text(L" " + data_downloaded)
);
auto screen = ftxui::Screen(100, 1);
Render(screen, document.get());
std::cout << '\r' << screen.ToString() << std::flush;

View File

@@ -0,0 +1,4 @@
add_executable(menu_main
main.cpp
)
target_link_libraries(menu_main PRIVATE ftxui)

21
examples/menu/main.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen_interactive.hpp"
#include "ftxui/component/menu.hpp"
int main(int argc, const char *argv[])
{
ftxui::ScreenInteractive screen(30,3);
ftxui::component::Menu menu(screen.delegate());
menu.entries = {
L"entry 1",
L"entry 2",
L"entry 3"
};
menu.selected = 0;
menu.on_enter = screen.ExitLoopClosure();
screen.Loop();
}

View File

@@ -0,0 +1,4 @@
add_executable(menu2_main
main.cpp
)
target_link_libraries(menu2_main PRIVATE ftxui)

75
examples/menu2/main.cpp Normal file
View File

@@ -0,0 +1,75 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen_interactive.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/component_horizontal.hpp"
#include "ftxui/component/component_vertical.hpp"
#include "ftxui/util/string.hpp"
using namespace ftxui::component;
using namespace ftxui::dom;
class MyComponent : ComponentHorizontal {
public:
MyComponent(ftxui::component::Delegate* delegate)
: ComponentHorizontal(delegate),
left_menu(delegate->NewChild()),
right_menu(delegate->NewChild()) {
left_menu.entries = {L"0%", L"10%", L"20%", L"30%", L"40%", L"50%",
L"60%", L"70%", L"80%", L"90%"};
right_menu.entries = {L"0%", L"1%", L"2%", L"3%", L"4%", L"5%",
L"6%", L"7%", L"8%", L"9%", L"10%"};
left_menu.on_enter = [this]() { on_enter(); };
right_menu.on_enter = [this]() { on_enter(); };
Focus(&left_menu);
}
std::function<void()> on_enter = [](){};
private:
Menu left_menu;
Menu right_menu;
Element Render() override {
int sum = left_menu.selected * 10 + right_menu.selected;
return
frame(
vbox(
// -------- Top panel --------------
hbox(
// -------- Left Menu --------------
flex(
vbox(
hcenter(bold(text(L"Percentage by 10%"))),
left_menu.Render()
)
),
// -------- Right Menu --------------
flex(
vbox(
hcenter(bold(text(L"Percentage by 1%"))),
right_menu.Render()
)
),
flex()
),
separator(),
// -------- Bottom panel --------------
flex(vbox(
hbox(text(L" gauge : "), gauge(sum/100.0)),
hbox(text(L" text : "), text(to_wstring(std::to_string(sum) + " %")))
))
)
);
}
};
int main(int argc, const char *argv[])
{
ftxui::ScreenInteractive screen(60,17);
MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure();
screen.Loop();
}

View File

@@ -0,0 +1,4 @@
add_executable(print_key_press
main.cpp
)
target_link_libraries(print_key_press PRIVATE ftxui)

View File

@@ -0,0 +1,44 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen_interactive.hpp"
#include "ftxui/component/component.hpp"
#include "ftxui/util/string.hpp"
class DrawKey : public ftxui::component::Component {
public:
DrawKey(ftxui::component::Delegate* delegate)
: ftxui::component::Component(delegate) {}
ftxui::dom::Element Render() override {
using namespace ftxui::dom;
Children children;
for (size_t i = std::max(0, (int)keys.size() - 10); i < keys.size(); ++i) {
try {
std::string line = std::to_string(i) + " -> " + std::to_string(keys[i]) +
" (" + char(keys[i]) + ")";
children.push_back(text(to_wstring(line)));
} catch (...) {
std::string line = std::to_string(i) + " -> " + std::to_string(keys[i]) +
" (undefined)";
children.push_back(text(to_wstring(line)));
}
}
return vbox(std::move(children));
}
bool Event(int key) override {
keys.push_back(key);
return true;
}
private:
std::vector<int> keys;
};
int main(int argc, const char* argv[]) {
ftxui::ScreenInteractive screen(80,10);
DrawKey draw_key(screen.delegate());
screen.Loop();
}

View File

@@ -1,5 +1,5 @@
#include "ftxui/core/screen.hpp"
#include "ftxui/core/dom/elements.hpp"
#include "ftxui/screen.hpp"
#include "ftxui/dom/elements.hpp"
#include <iostream>
int main(int argc, const char *argv[])
@@ -15,7 +15,7 @@ int main(int argc, const char *argv[])
center(text(L"bottom-column"))
))
);
auto screen = ftxui::Screen::WholeTerminal();
auto screen = ftxui::Screen::TerminalFullscreen();
Render(screen, document.get());
std::cout << screen.ToString();

View File

@@ -0,0 +1,4 @@
add_executable(toogle_main
main.cpp
)
target_link_libraries(toogle_main PRIVATE ftxui)

69
examples/toggle/main.cpp Normal file
View File

@@ -0,0 +1,69 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/component/component_horizontal.hpp"
#include "ftxui/component/component_vertical.hpp"
#include "ftxui/util/string.hpp"
using namespace ftxui::component;
using namespace ftxui::dom;
class MyComponent : ComponentVertical {
public:
MyComponent(ftxui::component::Delegate* delegate)
: ComponentVertical(delegate),
toggle_1(delegate->NewChild()),
toggle_2(delegate->NewChild()),
toggle_3(delegate->NewChild()) {
toggle_1.on = L"On";
toggle_1.off = L"Off";
toggle_2.on = L"Enabled";
toggle_2.off = L"Disabled";
toggle_3.on = L"10€";
toggle_3.off = L"0€";
Focus(&toggle_1);
}
std::function<void()> on_enter = []() {};
private:
Toggle toggle_1;
Toggle toggle_2;
Toggle toggle_3;
Element Render() override {
return
vbox(
text(L"Choose your options:"),
text(L""),
hbox(text(L" * Poweroff on startup : "), toggle_1.Render()),
hbox(text(L" * Out of process : "), toggle_2.Render()),
hbox(text(L" * Price of the information : "), toggle_3.Render())
);
}
bool Event(int key) override {
if (ComponentVertical::Event(key))
return true;
if (key == 10) {
on_enter();
return true;
}
return false;
}
};
int main(int argc, const char* argv[]) {
ftxui::ScreenInteractive screen(50,5);
MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure();
screen.Loop();
}

View File

@@ -1,5 +1,5 @@
#include "ftxui/core/screen.hpp"
#include "ftxui/core/dom/elements.hpp"
#include "ftxui/screen.hpp"
#include "ftxui/dom/elements.hpp"
#include <iostream>
int main(int argc, const char *argv[])
@@ -27,7 +27,7 @@ int main(int argc, const char *argv[])
text(L"south-east")
)
);
auto screen = ftxui::Screen::WholeTerminal();
auto screen = ftxui::Screen::TerminalFullscreen();
Render(screen, document.get());
std::cout << screen.ToString();