From c36ab03920aff49af446fd22bacc71ef8944a953 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Fri, 3 Dec 2021 03:10:05 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20update=20ui?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 +++- meson.build | 4 +++- src/config.hpp | 4 ++-- src/main.cpp | 7 +++++++ src/screen_service.cpp | 22 +++++++++++++++++++++ src/screen_service.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++ src/tui.cpp | 11 ++++++----- src/view.hpp | 28 +++++++++++++++++++++++++++ 8 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 src/screen_service.cpp create mode 100644 src/screen_service.hpp create mode 100644 src/view.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f3516f..d042bc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,7 +71,9 @@ target_compile_features(project_options INTERFACE cxx_std_20) ## Target ## add_executable(${PROJECT_NAME} + src/view.hpp src/definitions.hpp + src/screen_service.hpp src/screen_service.cpp src/config.cpp src/config.hpp src/utils.cpp src/utils.hpp src/tui.cpp src/tui.hpp @@ -90,7 +92,7 @@ enable_sanitizers(project_options) include_directories(${CMAKE_SOURCE_DIR}/src) -target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options fmt::fmt ftxui::screen ftxui::dom ftxui::component nlohmann_json::nlohmann_json cpr::cpr PkgConfig::LIBNM) +target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options fmt::fmt ftxui::screen ftxui::dom ftxui::component nlohmann_json::nlohmann_json cpr::cpr) option(ENABLE_UNITY "Enable Unity builds of projects" OFF) if(ENABLE_UNITY) diff --git a/meson.build b/meson.build index a7f9c32..f17b7ee 100644 --- a/meson.build +++ b/meson.build @@ -39,7 +39,9 @@ cpr = dependency('cpr', version : ['>=1.7.0'], fallback : ['cpr', 'cpr_dep']) libnm = dependency('libnm', version : ['>=1.10.6']) src_files = files( + 'src/view.hpp', 'src/definitions.hpp', + 'src/screen_service.hpp', 'src/screen_service.cpp', 'src/config.cpp', 'src/config.hpp', 'src/utils.cpp', 'src/utils.hpp', 'src/tui.cpp', 'src/tui.hpp', @@ -97,7 +99,7 @@ add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language : executable( 'cachyos-installer', src_files, - dependencies: [fmt, ftxui, nlohmann_json, cpr, libnm], + dependencies: [fmt, ftxui, nlohmann_json, cpr], include_directories: [include_directories('src')], install: true) diff --git a/src/config.hpp b/src/config.hpp index efb8fd5..4f1f121 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -11,8 +11,8 @@ class Config final { using reference = value_type&; using const_reference = const value_type&; - Config() = default; - virtual ~Config() = default; + Config() noexcept = default; + virtual ~Config() noexcept = default; static bool initialize() noexcept; static Config* instance(); diff --git a/src/main.cpp b/src/main.cpp index 01d177f..f997231 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include "config.hpp" #include "definitions.hpp" +#include "screen_service.hpp" #include "tui.hpp" #include "utils.hpp" @@ -31,5 +32,11 @@ int main() { return 0; } + if (!tui::screen_service::initialize()) { + return 1; + } + + // auto app_router = std::make_shared(tui::screen_service::instance()); + // app_router->navigate("", std::any()); tui::init(); } diff --git a/src/screen_service.cpp b/src/screen_service.cpp new file mode 100644 index 0000000..b359668 --- /dev/null +++ b/src/screen_service.cpp @@ -0,0 +1,22 @@ +#include "screen_service.hpp" +#include "definitions.hpp" + +#include + +namespace tui { +static std::unique_ptr s_screen = nullptr; + +bool screen_service::initialize() noexcept { + if (s_screen != nullptr) { + error("You should only initialize it once!\n"); + return false; + } + s_screen = std::make_unique(); + return s_screen.get(); +} + +auto screen_service::instance() -> screen_service* { + return s_screen.get(); +} + +} // namespace tui diff --git a/src/screen_service.hpp b/src/screen_service.hpp new file mode 100644 index 0000000..8adde63 --- /dev/null +++ b/src/screen_service.hpp @@ -0,0 +1,44 @@ +// taken from https://github.com/adrianoviana87/ltuiny +#ifndef SCREEN_SERVICE_HPP +#define SCREEN_SERVICE_HPP + +#include "view.hpp" + +/* clang-format off */ +#include +#include +/* clang-format on */ + +namespace tui { +class screen_service final { + public: + using value_type = ftxui::ScreenInteractive; + using reference = value_type&; + using const_reference = const value_type&; + + screen_service() noexcept = default; + virtual ~screen_service() noexcept = default; + + static bool initialize() noexcept; + static screen_service* instance(); + + /* clang-format off */ + + // Element access. + auto data() noexcept -> reference + { return m_screen; } + auto data() const noexcept -> const_reference + { return m_screen; } + + /* clang-format on */ + + screen_service(const screen_service&) noexcept = delete; + screen_service& operator=(const screen_service&) noexcept = delete; + + private: + value_type m_screen = ftxui::ScreenInteractive::Fullscreen(); + // std::shared_ptr m_current_view{}; +}; +} // namespace tui + +#endif // SCREEN_SERVICE_HPP diff --git a/src/tui.cpp b/src/tui.cpp index ed7af02..8ea83ca 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -1,6 +1,7 @@ #include "tui.hpp" #include "config.hpp" #include "definitions.hpp" +#include "screen_service.hpp" #include "utils.hpp" /* clang-format off */ @@ -81,8 +82,8 @@ ftxui::Element multiline_text(const std::vector& lines) { // Simple code to show devices / partitions. void show_devices() noexcept { - auto screen = ScreenInteractive::Fullscreen(); - auto lsblk = utils::exec("lsblk -o NAME,MODEL,TYPE,FSTYPE,SIZE,MOUNTPOINT | grep \"disk\\|part\\|lvm\\|crypt\\|NAME\\|MODEL\\|TYPE\\|FSTYPE\\|SIZE\\|MOUNTPOINT\""); + auto& screen = tui::screen_service::instance()->data(); + auto lsblk = utils::exec("lsblk -o NAME,MODEL,TYPE,FSTYPE,SIZE,MOUNTPOINT | grep \"disk\\|part\\|lvm\\|crypt\\|NAME\\|MODEL\\|TYPE\\|FSTYPE\\|SIZE\\|MOUNTPOINT\""); /* clang-format off */ auto button_option = ButtonOption(); @@ -109,7 +110,7 @@ void select_device() noexcept { 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(); + auto& screen = tui::screen_service::instance()->data(); std::int32_t selected{}; auto menu = Menu(&devices_list, &selected); auto content = Renderer(menu, [&] { @@ -117,7 +118,7 @@ void select_device() noexcept { }); auto ok_callback = [&] { - auto src = devices_list[selected]; + auto src = devices_list[static_cast(selected)]; const auto& lines = utils::make_multiline(src, " "); config_data["DEVICE"] = lines[0]; }; @@ -141,7 +142,7 @@ void select_device() noexcept { } void init() noexcept { - auto screen = ScreenInteractive::Fullscreen(); + auto& screen = tui::screen_service::instance()->data(); auto ok_callback = [=] { info("ok\n"); }; auto container = controls_widget({"OK", "Quit"}, {ok_callback, screen.ExitLoopClosure()}); diff --git a/src/view.hpp b/src/view.hpp new file mode 100644 index 0000000..e4cdda8 --- /dev/null +++ b/src/view.hpp @@ -0,0 +1,28 @@ +// taken from https://github.com/adrianoviana87/ltuiny +#ifndef VIEW_HPP +#define VIEW_HPP + +/* clang-format off */ +#include +#include +/* clang-format on */ + +namespace tui { +class view : public ftxui::Component { + public: + view() = default; + virtual ~view() = default; + + void initialize() { initialize_ui(); }; + void set_on_close(std::function&& val) { on_close = std::move(val); } + + protected: + void close() { on_close(); } + virtual void initialize_ui() = 0; + + private: + std::function on_close; +}; +} // namespace tui + +#endif // VIEW_HPP