🚧 chwd: fix logic

This commit is contained in:
Vladislav Nepogodin 2023-06-30 23:45:33 +04:00
parent 0ff8160e72
commit 54a28595c2
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
4 changed files with 104 additions and 3 deletions

View File

@ -41,6 +41,12 @@ CPMAddPackage(
GIT_TAG 10.0.0
EXCLUDE_FROM_ALL YES
)
CPMAddPackage(
NAME tomlplusplus
GITHUB_REPOSITORY marzer/tomlplusplus
GIT_TAG ad55bae8a11a6eee39e2292b01e95b529b105767
EXCLUDE_FROM_ALL YES
)
CPMAddPackage(
NAME spdlog
GITHUB_REPOSITORY gabime/spdlog
@ -99,6 +105,7 @@ add_executable(${PROJECT_NAME}
src/cpu.cpp src/cpu.hpp
src/pacmanconf_repo.cpp src/pacmanconf_repo.hpp
src/initcpio.cpp src/initcpio.hpp
src/chwd_profiles.cpp src/chwd_profiles.hpp
src/disk.cpp src/disk.hpp
src/drivers.cpp src/drivers.hpp
src/widgets.cpp src/widgets.hpp
@ -134,7 +141,7 @@ if(COS_INSTALLER_BUILD_TESTS)
add_subdirectory(tests)
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::screen ftxui::dom ftxui::component range-v3::range-v3 ctre::ctre)
target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::screen ftxui::dom ftxui::component range-v3::range-v3 ctre::ctre tomlplusplus::tomlplusplus)
if(NOT ENABLE_DEVENV)
target_link_libraries(${PROJECT_NAME} PRIVATE cpr::cpr)
endif()

73
src/chwd_profiles.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "chwd_profiles.hpp"
#include "utils.hpp"
#include <algorithm> // for any_of, sort, for_each
#include <fmt/compile.h>
#include <fmt/format.h>
#define TOML_EXCEPTIONS 0 // disable exceptions
#include <toml++/toml.h>
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#include <range/v3/algorithm/any_of.hpp>
#include <range/v3/algorithm/for_each.hpp>
#include <range/v3/view/filter.hpp>
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
namespace detail::chwd {
auto get_all_profile_names(std::string_view profile_type) noexcept -> std::optional<std::vector<std::string>> {
const auto& file_path = fmt::format(FMT_COMPILE("/var/lib/chwd/db/pci/{}/profiles.toml"), profile_type);
toml::parse_result config = toml::parse_file(file_path);
/* clang-format off */
if (config.failed()) { return {}; }
/* clang-format on */
std::vector<std::string> profile_names{};
for (auto&& [key, value] : config.table()) {
if (value.is_table()) {
for (auto&& [nested_key, nested_value] : *value.as_table()) {
/* clang-format off */
if (!nested_value.is_table()) { continue; }
/* clang-format on */
const auto& nested_profile_name = fmt::format(FMT_COMPILE("{}.{}"), std::string_view{key}, std::string_view{nested_key});
profile_names.push_back(nested_profile_name);
}
profile_names.push_back(std::string(key));
}
}
return profile_names;
}
auto get_available_profile_names(std::string_view profile_type) noexcept -> std::optional<std::vector<std::string>> {
auto&& all_profile_names = get_all_profile_names(profile_type);
/* clang-format off */
if (!all_profile_names.has_value()) { return {}; }
/* clang-format on */
const auto& available_profile_names = utils::make_multiline(utils::exec("chwd --list -d | grep Name | awk '{print $4}'"));
std::vector<std::string> filtered_profile_names{};
const auto& functor = [available_profile_names](auto&& profile_name) { return ranges::any_of(available_profile_names, [profile_name](auto&& available_profile) { return available_profile == profile_name; }); };
ranges::for_each(all_profile_names.value() | ranges::views::filter(functor), [&](auto&& rng) { filtered_profile_names.push_back(std::forward<decltype(rng)>(rng)); });
return filtered_profile_names;
}
} // namespace detail::chwd

16
src/chwd_profiles.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef CHWD_PROFILES_HPP
#define CHWD_PROFILES_HPP
#include <optional> // for optional
#include <string> // for string
#include <string_view> // for string_view
#include <vector> // for vector
namespace detail::chwd {
auto get_all_profile_names(std::string_view profile_type) noexcept -> std::optional<std::vector<std::string>>;
auto get_available_profile_names(std::string_view profile_type) noexcept -> std::optional<std::vector<std::string>>;
} // namespace detail::chwd
#endif // CHWD_PROFILES_HPP

View File

@ -2,6 +2,7 @@
#include "config.hpp"
#include "utils.hpp"
#include "widgets.hpp"
#include "chwd_profiles.hpp"
/* clang-format off */
#include <fstream> // for ofstream
@ -54,8 +55,12 @@ static void setup_graphics_card() noexcept {
/// TODO(vnepogodin): parse toml DBs
{
static constexpr auto UseSpaceBar = "\nUse [Spacebar] to de/select options listed.\n";
const auto& cmd = utils::exec("mhwd -l | awk '/ video-/{print $1}' | awk '$0=$0' | sort | uniq");
const auto& radiobox_list = utils::make_multiline(cmd);
const auto& profile_names = ::detail::chwd::get_available_profile_names("graphic_drivers");
if (!profile_names.has_value()) {
spdlog::error("failed to get profile names");
return;
}
const auto& radiobox_list = profile_names.value();
auto screen = ScreenInteractive::Fullscreen();
std::int32_t selected{};