From 0acda08217e5a3c225fdfc8aaf541411c9dd4906 Mon Sep 17 00:00:00 2001 From: nobounce Date: Tue, 3 Oct 2023 13:54:28 +0200 Subject: [PATCH] Add flag to set the refresh rate from command line --- src/btop.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/btop.cpp b/src/btop.cpp index 03467e5fb..c8ac8f4d3 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -16,6 +16,7 @@ indent = tab tab-size = 4 */ +#include #include #include #include @@ -51,6 +52,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; @@ -109,6 +111,7 @@ namespace Global { bool arg_tty{}; // defaults to false bool arg_low_color{}; // defaults to false int arg_preset = -1; + int arg_update = 0; } //* A simple argument parser @@ -117,7 +120,7 @@ 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 ] [--utf-force] [--debug]\n\n" + "usage: btop [-h] [-v] [-/+t] [-p ] [-u ] [--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" @@ -125,6 +128,7 @@ void argumentParser(const int argc, char **argv) { " -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 start with preset, integer value between 0-9\n" + " -u, --update set the program update 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" @@ -159,6 +163,19 @@ void argumentParser(const int argc, char **argv) { exit(1); } } + else if (is_in(argument, "-u", "--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_update = 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") @@ -1060,6 +1077,9 @@ int main(int argc, char **argv) { //? ------------------------------------------------ MAIN LOOP ---------------------------------------------------- + if (Global::arg_update != 0) { + Config::set("update_ms", Global::arg_update); + } uint64_t update_ms = Config::getI("update_ms"); auto future_time = time_ms();