Skip to content

Commit

Permalink
🧹 refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jun 17, 2024
1 parent fde1d55 commit c4e6626
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/conf-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ namespace fs = std::filesystem;
GENERATE_CONST_OPTION_VALUES(name, __VA_ARGS__) \
GENERATE_CONST_LOOKUP_VALUES(name, __VA_ARGS__)

namespace {

GENERATE_CONST_LOOKUP_OPTION_VALUES(kernel_name, "cachyos", "bore", "rc", "rt", "rt-bore", "sched-ext")
GENERATE_CONST_OPTION_VALUES(hz_tick, "1000", "750", "600", "500", "300", "250", "100")
GENERATE_CONST_OPTION_VALUES(tickless_mode, "full", "idle", "perodic")
Expand Down Expand Up @@ -149,11 +151,6 @@ inline bool checkstate_checked(QCheckBox* checkbox) noexcept {
return (checkbox->checkState() == Qt::Checked);
}

inline auto convert_checkstate(QCheckBox* checkbox) noexcept {
using namespace std::string_view_literals;
return checkstate_checked(checkbox) ? "y"sv : "n"sv;
}

constexpr auto convert_to_varname(std::string_view option) noexcept {
// force constexpr call with lambda
return [option] { return detail::option_map.at(option); }();
Expand Down Expand Up @@ -282,6 +279,8 @@ inline void list_widget_apply_edit_flag(QListWidget* list_widget) noexcept {
}
}

} // namespace

void ConfWindow::connect_all_checkboxes() noexcept {
auto* options_page_ui_obj = m_ui->conf_options_page_widget->get_ui_obj();

Expand Down
8 changes: 4 additions & 4 deletions src/schedext-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ auto get_current_scheduler() noexcept -> std::string {

// NOTE: we assume that window won't be launched on kernel without sched_ext
// e.g we won't show window at all in that case.
const auto& current_state = read_kernel_file("/sys/kernel/sched_ext/state");
const auto& current_state = read_kernel_file("/sys/kernel/sched_ext/state"sv);
if (current_state != "enabled"sv) {
return current_state;
}
const auto& current_sched = read_kernel_file("/sys/kernel/sched_ext/root/ops");
const auto& current_sched = read_kernel_file("/sys/kernel/sched_ext/root/ops"sv);
if (current_sched.empty()) {
return std::string{"unknown"sv};
}
Expand All @@ -85,12 +85,12 @@ auto get_current_scheduler() noexcept -> std::string {

auto is_scx_service_enabled() noexcept -> bool {
using namespace std::string_view_literals;
return utils::exec("systemctl is-enabled scx") == "enabled"sv;
return utils::exec("systemctl is-enabled scx"sv) == "enabled"sv;
}

auto is_scx_service_active() noexcept -> bool {
using namespace std::string_view_literals;
return utils::exec("systemctl is-active scx") == "active"sv;
return utils::exec("systemctl is-active scx"sv) == "active"sv;
}
} // namespace

Expand Down
6 changes: 3 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace fs = std::filesystem;

namespace utils {

auto read_whole_file(const std::string_view& filepath) noexcept -> std::string {
auto read_whole_file(std::string_view filepath) noexcept -> std::string {
// Use std::fopen because it's faster than std::ifstream
auto* file = std::fopen(filepath.data(), "rb");
if (file == nullptr) {
Expand All @@ -80,7 +80,7 @@ auto read_whole_file(const std::string_view& filepath) noexcept -> std::string {
return buf;
}

bool write_to_file(const std::string_view& filepath, const std::string_view& data) noexcept {
bool write_to_file(std::string_view filepath, std::string_view data) noexcept {
std::ofstream file{filepath.data()};
if (!file.is_open()) {
fmt::print(stderr, "[WRITE_TO_FILE] '{}' open failed: {}\n", filepath, std::strerror(errno));
Expand All @@ -95,7 +95,7 @@ bool write_to_file(const std::string_view& filepath, const std::string_view& dat
// https://github.com/arun11299/cpp-subprocess/blob/master/subprocess.hpp#L1218
// https://stackoverflow.com/questions/11342868/c-interface-for-interactive-bash
// https://github.com/hniksic/rust-subprocess
std::string exec(const std::string_view& command) noexcept {
std::string exec(std::string_view command) noexcept {
// NOLINTNEXTLINE
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.data(), "r"), pclose);
if (!pipe) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

namespace utils {

[[nodiscard]] auto read_whole_file(const std::string_view& filepath) noexcept -> std::string;
bool write_to_file(const std::string_view& filepath, const std::string_view& data) noexcept;
std::string exec(const std::string_view& command) noexcept;
[[nodiscard]] auto read_whole_file(std::string_view filepath) noexcept -> std::string;
bool write_to_file(std::string_view filepath, std::string_view data) noexcept;
std::string exec(std::string_view command) noexcept;
[[nodiscard]] std::string fix_path(std::string&& path) noexcept;

// Runs a command in a terminal, escalates using pkexec if escalate is true
Expand Down

0 comments on commit c4e6626

Please sign in to comment.