Repository: JanDeDobbeleer/oh-my-posh
Pull request #631 (2021-04-11 12:28:25)

fix(pwsh): support raspbian
### Prerequisites

- [x] I have read and understand the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes / features)
- [x] Docs have been added / updated (for bug fixes / features)

### Description

[Description of the change]

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/docs/docs/share.mdx b/docs/docs/share-theme.mdx
similarity index 96%
rename from docs/docs/share.mdx
rename to docs/docs/share-theme.mdx
index 10b695ca..389ac330 100644
--- a/docs/docs/share.mdx
+++ b/docs/docs/share-theme.mdx
@@ -1,7 +1,7 @@
 ---
 id: share
-title: Share
-sidebar_label: 📸 Share
+title: Share theme
+sidebar_label: 📸 Share theme
 ---
 
 import Tabs from "@theme/Tabs";
diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psm1 b/packages/powershell/oh-my-posh/oh-my-posh.psm1
index ad90daed..bf3be814 100644
--- a/packages/powershell/oh-my-posh/oh-my-posh.psm1
+++ b/packages/powershell/oh-my-posh/oh-my-posh.psm1
@@ -10,7 +10,7 @@ function Get-PoshCommand {
     if ($IsLinux) {
         # this is rather hacky but there's no other way for the time being
         $arch = uname -m
-        if ($arch -eq 'aarch64') {
+        if (($arch -eq 'aarch64') -or ($arch -eq 'armv7l')) {
             return "$PSScriptRoot/bin/posh-linux-arm"
         }
         return "$PSScriptRoot/bin/posh-linux-amd64"
diff --git a/src/console_title.go b/src/console_title.go
index bb9d918f..66df35dd 100644
--- a/src/console_title.go
+++ b/src/console_title.go
@@ -65,7 +65,11 @@ func (t *consoleTitle) getTemplateText() string {
 		Template: t.config.ConsoleTitleTemplate,
 		Context:  context,
 	}
-	return template.render()
+	text, err := template.render()
+	if err != nil {
+		return err.Error()
+	}
+	return text
 }
 
 func (t *consoleTitle) getPwd() string {
diff --git a/src/environment.go b/src/environment.go
index 3da9b866..132a1514 100644
--- a/src/environment.go
+++ b/src/environment.go
@@ -240,7 +240,26 @@ func (env *environment) getArgs() *args {
 }
 
 func (env *environment) getBatteryInfo() (*battery.Battery, error) {
-	return battery.Get(0)
+	getMostLogicalState := func(currentState battery.State, state battery.State) battery.State {
+		if currentState == battery.Unknown {
+			return state
+		}
+		if currentState == battery.Empty|battery.Full && state == battery.Charging|battery.Discharging {
+			return state
+		}
+		return battery.Charging
+	}
+	batteries, err := battery.GetAll()
+	if err != nil {
+		return nil, err
+	}
+	batt := &battery.Battery{}
+	for _, bt := range batteries {
+		batt.Current += bt.Current
+		batt.Full += bt.Full
+		batt.State = getMostLogicalState(batt.State, bt.State)
+	}
+	return batt, nil
 }
 
 func (env *environment) getShellName() string {
diff --git a/src/main.go b/src/main.go
index 33268b51..6ba5571d 100644
--- a/src/main.go
+++ b/src/main.go
@@ -275,5 +275,9 @@ func getConsoleBackgroundColor(env environmentInfo, backgroundColorTemplate stri
 		Template: backgroundColorTemplate,
 		Context:  context,
 	}
-	return template.render()
+	text, err := template.render()
+	if err != nil {
+		return err.Error()
+	}
+	return text
 }
diff --git a/src/segment.go b/src/segment.go
index c9cc2093..c50e9018 100644
--- a/src/segment.go
+++ b/src/segment.go
@@ -170,8 +170,8 @@ func (segment *Segment) getColor(templates []string, defaultColor string) string
 	}
 	for _, template := range templates {
 		txtTemplate.Template = template
-		value := txtTemplate.render()
-		if value == "" {
+		value, err := txtTemplate.render()
+		if err != nil || value == "" {
 			continue
 		}
 		return value
diff --git a/src/segment_aws.go b/src/segment_aws.go
index d3f4024e..aa3f3788 100644
--- a/src/segment_aws.go
+++ b/src/segment_aws.go
@@ -84,5 +84,9 @@ func (a *aws) string() string {
 		Template: segmentTemplate,
 		Context:  a,
 	}
-	return template.render()
+	text, err := template.render()
+	if err != nil {
+		return err.Error()
+	}
+	return text
 }
diff --git a/src/segment_battery.go b/src/segment_battery.go
index 081905ef..08459514 100644
--- a/src/segment_battery.go
+++ b/src/segment_battery.go
@@ -89,7 +89,11 @@ func (b *batt) string() string {
 		Template: segmentTemplate,
 		Context:  b,
 	}
-	return template.render()
+	text, err := template.render()
+	if err != nil {
+		return err.Error()
+	}
+	return text
 }
 
 func (b *batt) init(props *properties, env environmentInfo) {
diff --git a/src/segment_kubectl.go b/src/segment_kubectl.go
index b965b8a5..3dfdf8dc 100644
--- a/src/segment_kubectl.go
+++ b/src/segment_kubectl.go
@@ -17,7 +17,11 @@ func (k *kubectl) string() string {
 		Template: segmentTemplate,
 		Context:  k,
 	}
-	return template.render()
+	text, err := template.render()
+	if err != nil {
+		return err.Error()
+	}
+	return text
 }
 
 func (k *kubectl) init(props *properties, env environmentInfo) {
diff --git a/src/segment_session.go b/src/segment_session.go
index 96bc1236..ed7cfca7 100644
--- a/src/segment_session.go
+++ b/src/segment_session.go
@@ -47,7 +47,11 @@ func (s *session) enabled() bool {
 			Template: segmentTemplate,
 			Context:  s,
 		}
-		s.templateText = template.render()
+		var err error
+		s.templateText, err = template.render()
+		if err != nil {
+			s.templateText = err.Error()
+		}
 		return len(s.templateText) > 0
 	}
 	showDefaultUser := s.props.getBool(DisplayDefault, true)
diff --git a/src/segment_time.go b/src/segment_time.go
index 2851ec2d..f537c529 100644
--- a/src/segment_time.go
+++ b/src/segment_time.go
@@ -25,7 +25,11 @@ func (t *tempus) enabled() bool {
 			Template: segmentTemplate,
 			Context:  t,
 		}
-		t.templateText = template.render()
+		var err error
+		t.templateText, err = template.render()
+		if err != nil {
+			t.templateText = err.Error()
+		}
 		return len(t.templateText) > 0
 	}
 	return true
diff --git a/src/template.go b/src/template.go
index 2ec800db..bbeee660 100644
--- a/src/template.go
+++ b/src/template.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"bytes"
+	"errors"
 	"text/template"
 
 	"github.com/Masterminds/sprig"
@@ -18,16 +19,16 @@ type textTemplate struct {
 	Context  interface{}
 }
 
-func (t *textTemplate) render() string {
+func (t *textTemplate) render() (string, error) {
 	tmpl, err := template.New("title").Funcs(sprig.TxtFuncMap()).Parse(t.Template)
 	if err != nil {
-		return invalidTemplate
+		return "", errors.New(invalidTemplate)
 	}
 	buffer := new(bytes.Buffer)
 	defer buffer.Reset()
 	err = tmpl.Execute(buffer, t.Context)
 	if err != nil {
-		return incorrectTemplate
+		return "", errors.New(incorrectTemplate)
 	}
-	return buffer.String()
+	return buffer.String(), nil
 }


Test patch:
diff --git a/src/template_test.go b/src/template_test.go
index 539f1da5..dea8fa51 100644
--- a/src/template_test.go
+++ b/src/template_test.go
@@ -8,14 +8,15 @@ import (
 
 func TestRenderTemplate(t *testing.T) {
 	cases := []struct {
-		Case     string
-		Expected string
-		Template string
-		Context  interface{}
+		Case        string
+		Expected    string
+		Template    string
+		ShouldError bool
+		Context     interface{}
 	}{
 		{Case: "single property", Expected: "hello world", Template: "{{.Text}} world", Context: struct{ Text string }{Text: "hello"}},
-		{Case: "invalid property", Expected: incorrectTemplate, Template: "{{.Durp}} world", Context: struct{ Text string }{Text: "hello"}},
-		{Case: "invalid template", Expected: invalidTemplate, Template: "{{ if .Text }} world", Context: struct{ Text string }{Text: "hello"}},
+		{Case: "invalid property", ShouldError: true, Template: "{{.Durp}} world", Context: struct{ Text string }{Text: "hello"}},
+		{Case: "invalid template", ShouldError: true, Template: "{{ if .Text }} world", Context: struct{ Text string }{Text: "hello"}},
 		{Case: "if statement true", Expected: "hello world", Template: "{{ if .Text }}{{.Text}} world{{end}}", Context: struct{ Text string }{Text: "hello"}},
 		{Case: "if statement false", Expected: "world", Template: "{{ if .Text }}{{.Text}} {{end}}world", Context: struct{ Text string }{Text: ""}},
 		{
@@ -72,6 +73,11 @@ func TestRenderTemplate(t *testing.T) {
 			Template: tc.Template,
 			Context:  tc.Context,
 		}
-		assert.Equal(t, tc.Expected, template.render(), tc.Case)
+		text, err := template.render()
+		if tc.ShouldError {
+			assert.Error(t, err)
+			continue
+		}
+		assert.Equal(t, tc.Expected, text, tc.Case)
 	}
 }