diff --git a/src/main.cpp b/src/main.cpp index 8554cef..6999fbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include "utils.hpp" #include -#include #include #include @@ -13,22 +12,6 @@ using namespace ftxui; -static constexpr int32_t CONNECTION_TIMEOUT = 15; - -void show_iwctl() { - info("\nInstructions to connect to wifi using iwctl:\n"); - info("1 - To find your wifi device name (ex: wlan0) type `device list`\n"); - info("2 - type `station wlan0 scan`, and wait couple seconds\n"); - info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n"); - info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n"); - info("5 - type `station wlan0 show` (status should be connected)\n"); - info("6 - type `exit`\n"); - while (utils::prompt_char("Press a key to continue...", CYAN)) { - utils::exec("iwctl"); - break; - } -} - int main() { const auto& tty = utils::exec("tty"); const std::regex tty_regex("/dev/tty[0-9]*"); @@ -46,39 +29,9 @@ int main() { utils::id_system(); - bool connected; - - if (!(connected = utils::is_connected())) { - warning("An active network connection could not be detected, waiting 15 seconds ...\n"); - - int32_t time_waited = 0; - - while (!(connected = utils::is_connected())) { - std::this_thread::sleep_for(std::chrono::seconds(1)); - - if (time_waited++ >= CONNECTION_TIMEOUT) { - break; - } - } - - if (!connected) { - char type = '\0'; - - while (utils::prompt_char("An active network connection could not be detected, do you want to connect using wifi? [y/n]", RED, &type)) { - if (type != 'n') { - show_iwctl(); - } - - break; - } - - connected = utils::is_connected(); - } - - if (!connected) { - error("An active network connection could not be detected, please connect and restart the installer."); - return 0; - } + if (!utils::handle_connection()) { + error("An active network connection could not be detected, please connect and restart the installer."); + return 0; } auto screen = ScreenInteractive::Fullscreen(); diff --git a/src/utils.cpp b/src/utils.cpp index 8de92fb..6f15b3a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -7,6 +7,7 @@ #include #include +#include namespace fs = std::filesystem; @@ -14,6 +15,8 @@ static std::string_view H_INIT{"openrc"}; static std::string_view SYSTEM{"BIOS"}; static std::string_view NW_CMD{}; +static constexpr int32_t CONNECTION_TIMEOUT = 15; + namespace utils { bool is_connected() noexcept { return gethostbyname("google.com"); @@ -102,4 +105,53 @@ void id_system() noexcept { NW_CMD = "nmtui"; } +bool handle_connection() noexcept { + bool connected; + + if (!(connected = utils::is_connected())) { + warning("An active network connection could not be detected, waiting 15 seconds ...\n"); + + int32_t time_waited = 0; + + while (!(connected = utils::is_connected())) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + + if (time_waited++ >= CONNECTION_TIMEOUT) { + break; + } + } + + if (!connected) { + char type = '\0'; + + while (utils::prompt_char("An active network connection could not be detected, do you want to connect using wifi? [y/n]", RED, &type)) { + if (type != 'n') { + show_iwctl(); + } + + break; + } + + connected = utils::is_connected(); + } + } + + return connected; +} + +void show_iwctl() noexcept { + info("\nInstructions to connect to wifi using iwctl:\n"); + info("1 - To find your wifi device name (ex: wlan0) type `device list`\n"); + info("2 - type `station wlan0 scan`, and wait couple seconds\n"); + info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n"); + info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n"); + info("5 - type `station wlan0 show` (status should be connected)\n"); + info("6 - type `exit`\n"); + + while (utils::prompt_char("Press a key to continue...", CYAN)) { + utils::exec("iwctl"); + break; + } +} + } // namespace utils diff --git a/src/utils.hpp b/src/utils.hpp index 3d5f15e..3bff0db 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -14,6 +14,8 @@ void clear_screen() noexcept; auto exec(const std::string_view& command) noexcept -> std::string; [[nodiscard]] bool check_root() noexcept; void id_system() noexcept; +bool handle_connection() noexcept; +void show_iwctl() noexcept; } // namespace utils #endif // UTILS_HPP