Skip to content

Commit 9557815

Browse files
committed
print bad instead of value if cpu watts reading bad
replace string with `const string` if cpu watts is greater then 10,000 start using abbreviated value ending in `KW` use `fmt::format_to` for output
1 parent 633b4ba commit 9557815

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/btop_draw.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,17 @@ namespace Cpu {
846846
}
847847

848848
if (show_watts) {
849-
string cwatts = fmt::format(" {:>4.{}f}", cpu.usage_watts, cpu.usage_watts < 10.0f ? 2 : cpu.usage_watts < 100.0f ? 1 : 0);
850-
string cwatts_post = "W";
851-
849+
const auto watts_error = cpu.usage_watts < 0.0f;
850+
const auto is_cwatts_kilo = cpu.usage_watts >= 10'000.0f;
851+
const auto cwatts = (watts_error) ? " bad" : (is_cwatts_kilo)
852+
? fmt::format(" {:>3.{}f}", cpu.usage_watts / 10'000.0f, cpu.usage_watts < 100'000.0f ? 1 : 0)
853+
: fmt::format(" {:>4.{}f}", cpu.usage_watts, cpu.usage_watts < 10.0f ? 2 : cpu.usage_watts < 100.0f ? 1 : 0);
852854
max_observed_pwr = max(max_observed_pwr, cpu.usage_watts);
853-
out += Theme::g("cached").at(clamp(cpu.usage_watts / max_observed_pwr * 100.0f, 0.0f, 100.0f)) + cwatts + Theme::c("main_fg") + cwatts_post;
855+
fmt::format_to(std::back_inserter(out), "{watts_color}{cwatts}{main_fg}{cpost}",
856+
"watts_color"_a = (!watts_error) ? Theme::g("cached").at(clamp(cpu.usage_watts / max_observed_pwr * 100.0f, 0.0f, 100.0f)) : "\x1b[0;91m",
857+
"cwatts"_a = cwatts,
858+
"main_fg"_a = Theme::c("main_fg"),
859+
"cpost"_a = (is_cwatts_kilo) ? "KW" : "W");
854860
}
855861

856862
out += Theme::c("div_line") + Symbols::v_line;

0 commit comments

Comments
 (0)