Skip to content

Commit 114a98d

Browse files
authored
Merge pull request #640 from imwints/refresh-rate
2 parents f6cab3d + 1e6bf18 commit 114a98d

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/btop.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ indent = tab
1616
tab-size = 4
1717
*/
1818

19+
#include <algorithm>
1920
#include <csignal>
2021
#include <clocale>
2122
#include <pthread.h>
@@ -51,6 +52,7 @@ tab-size = 4
5152
#include "btop_theme.hpp"
5253
#include "btop_draw.hpp"
5354
#include "btop_menu.hpp"
55+
#include "fmt/core.h"
5456

5557
using std::atomic;
5658
using std::cout;
@@ -109,22 +111,24 @@ namespace Global {
109111
bool arg_tty{}; // defaults to false
110112
bool arg_low_color{}; // defaults to false
111113
int arg_preset = -1;
114+
int arg_update = 0;
112115
}
113116

114117
//* A simple argument parser
115-
void argumentParser(const int& argc, char **argv) {
118+
void argumentParser(const int argc, char **argv) {
116119
for(int i = 1; i < argc; i++) {
117120
const string argument = argv[i];
118121
if (is_in(argument, "-h", "--help")) {
119122
fmt::println(
120-
"usage: btop [-h] [-v] [-/+t] [-p <id>] [--utf-force] [--debug]\n\n"
123+
"usage: btop [-h] [-v] [-/+t] [-p <id>] [-u <ms>] [--utf-force] [--debug]\n\n"
121124
"optional arguments:\n"
122125
" -h, --help show this help message and exit\n"
123126
" -v, --version show version info and exit\n"
124127
" -lc, --low-color disable truecolor, converts 24-bit colors to 256-color\n"
125128
" -t, --tty_on force (ON) tty mode, max 16 colors and tty friendly graph symbols\n"
126129
" +t, --tty_off force (OFF) tty mode\n"
127130
" -p, --preset <id> start with preset, integer value between 0-9\n"
131+
" -u, --update <ms> set the program update rate in milliseconds\n"
128132
" --utf-force force start even if no UTF-8 locale was detected\n"
129133
" --debug start in DEBUG mode: shows microsecond timer for information collect\n"
130134
" and screen draw functions and sets loglevel to DEBUG"
@@ -159,6 +163,19 @@ void argumentParser(const int& argc, char **argv) {
159163
exit(1);
160164
}
161165
}
166+
else if (is_in(argument, "-u", "--update")) {
167+
if (++i >= argc) {
168+
fmt::println("ERROR: Update option needs an argument");
169+
exit(1);
170+
}
171+
const std::string value = argv[i];
172+
if (isint(value)) {
173+
Global::arg_update = std::clamp(std::stoi(value), 100, Config::ONE_DAY_MILLIS);
174+
} else {
175+
fmt::println("ERROR: Invalid update rate");
176+
exit(1);
177+
}
178+
}
162179
else if (argument == "--utf-force")
163180
Global::utf_force = true;
164181
else if (argument == "--debug")
@@ -1054,6 +1071,9 @@ int main(int argc, char **argv) {
10541071

10551072
//? ------------------------------------------------ MAIN LOOP ----------------------------------------------------
10561073

1074+
if (Global::arg_update != 0) {
1075+
Config::set("update_ms", Global::arg_update);
1076+
}
10571077
uint64_t update_ms = Config::getI("update_ms");
10581078
auto future_time = time_ms();
10591079

src/btop_config.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ namespace Config {
488488
if (name == "update_ms" and i_value < 100)
489489
validError = "Config value update_ms set too low (<100).";
490490

491-
else if (name == "update_ms" and i_value > 86400000)
492-
validError = "Config value update_ms set too high (>86400000).";
491+
else if (name == "update_ms" and i_value > ONE_DAY_MILLIS)
492+
validError = fmt::format("Config value update_ms set too high (>{}).", ONE_DAY_MILLIS);
493493

494494
else
495495
return true;

src/btop_config.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace Config {
5858
extern vector<string> available_batteries;
5959
extern int current_preset;
6060

61+
constexpr int ONE_DAY_MILLIS = 1000 * 60 * 60 * 24;
62+
6163
[[nodiscard]] std::optional<std::filesystem::path> get_config_dir() noexcept;
6264

6365
//* Check if string only contains space separated valid names for boxes
@@ -97,7 +99,7 @@ namespace Config {
9799
}
98100

99101
//* Set config key <name> to int <value>
100-
inline void set(const std::string_view name, const int& value) {
102+
inline void set(const std::string_view name, const int value) {
101103
if (_locked(name)) intsTmp.insert_or_assign(name, value);
102104
else ints.at(name) = value;
103105
}

0 commit comments

Comments
 (0)