feat: add cli option to dump default config

Closes: https://github.com/aristocratos/btop/issues/1394
This commit is contained in:
Steffen Winter
2025-12-13 12:05:10 +01:00
committed by Steffen
parent c2b477ec9f
commit 10086d6009
6 changed files with 133 additions and 29 deletions

View File

@@ -5,15 +5,20 @@
#include <algorithm>
#include <expected>
#include <filesystem>
#include <iterator>
#include <optional>
#include <ranges>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unistd.h>
#include <fmt/base.h>
#include <fmt/format.h>
#include "btop_config.hpp"
#include "btop_shared.hpp"
#include "config.h"
@@ -22,6 +27,9 @@ using namespace std::string_view_literals;
static constexpr auto BOLD = "\033[1m"sv;
static constexpr auto BOLD_UNDERLINE = "\033[1;4m"sv;
static constexpr auto BOLD_RED = "\033[1;31m"sv;
static constexpr auto BOLD_GREEN = "\033[1;32m"sv;
static constexpr auto BOLD_YELLOW = "\033[1;33m"sv;
static constexpr auto BOLD_BRIGHT_BLACK = "\033[1;90m"sv;
static constexpr auto YELLOW = "\033[33m"sv;
static constexpr auto RESET = "\033[0m"sv;
@@ -49,6 +57,9 @@ namespace Cli {
for (auto it = args.begin(); it != args.end(); ++it) {
auto arg = *it;
if (arg == "--default-config") {
return default_config();
}
if (arg == "-h" || arg == "--help") {
usage();
help();
@@ -187,6 +198,50 @@ namespace Cli {
return cli;
}
auto default_config() noexcept -> Result {
// The idea of using `current_config` is that the CLI parser is run before loading the actual config and thus
// provides default values.
auto config = Config::current_config();
if (isatty(STDOUT_FILENO)) {
std::string buffer {};
// The config buffer ends in `\n`. `std::views::split` will then create an empty element after the last
// newline, which we would write as an additional empty line at the very end.
auto trimmed_config = config.substr(0, config.length() - 1);
for (const auto line : std::views::split(trimmed_config, '\n')) {
auto line_view = std::string_view { line };
if (line_view.starts_with("#")) {
fmt::format_to(
std::back_inserter(buffer), "{1}{0}{2}\n", line_view, BOLD_BRIGHT_BLACK, RESET
);
} else if (!line_view.empty()) {
auto pos = line_view.find("=");
if (pos == line_view.npos) {
error("invalid default config: '=' not found");
return std::unexpected { 1 };
}
auto name = line_view.substr(0, pos);
auto value = line_view.substr(pos + 1);
fmt::format_to(
std::back_inserter(buffer),
"{2}{0}{4}={3}{1}{4}\n",
name,
value,
BOLD_YELLOW,
BOLD_GREEN,
RESET
);
} else {
fmt::format_to(std::back_inserter(buffer), "\n");
}
}
fmt::print("{}", buffer);
} else {
fmt::print("{}", config);
}
return std::unexpected { 0 };
}
void usage() noexcept {
fmt::println("{0}Usage:{1} {2}btop{1} [OPTIONS]\n", BOLD_UNDERLINE, RESET, BOLD);
}
@@ -204,6 +259,7 @@ namespace Cli {
" {2} --themes-dir{1} <dir> Path to a custom themes directory\n"
" {2} --no-tty{1} Force disable tty mode\n"
" {2}-u, --update{1} <ms> Set an initial update rate in milliseconds\n"
" {2} --default-config{1} Print default config to standard output\n"
" {2}-h, --help{1} Show this help message and exit\n"
" {2}-V, --version{1} Show a version message and exit (more with --version)\n",
BOLD_UNDERLINE, RESET, BOLD