mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
parent
fb203eba51
commit
90680f2c70
|
@ -32,6 +32,12 @@ func (e *commandError) Error() string {
|
||||||
return e.err
|
return e.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type noBatteryError struct{}
|
||||||
|
|
||||||
|
func (m *noBatteryError) Error() string {
|
||||||
|
return "no battery"
|
||||||
|
}
|
||||||
|
|
||||||
type fileInfo struct {
|
type fileInfo struct {
|
||||||
parentFolder string
|
parentFolder string
|
||||||
path string
|
path string
|
||||||
|
@ -253,6 +259,9 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if batteries == nil {
|
||||||
|
return nil, &noBatteryError{}
|
||||||
|
}
|
||||||
batt := &battery.Battery{}
|
batt := &battery.Battery{}
|
||||||
for _, bt := range batteries {
|
for _, bt := range batteries {
|
||||||
batt.Current += bt.Current
|
batt.Current += bt.Current
|
||||||
|
|
|
@ -35,22 +35,8 @@ const (
|
||||||
func (b *batt) enabled() bool {
|
func (b *batt) enabled() bool {
|
||||||
var err error
|
var err error
|
||||||
b.Battery, err = b.env.getBatteryInfo()
|
b.Battery, err = b.env.getBatteryInfo()
|
||||||
|
if !b.enabledWhileError(err) {
|
||||||
displayError := b.props.getBool(DisplayError, false)
|
return 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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
display := b.props.getBool(DisplayCharging, true)
|
display := b.props.getBool(DisplayCharging, true)
|
||||||
|
@ -83,6 +69,30 @@ func (b *batt) enabled() bool {
|
||||||
return true
|
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 {
|
func (b *batt) string() string {
|
||||||
segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}")
|
segmentTemplate := b.props.getString(SegmentTemplate, "{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}")
|
||||||
template := &textTemplate{
|
template := &textTemplate{
|
||||||
|
|
|
@ -146,8 +146,22 @@ func TestBatteryErrorHidden(t *testing.T) {
|
||||||
},
|
},
|
||||||
env: env,
|
env: env,
|
||||||
}
|
}
|
||||||
assert.True(t, b.enabled())
|
assert.False(t, b.enabled())
|
||||||
assert.Equal(t, "100", b.string())
|
}
|
||||||
|
|
||||||
|
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) {
|
func TestBatteryDischargingAndDisplayChargingDisabled(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue