Skip to content

Commit

Permalink
Add flag to set refresh rate from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
imwints committed Oct 14, 2023
1 parent 2fd0f7b commit 1612216
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ indent = tab
tab-size = 4
*/

#include <algorithm>
#include <csignal>
#include <clocale>
#include <pthread.h>
Expand Down Expand Up @@ -50,6 +51,7 @@ tab-size = 4
#include "btop_theme.hpp"
#include "btop_draw.hpp"
#include "btop_menu.hpp"
#include "fmt/core.h"

using std::atomic;
using std::cout;
Expand Down Expand Up @@ -108,6 +110,7 @@ namespace Global {
bool arg_tty{}; // defaults to false
bool arg_low_color{}; // defaults to false
int arg_preset = -1;
int arg_refresh = 0;
}

//* A simple argument parser
Expand All @@ -116,14 +119,15 @@ void argumentParser(const int argc, char **argv) {
const string argument = argv[i];
if (is_in(argument, "-h", "--help")) {
fmt::println(
"usage: btop [-h] [-v] [-/+t] [-p <id>] [--utf-force] [--debug]\n\n"
"usage: btop [-h] [-v] [-/+t] [-p <id>] [-r <ms>] [--utf-force] [--debug]\n\n"
"optional arguments:\n"
" -h, --help show this help message and exit\n"
" -v, --version show version info and exit\n"
" -lc, --low-color disable truecolor, converts 24-bit colors to 256-color\n"
" -t, --tty_on force (ON) tty mode, max 16 colors and tty friendly graph symbols\n"
" +t, --tty_off force (OFF) tty mode\n"
" -p, --preset <id> start with preset, integer value between 0-9\n"
" -r, --refresh <ms> set the refresh rate in milliseconds\n"
" --utf-force force start even if no UTF-8 locale was detected\n"
" --debug start in DEBUG mode: shows microsecond timer for information collect\n"
" and screen draw functions and sets loglevel to DEBUG"
Expand Down Expand Up @@ -158,6 +162,19 @@ void argumentParser(const int argc, char **argv) {
exit(1);
}
}
else if (is_in(argument, "-r", "--refresh")) {
if (++i >= argc) {
fmt::println("ERROR: Refresh option needs an argument");
exit(1);
}
const std::string value = argv[i];
if (isint(value)) {
Global::arg_refresh = std::clamp(std::stoi(value), 100, 99999);
} else {
fmt::println("ERROR: Invalid refresh rate");
exit(1);
}
}
else if (argument == "--utf-force")
Global::utf_force = true;
else if (argument == "--debug")
Expand Down Expand Up @@ -973,6 +990,9 @@ int main(int argc, char **argv) {

//? ------------------------------------------------ MAIN LOOP ----------------------------------------------------

if (Global::arg_refresh != 0) {
Config::set("update_ms", Global::arg_refresh);
}
uint64_t update_ms = Config::getI("update_ms");
auto future_time = time_ms();

Expand Down

0 comments on commit 1612216

Please sign in to comment.