Skip to content

Commit

Permalink
feat: option to hide the battery status when it's charging
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Dec 4, 2020
1 parent 67beefe commit c338278
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/docs/segment-battery.md
Expand Up @@ -26,7 +26,8 @@ Battery displays the remaining power percentage for your battery.
"charged_color": "#4caf50",
"charging_color": "#40c4ff",
"discharging_color": "#ff5722",
"postfix": "\uF295 "
"postfix": "\uF295 ",
"display_charging": true
}
}
```
Expand All @@ -42,5 +43,6 @@ Battery displays the remaining power percentage for your battery.
- charged_color: `string` [color][colors] - color to use when fully charged - defaults to segment color
- charging_color: `string` [color][colors] - color to use when charging - defaults to segment color
- discharging_color: `string` [color][colors] - color to use when discharging - defaults to segment color
- display_charging: `bool` - displays the battery status while charging (Charging or Full)

[colors]: /docs/configure#colors
8 changes: 8 additions & 0 deletions segment_battery.go
Expand Up @@ -30,10 +30,18 @@ const (
ChargingColor Property = "charging_color"
// DischargingColor to display when discharging
DischargingColor Property = "discharging_color"
// DisplayCharging Hide the battery icon while it's charging
DisplayCharging Property = "display_charging"
)

func (b *batt) enabled() bool {
bt, err := b.env.getBatteryInfo()

display := b.props.getBool(DisplayCharging, true)
if !display && (bt.State == battery.Charging || bt.State == battery.Full) {
return false
}

displayError := b.props.getBool(DisplayError, false)
if err != nil && !displayError {
b.percentageText = "100%"
Expand Down
34 changes: 34 additions & 0 deletions segment_battery_test.go
Expand Up @@ -149,3 +149,37 @@ func TestBatteryErrorHidden(t *testing.T) {
assert.True(t, b.enabled())
assert.Equal(t, "100%", b.string())
}

func TestBatteryDischargingAndDisplayChargingDisabled(t *testing.T) {
props := &properties{
values: map[Property]interface{}{
DischargingIcon: "going down ",
DisplayCharging: false,
},
}
b := setupBatteryTests(battery.Discharging, 70, props)
assert.Equal(t, true, b.enabled())
assert.Equal(t, "going down 70", b.string())
}

func TestBatteryChargingAndDisplayChargingDisabled(t *testing.T) {
props := &properties{
values: map[Property]interface{}{
ChargingIcon: "charging ",
DisplayCharging: false,
},
}
b := setupBatteryTests(battery.Charging, 80, props)
assert.Equal(t, false, b.enabled())
}

func TestBatteryChargedAndDisplayChargingDisabled(t *testing.T) {
props := &properties{
values: map[Property]interface{}{
ChargedIcon: "charged ",
DisplayCharging: false,
},
}
b := setupBatteryTests(battery.Full, 100, props)
assert.Equal(t, false, b.enabled())
}
8 changes: 7 additions & 1 deletion themes/schema.json
Expand Up @@ -277,7 +277,13 @@
},
"charged_color": { "$ref": "#/definitions/color" },
"charging_color": { "$ref": "#/definitions/color" },
"discharging_color": { "$ref": "#/definitions/color" }
"discharging_color": { "$ref": "#/definitions/color" },
"display_charging": {
"type": "boolean",
"title": "Display while charging",
"description": "displays the battery status while charging (Charging or Full)",
"default": true
}
}
}
}
Expand Down

0 comments on commit c338278

Please sign in to comment.