👷 update devices page

This commit is contained in:
Vladislav Nepogodin 2021-12-02 14:52:18 +04:00
parent 12d4a06b52
commit 88c531af14
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
3 changed files with 60 additions and 9 deletions

View File

@ -3,6 +3,7 @@
#include "utils.hpp" #include "utils.hpp"
/* clang-format off */ /* clang-format off */
#include <algorithm> // for transform
#include <memory> // for __shared_ptr_access #include <memory> // for __shared_ptr_access
#include <string> // for basic_string #include <string> // for basic_string
#include <ftxui/component/captured_mouse.hpp> // for ftxui #include <ftxui/component/captured_mouse.hpp> // for ftxui
@ -35,10 +36,18 @@ ftxui::Element centered_widget(ftxui::Component& container, const std::string_vi
}); });
} }
ftxui::Element multiline_text(const std::vector<std::string>& lines) {
Elements multiline;
std::transform(lines.cbegin(), lines.cend(), std::back_inserter(multiline),
[=](const std::string& line) -> Element { return text(line); });
return vbox(std::move(multiline)) | frame;
}
// Simple code to show devices / partitions. // Simple code to show devices / partitions.
void show_devices() { void show_devices() noexcept {
auto screen = ScreenInteractive::Fullscreen(); auto screen = ScreenInteractive::Fullscreen();
const 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();
@ -51,7 +60,7 @@ void show_devices() {
}); });
auto renderer = Renderer(container, [&] { auto renderer = Renderer(container, [&] {
return tui::centered_widget(container, "New CLI Installer", text(lsblk.data()) | size(HEIGHT, GREATER_THAN, 5)); return tui::centered_widget(container, "New CLI Installer", multiline_text(utils::make_multiline(lsblk)) | size(HEIGHT, GREATER_THAN, 5));
}); });
screen.Loop(renderer); screen.Loop(renderer);

View File

@ -1,5 +1,6 @@
#include "utils.hpp" #include "utils.hpp"
#include "config.hpp" #include "config.hpp"
#include "definitions.hpp"
#include <array> // for array #include <array> // for array
#include <chrono> // for filesystem, seconds #include <chrono> // for filesystem, seconds
@ -39,7 +40,7 @@ bool is_connected() noexcept {
bool check_root() noexcept { bool check_root() noexcept {
#ifdef NDEVENV #ifdef NDEVENV
return (utils::exec("whoami") == "root\n"); return (utils::exec("whoami") == "root");
#else #else
return true; return true;
#endif #endif
@ -79,6 +80,10 @@ std::string exec(const std::string_view& command, bool capture_output) noexcept
} }
} }
if (result.ends_with('\n')) {
result.pop_back();
}
pclose(pipe); pclose(pipe);
return result; return result;
@ -105,13 +110,48 @@ bool prompt_char(const char* prompt, const char* color, char* read) noexcept {
return false; return false;
} }
auto make_multiline(std::string& str) noexcept -> std::vector<std::string> {
static constexpr std::string_view delim{"\n"};
std::vector<std::string> lines{};
std::size_t start{};
std::size_t end = str.find(delim);
while (end != std::string::npos) {
lines.push_back(str.substr(start, end - start));
start = end + delim.size();
end = str.find(delim, start);
}
lines.push_back(str.substr(start, end - start));
return lines;
}
// Unmount partitions.
void umount_partitions() noexcept {
auto* config_instance = Config::instance();
auto& config_data = config_instance->data();
auto mount_info = utils::exec(fmt::format("mount | grep \"{}\" | {}", config_data["MOUNTPOINT"], "awk \'{print $3}\' | sort -r"));
#ifdef NDEVENV
utils::exec("swapoff -a");
#endif
const auto& lines = utils::make_multiline(mount_info);
for (const auto& line : lines) {
#ifdef NDEVENV
umount(line.c_str());
#else
output("{}\n", line);
#endif
}
}
void id_system() noexcept { void id_system() noexcept {
auto* config_instance = Config::instance(); auto* config_instance = Config::instance();
auto& config_data = config_instance->data(); auto& config_data = config_instance->data();
// Apple System Detection // Apple System Detection
const auto& sys_vendor = utils::exec("cat /sys/class/dmi/id/sys_vendor"); const auto& sys_vendor = utils::exec("cat /sys/class/dmi/id/sys_vendor");
if ((sys_vendor == "Apple Inc.\n") || (sys_vendor == "Apple Computer, Inc.\n")) if ((sys_vendor == "Apple Inc.") || (sys_vendor == "Apple Computer, Inc."))
utils::exec("modprobe -r -q efivars || true"); // if MAC utils::exec("modprobe -r -q efivars || true"); // if MAC
else else
utils::exec("modprobe -q efivarfs"); // all others utils::exec("modprobe -q efivarfs"); // all others
@ -121,7 +161,7 @@ void id_system() noexcept {
if (fs::exists(efi_path) && fs::is_directory(efi_path)) { if (fs::exists(efi_path) && fs::is_directory(efi_path)) {
// Mount efivarfs if it is not already mounted // Mount efivarfs if it is not already mounted
const auto& mount_out = utils::exec("mount | grep /sys/firmware/efi/efivars"); const auto& mount_out = utils::exec("mount | grep /sys/firmware/efi/efivars");
if (mount_out == "\n") { if (mount_out.empty()) {
if (mount("efivarfs", "/sys/firmware/efi/efivars", "efivarfs", 0, "") != 0) { if (mount("efivarfs", "/sys/firmware/efi/efivars", "efivarfs", 0, "") != 0) {
perror("utils::id_system"); perror("utils::id_system");
exit(1); exit(1);
@ -132,11 +172,11 @@ void id_system() noexcept {
// init system // init system
const auto& init_sys = utils::exec("cat /proc/1/comm"); const auto& init_sys = utils::exec("cat /proc/1/comm");
if (init_sys == "systemd\n") if (init_sys == "systemd")
config_data["H_INIT"] = "systemd"; config_data["H_INIT"] = "systemd";
// TODO: Test which nw-client is available, including if the service according to $H_INIT is running // TODO: Test which nw-client is available, including if the service according to $H_INIT is running
if (config_data["H_INIT"] == "systemd" && utils::exec("systemctl is-active NetworkManager") == "active\n") if (config_data["H_INIT"] == "systemd" && utils::exec("systemctl is-active NetworkManager") == "active")
config_data["NW_CMD"] = "nmtui"; config_data["NW_CMD"] = "nmtui";
} }

View File

@ -5,12 +5,14 @@
#include <string> // for string #include <string> // for string
#include <string_view> // for string_view #include <string_view> // for string_view
#include <vector> // for vector
namespace utils { namespace utils {
void print_banner() noexcept; void print_banner() noexcept;
[[nodiscard]] bool is_connected() noexcept; [[nodiscard]] bool is_connected() noexcept;
bool prompt_char(const char* prompt, const char* color = RESET, char* read = nullptr) noexcept; bool prompt_char(const char* prompt, const char* color = RESET, char* read = nullptr) noexcept;
void clear_screen() noexcept; void clear_screen() noexcept;
[[nodiscard]] auto make_multiline(std::string& str) noexcept -> std::vector<std::string>;
auto exec(const std::string_view& command, bool capture_output = true) noexcept -> std::string; auto exec(const std::string_view& command, bool capture_output = true) noexcept -> std::string;
[[nodiscard]] bool check_root() noexcept; [[nodiscard]] bool check_root() noexcept;