👷 update ui

This commit is contained in:
Vladislav Nepogodin 2021-12-03 03:10:05 +04:00
parent d0bc5f469a
commit c36ab03920
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
8 changed files with 115 additions and 9 deletions

View File

@ -71,7 +71,9 @@ target_compile_features(project_options INTERFACE cxx_std_20)
## Target ## Target
## ##
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
src/view.hpp
src/definitions.hpp src/definitions.hpp
src/screen_service.hpp src/screen_service.cpp
src/config.cpp src/config.hpp src/config.cpp src/config.hpp
src/utils.cpp src/utils.hpp src/utils.cpp src/utils.hpp
src/tui.cpp src/tui.hpp src/tui.cpp src/tui.hpp
@ -90,7 +92,7 @@ enable_sanitizers(project_options)
include_directories(${CMAKE_SOURCE_DIR}/src) 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) option(ENABLE_UNITY "Enable Unity builds of projects" OFF)
if(ENABLE_UNITY) if(ENABLE_UNITY)

View File

@ -39,7 +39,9 @@ cpr = dependency('cpr', version : ['>=1.7.0'], fallback : ['cpr', 'cpr_dep'])
libnm = dependency('libnm', version : ['>=1.10.6']) libnm = dependency('libnm', version : ['>=1.10.6'])
src_files = files( src_files = files(
'src/view.hpp',
'src/definitions.hpp', 'src/definitions.hpp',
'src/screen_service.hpp', 'src/screen_service.cpp',
'src/config.cpp', 'src/config.hpp', 'src/config.cpp', 'src/config.hpp',
'src/utils.cpp', 'src/utils.hpp', 'src/utils.cpp', 'src/utils.hpp',
'src/tui.cpp', 'src/tui.hpp', 'src/tui.cpp', 'src/tui.hpp',
@ -97,7 +99,7 @@ add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language :
executable( executable(
'cachyos-installer', 'cachyos-installer',
src_files, src_files,
dependencies: [fmt, ftxui, nlohmann_json, cpr, libnm], dependencies: [fmt, ftxui, nlohmann_json, cpr],
include_directories: [include_directories('src')], include_directories: [include_directories('src')],
install: true) install: true)

View File

@ -11,8 +11,8 @@ class Config final {
using reference = value_type&; using reference = value_type&;
using const_reference = const value_type&; using const_reference = const value_type&;
Config() = default; Config() noexcept = default;
virtual ~Config() = default; virtual ~Config() noexcept = default;
static bool initialize() noexcept; static bool initialize() noexcept;
static Config* instance(); static Config* instance();

View File

@ -1,5 +1,6 @@
#include "config.hpp" #include "config.hpp"
#include "definitions.hpp" #include "definitions.hpp"
#include "screen_service.hpp"
#include "tui.hpp" #include "tui.hpp"
#include "utils.hpp" #include "utils.hpp"
@ -31,5 +32,11 @@ int main() {
return 0; return 0;
} }
if (!tui::screen_service::initialize()) {
return 1;
}
// auto app_router = std::make_shared<router>(tui::screen_service::instance());
// app_router->navigate("", std::any());
tui::init(); tui::init();
} }

22
src/screen_service.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "screen_service.hpp"
#include "definitions.hpp"
#include <memory>
namespace tui {
static std::unique_ptr<screen_service> 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<screen_service>();
return s_screen.get();
}
auto screen_service::instance() -> screen_service* {
return s_screen.get();
}
} // namespace tui

44
src/screen_service.hpp Normal file
View File

@ -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 <memory>
#include <ftxui/component/screen_interactive.hpp>
/* 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<tui::view> m_current_view{};
};
} // namespace tui
#endif // SCREEN_SERVICE_HPP

View File

@ -1,6 +1,7 @@
#include "tui.hpp" #include "tui.hpp"
#include "config.hpp" #include "config.hpp"
#include "definitions.hpp" #include "definitions.hpp"
#include "screen_service.hpp"
#include "utils.hpp" #include "utils.hpp"
/* clang-format off */ /* clang-format off */
@ -81,8 +82,8 @@ ftxui::Element multiline_text(const std::vector<std::string>& lines) {
// Simple code to show devices / partitions. // Simple code to show devices / partitions.
void show_devices() noexcept { void show_devices() noexcept {
auto screen = ScreenInteractive::Fullscreen(); 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\""); 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 */ /* clang-format off */
auto button_option = ButtonOption(); 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"); 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); const auto& devices_list = utils::make_multiline(devices);
auto screen = ScreenInteractive::Fullscreen(); auto& screen = tui::screen_service::instance()->data();
std::int32_t selected{}; std::int32_t selected{};
auto menu = Menu(&devices_list, &selected); auto menu = Menu(&devices_list, &selected);
auto content = Renderer(menu, [&] { auto content = Renderer(menu, [&] {
@ -117,7 +118,7 @@ void select_device() noexcept {
}); });
auto ok_callback = [&] { auto ok_callback = [&] {
auto src = devices_list[selected]; auto src = devices_list[static_cast<std::size_t>(selected)];
const auto& lines = utils::make_multiline(src, " "); const auto& lines = utils::make_multiline(src, " ");
config_data["DEVICE"] = lines[0]; config_data["DEVICE"] = lines[0];
}; };
@ -141,7 +142,7 @@ void select_device() noexcept {
} }
void init() noexcept { void init() noexcept {
auto screen = ScreenInteractive::Fullscreen(); auto& screen = tui::screen_service::instance()->data();
auto ok_callback = [=] { info("ok\n"); }; auto ok_callback = [=] { info("ok\n"); };
auto container = controls_widget({"OK", "Quit"}, {ok_callback, screen.ExitLoopClosure()}); auto container = controls_widget({"OK", "Quit"}, {ok_callback, screen.ExitLoopClosure()});

28
src/view.hpp Normal file
View File

@ -0,0 +1,28 @@
// taken from https://github.com/adrianoviana87/ltuiny
#ifndef VIEW_HPP
#define VIEW_HPP
/* clang-format off */
#include <functional>
#include <ftxui/component/component.hpp>
/* 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<void()>&& val) { on_close = std::move(val); }
protected:
void close() { on_close(); }
virtual void initialize_ui() = 0;
private:
std::function<void()> on_close;
};
} // namespace tui
#endif // VIEW_HPP