mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-29 20:10:56 -08:00
parent
fb203eba51
commit
90680f2c70
|
@ -32,6 +32,12 @@ func (e *commandError) Error() string {
|
|||
return e.err
|
||||
}
|
||||
|
||||
type noBatteryError struct{}
|
||||
|
||||
func (m *noBatteryError) Error() string {
|
||||
return "no battery"
|
||||
}
|
||||
|
||||
type fileInfo struct {
|
||||
parentFolder string
|
||||
path string
|
||||
|
@ -253,6 +259,9 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if batteries == nil {
|
||||
return nil, &noBatteryError{}
|
||||
}
|
||||
batt := &battery.Battery{}
|
||||
for _, bt := range batteries {
|
||||
batt.Current += bt.Current
|
||||
|
|
|
@ -35,22 +35,8 @@ const (
|
|||
func (b *batt) enabled() bool {
|
||||
var err error
|
||||
b.Battery, err = b.env.getBatteryInfo()
|
||||
|
||||
displayError := b.props.getBool(DisplayError, false)
|
||||
if err != nil && displayError {
|
||||
b.Error = err.Error()
|
||||
return true
|
||||
}
|
||||
if err != nil {
|
||||
// On Windows, it sometimes errors when the battery is full.
|
||||
// This hack ensures we display a fully charged battery, even if
|
||||
// that state can be incorrect. It's better to "ignore" the error
|
||||
// than to not display the segment at all as that will confuse users.
|
||||
b.Battery = &battery.Battery{
|
||||
Current: 100,
|
||||
Full: 100,
|
||||
State: battery.Full,
|
||||
}
|
||||
if !b.enabledWhileError(err) {
|
||||
return false
|
||||
}
|
||||
|
||||
display := b.props.getBool(DisplayCharging, true)
|
||||
|
@ -83,6 +69,30 @@ func (b *batt) enabled() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (b *batt) enabledWhileError(err error) bool {
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if _, ok := err.(*noBatteryError); ok {
|
||||
return false
|
||||
}
|
||||
displayError := b.props.getBool(DisplayError, false)
|
||||
if !displayError {
|
||||
return false
|
||||
}
|
||||
b.Error = err.Error()
|
||||
// On Windows, it sometimes errors when the battery is full.
|
||||
// This hack ensures we display a fully charged battery, even if
|
||||
// that state can be incorrect. It's better to "ignore" the error
|
||||
// than to not display the segment at all as that will confuse users.
|
||||
b.Battery = &battery.Battery{
|
||||
Current: 100,
|
||||
Full: 100,
|
||||
State: battery.Full,
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *batt) string() string {
|
||||
segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}")
|
||||
template := &textTemplate{
|
||||
|
|
|
@ -146,8 +146,22 @@ func TestBatteryErrorHidden(t *testing.T) {
|
|||
},
|
||||
env: env,
|
||||
}
|
||||
assert.True(t, b.enabled())
|
||||
assert.Equal(t, "100", b.string())
|
||||
assert.False(t, b.enabled())
|
||||
}
|
||||
|
||||
func TestBatteryNoBattery(t *testing.T) {
|
||||
env := &MockedEnvironment{}
|
||||
err := &noBatteryError{}
|
||||
env.On("getBatteryInfo", nil).Return(&battery.Battery{}, err)
|
||||
b := &batt{
|
||||
props: &properties{
|
||||
values: map[Property]interface{}{
|
||||
DisplayError: true,
|
||||
},
|
||||
},
|
||||
env: env,
|
||||
}
|
||||
assert.False(t, b.enabled())
|
||||
}
|
||||
|
||||
func TestBatteryDischargingAndDisplayChargingDisabled(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue