mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-15 23:48:15 +08:00
Add Blink. Refactor examples.
This commit is contained in:
6
examples/component/CMakeLists.txt
Normal file
6
examples/component/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
example(color)
|
||||
example(gauge)
|
||||
example(input)
|
||||
example(menu)
|
||||
example(menu2)
|
||||
example(toggle)
|
58
examples/component/color.cpp
Normal file
58
examples/component/color.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
using namespace ftxui;
|
||||
using namespace ftxui::dom;
|
||||
auto document =
|
||||
hbox(
|
||||
vbox(
|
||||
color(Color::Default, text(L"Default")),
|
||||
color(Color::Black, text(L"Black")),
|
||||
color(Color::GrayDark, text(L"GrayDark")),
|
||||
color(Color::GrayLight, text(L"GrayLight")),
|
||||
color(Color::White, text(L"White")),
|
||||
color(Color::Blue, text(L"Blue")),
|
||||
color(Color::BlueLight, text(L"BlueLight")),
|
||||
color(Color::Cyan, text(L"Cyan")),
|
||||
color(Color::CyanLight, text(L"CyanLight")),
|
||||
color(Color::Green, text(L"Green")),
|
||||
color(Color::GreenLight, text(L"GreenLight")),
|
||||
color(Color::Magenta, text(L"Magenta")),
|
||||
color(Color::MagentaLight, text(L"MagentaLight")),
|
||||
color(Color::Red, text(L"Red")),
|
||||
color(Color::RedLight, text(L"RedLight")),
|
||||
color(Color::Yellow, text(L"Yellow")),
|
||||
color(Color::YellowLight, text(L"YellowLight"))
|
||||
),
|
||||
vbox(
|
||||
bgcolor(Color::Default, text(L"Default")),
|
||||
bgcolor(Color::Black, text(L"Black")),
|
||||
bgcolor(Color::GrayDark, text(L"GrayDark")),
|
||||
bgcolor(Color::GrayLight, text(L"GrayLight")),
|
||||
bgcolor(Color::White, text(L"White")),
|
||||
bgcolor(Color::Blue, text(L"Blue")),
|
||||
bgcolor(Color::BlueLight, text(L"BlueLight")),
|
||||
bgcolor(Color::Cyan, text(L"Cyan")),
|
||||
bgcolor(Color::CyanLight, text(L"CyanLight")),
|
||||
bgcolor(Color::Green, text(L"Green")),
|
||||
bgcolor(Color::GreenLight, text(L"GreenLight")),
|
||||
bgcolor(Color::Magenta, text(L"Magenta")),
|
||||
bgcolor(Color::MagentaLight, text(L"MagentaLight")),
|
||||
bgcolor(Color::Red, text(L"Red")),
|
||||
bgcolor(Color::RedLight, text(L"RedLight")),
|
||||
bgcolor(Color::Yellow, text(L"Yellow")),
|
||||
bgcolor(Color::YellowLight, text(L"YellowLight"))
|
||||
),
|
||||
flex()
|
||||
);
|
||||
|
||||
auto screen = ftxui::Screen::TerminalOutput(document);
|
||||
Render(screen, document.get());
|
||||
|
||||
std::cout << screen.ToString();
|
||||
|
||||
return 0;
|
||||
}
|
28
examples/component/gauge.cpp
Normal file
28
examples/component/gauge.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#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"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;
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
std::this_thread::sleep_for(0.01s);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
52
examples/component/input.cpp
Normal file
52
examples/component/input.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "ftxui/screen_interactive.hpp"
|
||||
#include "ftxui/component/input.hpp"
|
||||
#include "ftxui/component/component_vertical.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
|
||||
using namespace ftxui::component;
|
||||
using namespace ftxui::dom;
|
||||
using namespace ftxui;
|
||||
|
||||
class MyComponent : ComponentVertical {
|
||||
public:
|
||||
MyComponent(ftxui::component::Delegate* delegate)
|
||||
: ComponentVertical(delegate),
|
||||
input_1(delegate->NewChild()),
|
||||
input_2(delegate->NewChild()),
|
||||
input_3(delegate->NewChild()) {
|
||||
|
||||
input_1.placeholder = L"input1";
|
||||
input_2.placeholder = L"input2";
|
||||
input_3.placeholder = L"input3";
|
||||
Focus(&input_1);
|
||||
}
|
||||
|
||||
std::function<void()> on_enter = []() {};
|
||||
|
||||
private:
|
||||
Input input_1;
|
||||
Input input_2;
|
||||
Input input_3;
|
||||
|
||||
Element Render() override {
|
||||
return
|
||||
frame(
|
||||
vbox(
|
||||
hbox(text(L" input_1 : "), input_1.Render()),
|
||||
hbox(text(L" input_2 : "), input_2.Render()),
|
||||
hbox(text(L" input_3 : "), input_3.Render())
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
ftxui::ScreenInteractive screen(60, 5);
|
||||
MyComponent component(screen.delegate());
|
||||
component.on_enter = screen.ExitLoopClosure();
|
||||
screen.Loop();
|
||||
}
|
21
examples/component/menu.cpp
Normal file
21
examples/component/menu.cpp
Normal 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();
|
||||
}
|
77
examples/component/menu2.cpp
Normal file
77
examples/component/menu2.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#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%"))),
|
||||
separator(),
|
||||
left_menu.Render()
|
||||
)
|
||||
),
|
||||
// -------- Right Menu --------------
|
||||
flex(
|
||||
vbox(
|
||||
hcenter(bold(text(L"Percentage by 1%"))),
|
||||
separator(),
|
||||
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();
|
||||
}
|
70
examples/component/toggle.cpp
Normal file
70
examples/component/toggle.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#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;
|
||||
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 OnEvent(Event event) override {
|
||||
if (ComponentVertical::OnEvent(event))
|
||||
return true;
|
||||
|
||||
if (event == Event::Return) {
|
||||
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();
|
||||
}
|
Reference in New Issue
Block a user