Skip to content

Commit d0c6ab0

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 0c11d73 commit d0c6ab0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/btop_draw.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,15 @@ 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 cwatts = (watts_error) ? " bad" : (cpu.usage_watts < 10'000.0f)
851+
? fmt::format(" {:>4.{}f}", cpu.usage_watts, cpu.usage_watts < 10.0f ? 2 : cpu.usage_watts < 100.0f ? 1 : 0)
852+
: fmt::format(" {:>3.{}f}{}K", cpu.usage_watts / 10'000.0f, cpu.usage_watts < 100'000.0f ? 1 : 0, Theme::c("main_fg"));
852853
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;
854+
fmt::format_to(std::back_inserter(out), "{watts_color}{cwatts}{main_fg}W",
855+
"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",
856+
"cwatts"_a = cwatts,
857+
"main_fg"_a = Theme::c("main_fg"));
854858
}
855859

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

0 commit comments

Comments
 (0)