mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 03:49:40 -08:00
parent
3e18c241ab
commit
c0aa27a342
|
@ -7,13 +7,16 @@ import (
|
|||
)
|
||||
|
||||
type batt struct {
|
||||
props *properties
|
||||
env environmentInfo
|
||||
props *properties
|
||||
env environmentInfo
|
||||
percentageText string
|
||||
}
|
||||
|
||||
const (
|
||||
//BatteryIcon to display in front of the battery
|
||||
BatteryIcon Property = "battery_icon"
|
||||
//DisplayError to display when an error occurs or not
|
||||
DisplayError Property = "display_error"
|
||||
//ChargingIcon to display when charging
|
||||
ChargingIcon Property = "charging_icon"
|
||||
//DischargingIcon o display when discharging
|
||||
|
@ -29,13 +32,14 @@ const (
|
|||
)
|
||||
|
||||
func (b *batt) enabled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *batt) string() string {
|
||||
bt, err := b.env.getBatteryInfo()
|
||||
displayError := b.props.getBool(DisplayError, true)
|
||||
if err != nil && !displayError {
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
return "BATT ERR"
|
||||
b.percentageText = "BATT ERR"
|
||||
return true
|
||||
}
|
||||
batteryPercentage := bt.Current / bt.Full * 100
|
||||
percentageText := fmt.Sprintf("%.0f", batteryPercentage)
|
||||
|
@ -52,7 +56,8 @@ func (b *batt) string() string {
|
|||
colorPorperty = ChargedColor
|
||||
icon = b.props.getString(ChargedIcon, "")
|
||||
default:
|
||||
return percentageText
|
||||
b.percentageText = percentageText
|
||||
return true
|
||||
}
|
||||
colorBackground := b.props.getBool(ColorBackground, false)
|
||||
if colorBackground {
|
||||
|
@ -61,7 +66,12 @@ func (b *batt) string() string {
|
|||
b.props.foreground = b.props.getColor(colorPorperty, b.props.foreground)
|
||||
}
|
||||
batteryIcon := b.props.getString(BatteryIcon, "")
|
||||
return fmt.Sprintf("%s%s%s", icon, batteryIcon, percentageText)
|
||||
b.percentageText = fmt.Sprintf("%s%s%s", icon, batteryIcon, percentageText)
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *batt) string() string {
|
||||
return b.percentageText
|
||||
}
|
||||
|
||||
func (b *batt) init(props *properties, env environmentInfo) {
|
||||
|
|
|
@ -8,16 +8,6 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBattery(t *testing.T) {
|
||||
env := &environment{}
|
||||
b := &batt{
|
||||
env: env,
|
||||
props: &properties{},
|
||||
}
|
||||
val := b.string()
|
||||
assert.NotEmpty(t, val)
|
||||
}
|
||||
|
||||
func setupBatteryTests(state battery.State, batteryLevel float64, props *properties) *batt {
|
||||
env := &MockedEnvironment{}
|
||||
bt := &battery.Battery{
|
||||
|
@ -26,10 +16,12 @@ func setupBatteryTests(state battery.State, batteryLevel float64, props *propert
|
|||
Current: batteryLevel,
|
||||
}
|
||||
env.On("getBatteryInfo", nil).Return(bt, nil)
|
||||
return &batt{
|
||||
b := &batt{
|
||||
props: props,
|
||||
env: env,
|
||||
}
|
||||
b.enabled()
|
||||
return b
|
||||
}
|
||||
|
||||
func TestBatteryCharging(t *testing.T) {
|
||||
|
@ -130,5 +122,23 @@ func TestBatteryError(t *testing.T) {
|
|||
props: nil,
|
||||
env: env,
|
||||
}
|
||||
assert.True(t, b.enabled())
|
||||
assert.Equal(t, "BATT ERR", b.string())
|
||||
}
|
||||
|
||||
func TestBatteryErrorHidden(t *testing.T) {
|
||||
env := &MockedEnvironment{}
|
||||
err := errors.New("oh snap")
|
||||
env.On("getBatteryInfo", nil).Return(&battery.Battery{}, err)
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
DisplayError: false,
|
||||
},
|
||||
}
|
||||
b := &batt{
|
||||
props: props,
|
||||
env: env,
|
||||
}
|
||||
assert.False(t, b.enabled())
|
||||
assert.Equal(t, "", b.string())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue