From b1aa7a8c625d09a138fbad89a5467ae0a50cc1e2 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Fri, 3 Dec 2021 00:45:56 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20add=20device=20selection=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tui.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/src/tui.cpp b/src/tui.cpp index bdecf4c..c6a0a86 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -1,4 +1,5 @@ #include "tui.hpp" +#include "config.hpp" #include "definitions.hpp" #include "utils.hpp" @@ -36,6 +37,40 @@ ftxui::Element centered_widget(ftxui::Component& container, const std::string_vi }); } +ftxui::Component controls_widget(const std::array&& titles, const std::array, 2>&& callbacks) { + /* clang-format off */ + auto button_option = ButtonOption(); + button_option.border = false; + auto button_ok = Button(titles[0].data(), callbacks[0], &button_option); + auto button_quit = Button(titles[1].data(), callbacks[1], &button_option); + /* clang-format on */ + + auto container = Container::Horizontal({ + button_ok, + Renderer([] { return filler() | size(WIDTH, GREATER_THAN, 3); }), + button_quit, + }); + + return container; +} + +ftxui::Element centered_interative_multi(const std::string_view& title, ftxui::Component& widgets) { + return vbox({ + // -------- Title -------------- + text(title.data()) | bold, + filler(), + // -------- Center Menu -------------- + hbox({ + filler(), + border(vbox({ + widgets->Render(), + })), + filler(), + }) | center, + filler(), + }); +} + ftxui::Element multiline_text(const std::vector& lines) { Elements multiline; @@ -66,22 +101,46 @@ void show_devices() noexcept { screen.Loop(renderer); } -void init() noexcept { +// This function does not assume that the formatted device is the Root installation device as +// more than one device may be formatted. Root is set in the mount_partitions function. +void select_device() noexcept { + auto* config_instance = Config::instance(); + auto& config_data = config_instance->data(); + auto devices = utils::exec("lsblk -lno NAME,SIZE,TYPE | grep 'disk' | awk '{print \"/dev/\" $1 \" \" $2}' | sort -u"); + const auto& devices_list = utils::make_multiline(devices); + auto screen = ScreenInteractive::Fullscreen(); - - /* clang-format off */ - auto button_option = ButtonOption(); - button_option.border = false; - auto button_ok = Button("OK", [=] { info("ok\n"); }, &button_option); - auto button_quit = Button("Quit", screen.ExitLoopClosure(), &button_option); - /* clang-format on */ - - auto container = Container::Horizontal({ - button_ok, - Renderer([] { return filler() | size(WIDTH, GREATER_THAN, 3); }), - button_quit, + std::int32_t selected{}; + auto menu = Menu(&devices_list, &selected); + auto content = Renderer(menu, [&] { + return menu->Render() | center | size(HEIGHT, GREATER_THAN, 10) | size(WIDTH, GREATER_THAN, 40); }); + auto ok_callback = [&] { config_data["DEVICE"] = devices_list[selected]; }; + auto controls_container = controls_widget({"OK", "Cancel"}, {ok_callback, screen.ExitLoopClosure()}); + + auto controls = Renderer(controls_container, [&] { + return controls_container->Render() | hcenter | size(HEIGHT, LESS_THAN, 3) | size(WIDTH, GREATER_THAN, 25); + }); + + auto global = Container::Vertical({ + content, + Renderer([] { return separator(); }), + controls, + }); + + auto renderer = Renderer(global, [&] { + return tui::centered_interative_multi("New CLI Installer", global); + }); + + screen.Loop(renderer); +} + +void init() noexcept { + auto screen = ScreenInteractive::Fullscreen(); + auto ok_callback = [=] { info("ok\n"); }; + auto container = controls_widget({"OK", "Quit"}, {ok_callback, screen.ExitLoopClosure()}); + auto renderer = Renderer(container, [&] { return tui::centered_widget(container, "New CLI Installer", text("TODO!!") | size(HEIGHT, GREATER_THAN, 5)); });