mirror of
https://github.com/polybar/polybar.git
synced 2024-11-11 13:50:56 -05:00
feat(battery): add %percentage_raw%
token (#1756)
Displays real percentage instead of being set to 100 if percentage > full-at * battery: added percentage_raw token, which ignores full-at * battery: current_percentage returns raw, added clamping function instead * battery: clamp percentage used by build() Made clamp_percentage() const to allow its usage inside build() * battery: read and return percentage in one line
This commit is contained in:
parent
65e72fd71e
commit
b2b73b5d91
2 changed files with 14 additions and 9 deletions
|
@ -56,7 +56,8 @@ namespace modules {
|
|||
|
||||
protected:
|
||||
state current_state();
|
||||
int current_percentage(state state);
|
||||
int current_percentage();
|
||||
int clamp_percentage(int percentage, state state) const;
|
||||
string current_time();
|
||||
string current_consumption();
|
||||
void subthread();
|
||||
|
|
|
@ -109,7 +109,7 @@ namespace modules {
|
|||
|
||||
// Load state and capacity level
|
||||
m_state = current_state();
|
||||
m_percentage = current_percentage(m_state);
|
||||
m_percentage = current_percentage();
|
||||
|
||||
// Add formats and elements
|
||||
m_formatter->add(FORMAT_CHARGING, TAG_LABEL_CHARGING,
|
||||
|
@ -199,7 +199,7 @@ namespace modules {
|
|||
*/
|
||||
bool battery_module::on_event(inotify_event* event) {
|
||||
auto state = current_state();
|
||||
auto percentage = current_percentage(state);
|
||||
auto percentage = current_percentage();
|
||||
|
||||
// Reset timer to avoid unnecessary polling
|
||||
m_lastpoll = chrono::system_clock::now();
|
||||
|
@ -229,7 +229,8 @@ namespace modules {
|
|||
|
||||
if (label) {
|
||||
label->reset_tokens();
|
||||
label->replace_token("%percentage%", to_string(m_percentage));
|
||||
label->replace_token("%percentage%", to_string(clamp_percentage(m_percentage, m_state)));
|
||||
label->replace_token("%percentage_raw%", to_string(m_percentage));
|
||||
label->replace_token("%consumption%", current_consumption());
|
||||
|
||||
if (m_state != battery_module::state::FULL && !m_timeformat.empty()) {
|
||||
|
@ -262,9 +263,9 @@ namespace modules {
|
|||
} else if (tag == TAG_ANIMATION_DISCHARGING) {
|
||||
builder->node(m_animation_discharging->get());
|
||||
} else if (tag == TAG_BAR_CAPACITY) {
|
||||
builder->node(m_bar_capacity->output(m_percentage));
|
||||
builder->node(m_bar_capacity->output(clamp_percentage(m_percentage, m_state)));
|
||||
} else if (tag == TAG_RAMP_CAPACITY) {
|
||||
builder->node(m_ramp_capacity->get_by_percentage(m_percentage));
|
||||
builder->node(m_ramp_capacity->get_by_percentage(clamp_percentage(m_percentage, m_state)));
|
||||
} else if (tag == TAG_LABEL_CHARGING) {
|
||||
builder->node(m_label_charging);
|
||||
} else if (tag == TAG_LABEL_DISCHARGING) {
|
||||
|
@ -294,10 +295,13 @@ namespace modules {
|
|||
/**
|
||||
* Get the current capacity level
|
||||
*/
|
||||
int battery_module::current_percentage(state state) {
|
||||
int percentage{read(*m_capacity_reader)};
|
||||
int battery_module::current_percentage() {
|
||||
return read(*m_capacity_reader);
|
||||
}
|
||||
|
||||
int battery_module::clamp_percentage(int percentage, state state) const {
|
||||
if (state == battery_module::state::FULL && percentage >= m_fullat) {
|
||||
percentage = 100;
|
||||
return 100;
|
||||
}
|
||||
return percentage;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue