From 279b6539b78a25291578741629ebcbb09f435b45 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Thu, 2 Dec 2021 00:03:46 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20enable=20devenv=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `centered_widget` element which allows to place any widget in the center --- cmake/StandardProjectSettings.cmake | 6 +++++ meson.build | 19 +++++++++++---- meson_options.txt | 1 + src/main.cpp | 2 -- src/tui.cpp | 37 ++++++++++++++++------------- src/tui.hpp | 3 +++ src/utils.cpp | 10 ++++++-- 7 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 meson_options.txt diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake index 4c1d842..c11f43a 100644 --- a/cmake/StandardProjectSettings.cmake +++ b/cmake/StandardProjectSettings.cmake @@ -44,3 +44,9 @@ endif() if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-D_GLIBCXX_ASSERTIONS) endif() + +# Enables dev environment. +option(ENABLE_DEVENV "Enable dev environment" ON) +if(NOT ENABLE_DEVENV) + add_definitions(-DNDEVENV) +endif() diff --git a/meson.build b/meson.build index 0ccfd5d..b6a07de 100644 --- a/meson.build +++ b/meson.build @@ -9,6 +9,7 @@ project('cachyos-installer', 'cpp', 'b_ndebug=if-release']) is_debug_build = get_option('buildtype').startswith('debug') +is_dev_environment = get_option('devenv') cc = meson.get_compiler('cpp') if cc.get_id() == 'clang' specific_cc_flags = [ @@ -22,6 +23,14 @@ if cc.get_id() == 'clang' add_global_link_arguments(cc.get_supported_link_arguments(specific_link_flags), language : 'cpp') endif +if is_debug_build + add_global_arguments('-D_GLIBCXX_ASSERTIONS', language : 'cpp') +endif + +if not is_dev_environment + add_global_arguments('-DNDEVENV', language : 'cpp') +endif + # Common dependencies fmt = dependency('fmt', version : ['>=8.0.0'], fallback : ['fmt', 'fmt_dep']) ftxui = dependency('ftxui', modules : ['ftxui::screen', 'ftxui::dom', 'ftxui::component'], fallback : ['ftxui', 'ftxui_dep']) @@ -66,11 +75,7 @@ if cc.get_id() == 'gcc' ] endif -if is_debug_build - possible_cc_flags += [ - '-D_GLIBCXX_ASSERTIONS', - ] -else +if not is_debug_build if cc.get_id() == 'gcc' possible_cc_flags += [ '-flto', @@ -81,6 +86,10 @@ else '-flto=thin', ] endif + + possible_cc_flags += ['-fdata-sections', '-ffunction-sections'] + possible_link_flags = ['-Wl,--gc-sections'] + add_project_link_arguments(cc.get_supported_link_arguments(possible_link_flags), language : 'cpp') endif add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language : 'cpp') diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..5591e2e --- /dev/null +++ b/meson_options.txt @@ -0,0 +1 @@ +option('devenv', type: 'boolean', value: true, description: 'enable dev environment') diff --git a/src/main.cpp b/src/main.cpp index e1c3e9f..f282cf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,13 +14,11 @@ int main() { utils::exec("setterm -blank 0 -powersave off"); } -#ifdef NDEBUG if (!utils::check_root()) { std::this_thread::sleep_for(std::chrono::seconds(3)); utils::clear_screen(); return 1; } -#endif if (!Config::initialize()) { return 1; diff --git a/src/tui.cpp b/src/tui.cpp index 7d583a1..b4fb678 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -11,6 +11,26 @@ using namespace ftxui; namespace tui { + +ftxui::Element centered_widget(ftxui::Component& container, const std::string_view& title, const ftxui::Element& widget) { + return vbox({ + // -------- Title -------------- + text(title.data()) | bold, + filler(), + // -------- Center Menu -------------- + hbox({ + filler(), + border(vbox({ + widget, + separator(), + container->Render() | hcenter | size(HEIGHT, LESS_THAN, 3) | size(WIDTH, GREATER_THAN, 25), + })), + filler(), + }) | center, + filler(), + }); +} + void init() noexcept { auto screen = ScreenInteractive::Fullscreen(); @@ -28,22 +48,7 @@ void init() noexcept { }); auto renderer = Renderer(container, [&] { - return vbox({ - // -------- Title -------------- - text("New CLI Installer") | bold, - filler(), - // -------- Center Menu -------------- - hbox({ - filler(), - border(vbox({ - text("TODO!!") | size(HEIGHT, GREATER_THAN, 5), - separator(), - container->Render() | hcenter | size(HEIGHT, LESS_THAN, 3) | size(WIDTH, GREATER_THAN, 25), - })), - filler(), - }) | center, - filler(), - }); + return tui::centered_widget(container, "New CLI Installer", text("TODO!!") | size(HEIGHT, GREATER_THAN, 5)); }); screen.Loop(renderer); diff --git a/src/tui.hpp b/src/tui.hpp index e9df554..95c3c53 100644 --- a/src/tui.hpp +++ b/src/tui.hpp @@ -1,7 +1,10 @@ #ifndef TUI_HPP #define TUI_HPP +#include + namespace tui { +ftxui::Element centered_widget(ftxui::Component& container, const std::string_view& title, const ftxui::Element& widget); void init() noexcept; } // namespace tui diff --git a/src/utils.cpp b/src/utils.cpp index aa9a12c..7956280 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -20,17 +20,23 @@ namespace utils { static constexpr std::int32_t CONNECTION_TIMEOUT = 15; bool is_connected() noexcept { +#ifdef NDEVENV /* clang-format off */ auto r = cpr::Get(cpr::Url{"https://www.google.com"}, cpr::Timeout{1000}); - - info("{}\n", r.status_code); /* clang-format on */ return cpr::status::is_success(static_cast(r.status_code)) || cpr::status::is_redirect(static_cast(r.status_code)); +#else + return true; +#endif } bool check_root() noexcept { +#ifdef NDEVENV return (utils::exec("whoami") == "root\n"); +#else + return true; +#endif } void clear_screen() noexcept {