refactor: move segments to module

This commit is contained in:
Jan De Dobbeleer 2022-01-26 15:54:36 +01:00 committed by Jan De Dobbeleer
parent 4e17e4a853
commit 9e7abe4541
112 changed files with 267 additions and 249 deletions

View file

@ -6,7 +6,7 @@ sidebar_label: Add Segment
## Create the logic ## Create the logic
Add a new file following this convention: `new_segment.go`. Add a new file in the `./src/segments` folder: `new.go`.
Ensure `New` is a single verb indicating the context the segment renders. Ensure `New` is a single verb indicating the context the segment renders.
You can use the following template as a guide. You can use the following template as a guide.
@ -50,7 +50,7 @@ This will facilitate the review process as not all environments display the icon
You can find these values and query for icons easily at [Nerd Fonts][nf-icons]. You can find these values and query for icons easily at [Nerd Fonts][nf-icons].
For each segment, there's a single test file ensuring the functionality going forward. The convention For each segment, there's a single test file ensuring the functionality going forward. The convention
is `new_segment_test.go`, have a look at [existing segment tests][tests] for inspiration. Oh My Posh makes is `new_test.go`, have a look at [existing segment tests][tests] for inspiration. Oh My Posh makes
use of the test tables pattern for all newly added tests. See [this][tables] blog post for more information. use of the test tables pattern for all newly added tests. See [this][tables] blog post for more information.
## Create a name for your Segment ## Create a name for your Segment
@ -168,5 +168,5 @@ And be patient, I'm going as fast as I can 🏎
[docs]: https://github.com/JanDeDobbeleer/oh-my-posh/tree/main/docs/docs [docs]: https://github.com/JanDeDobbeleer/oh-my-posh/tree/main/docs/docs
[sidebars]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/docs/sidebars.js [sidebars]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/docs/sidebars.js
[nf-icons]: https://www.nerdfonts.com/cheat-sheet [nf-icons]: https://www.nerdfonts.com/cheat-sheet
[tests]: hhttps://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/segment_az_test.go [tests]: hhttps://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/segments/az_test.go
[tables]: https://blog.alexellis.io/golang-writing-unit-tests/ [tables]: https://blog.alexellis.io/golang-writing-unit-tests/

View file

@ -10,6 +10,7 @@ import (
"oh-my-posh/color" "oh-my-posh/color"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/segments"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -48,15 +49,6 @@ type TransientPrompt struct {
Foreground string `config:"foreground"` Foreground string `config:"foreground"`
} }
const (
// HTTPTimeout timeout used when executing http request
HTTPTimeout properties.Property = "http_timeout"
// DefaultHTTPTimeout default timeout used when executing http request
DefaultHTTPTimeout = 20
// DefaultCacheTimeout default timeout used when caching data
DefaultCacheTimeout = 10
)
func printConfigError(err error, eval bool) { func printConfigError(err error, eval bool) {
if eval { if eval {
fmt.Println("echo \"Oh My Posh Error:\n\"", err.Error()) fmt.Println("echo \"Oh My Posh Error:\n\"", err.Error())
@ -208,8 +200,8 @@ func getDefaultConfig(info string) *Config {
Background: "#fffb38", Background: "#fffb38",
Foreground: "#193549", Foreground: "#193549",
Properties: properties.Map{ Properties: properties.Map{
FetchStashCount: true, segments.FetchStashCount: true,
FetchUpstreamIcon: true, segments.FetchUpstreamIcon: true,
}, },
}, },
{ {

View file

@ -1,9 +1,11 @@
package main package main
import ( import (
"oh-my-posh/segments"
"testing" "testing"
"github.com/gookit/config/v2" "github.com/gookit/config/v2"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -17,3 +19,29 @@ func TestSettingsExportJSON(t *testing.T) {
func testClearDefaultConfig() { func testClearDefaultConfig() {
config.Default().ClearAll() config.Default().ClearAll()
} }
func TestParseMappedLocations(t *testing.T) {
defer testClearDefaultConfig()
cases := []struct {
Case string
JSON string
}{
{Case: "new format", JSON: `{ "properties": { "mapped_locations": {"folder1": "one","folder2": "two"} } }`},
{Case: "old format", JSON: `{ "properties": { "mapped_locations": [["folder1", "one"], ["folder2", "two"]] } }`},
}
for _, tc := range cases {
config.ClearAll()
config.WithOptions(func(opt *config.Options) {
opt.DecoderConfig = &mapstructure.DecoderConfig{
TagName: "config",
}
})
err := config.LoadStrings(config.JSON, tc.JSON)
assert.NoError(t, err)
var segment Segment
err = config.BindStruct("", &segment)
assert.NoError(t, err)
mappedLocations := segment.Properties.GetKeyValueMap(segments.MappedLocations, make(map[string]string))
assert.Equal(t, "two", mappedLocations["folder2"])
}
}

View file

@ -3,6 +3,7 @@ package main
import ( import (
"oh-my-posh/color" "oh-my-posh/color"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/template"
"strings" "strings"
) )
@ -41,11 +42,11 @@ func (t *consoleTitle) getConsoleTitle() string {
} }
func (t *consoleTitle) getTemplateText() string { func (t *consoleTitle) getTemplateText() string {
template := &textTemplate{ tmpl := &template.Text{
Template: t.config.ConsoleTitleTemplate, Template: t.config.ConsoleTitleTemplate,
Env: t.env, Env: t.env,
} }
if text, err := template.render(); err == nil { if text, err := tmpl.Render(); err == nil {
return text return text
} }
return "" return ""

View file

@ -0,0 +1,7 @@
//go:build !windows
package constants
const (
DotnetExitCode = 142
)

View file

@ -0,0 +1,7 @@
//go:build windows && !386
package constants
const (
DotnetExitCode = int(0x80008091)
)

View file

@ -0,0 +1,7 @@
//go:build windows && 386
package constants
const (
DotnetExitCode = -2147450735
)

View file

@ -1,7 +0,0 @@
//go:build !windows
package main
const (
dotnetExitCode = 142
)

View file

@ -1,7 +0,0 @@
//go:build windows && !386
package main
const (
dotnetExitCode = int(0x80008091)
)

View file

@ -1,7 +0,0 @@
//go:build windows && 386
package main
const (
dotnetExitCode = -2147450735
)

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"oh-my-posh/color" "oh-my-posh/color"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/template"
"strings" "strings"
"time" "time"
) )
@ -240,11 +241,11 @@ func (e *engine) renderTransientPrompt() string {
if len(promptTemplate) == 0 { if len(promptTemplate) == 0 {
promptTemplate = "{{ .Shell }}> " promptTemplate = "{{ .Shell }}> "
} }
template := &textTemplate{ tmpl := &template.Text{
Template: promptTemplate, Template: promptTemplate,
Env: e.env, Env: e.env,
} }
prompt, err := template.render() prompt, err := tmpl.Render()
if err != nil { if err != nil {
prompt = err.Error() prompt = err.Error()
} }

View file

@ -7,6 +7,7 @@ import (
"oh-my-posh/color" "oh-my-posh/color"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/regex" "oh-my-posh/regex"
"oh-my-posh/template"
"os" "os"
"strings" "strings"
"time" "time"
@ -311,12 +312,12 @@ func getConsoleBackgroundColor(env environment.Environment, backgroundColorTempl
if len(backgroundColorTemplate) == 0 { if len(backgroundColorTemplate) == 0 {
return backgroundColorTemplate return backgroundColorTemplate
} }
template := &textTemplate{ tmpl := &template.Text{
Template: backgroundColorTemplate, Template: backgroundColorTemplate,
Context: nil, Context: nil,
Env: env, Env: env,
} }
text, err := template.render() text, err := tmpl.Render()
if err != nil { if err != nil {
return err.Error() return err.Error()
} }

View file

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/segments"
"oh-my-posh/template"
"runtime/debug" "runtime/debug"
"time" "time"
) )
@ -148,12 +150,12 @@ const (
) )
func (segment *Segment) string() string { func (segment *Segment) string() string {
template := &textTemplate{ tmpl := &template.Text{
Template: segment.writer.Template(), Template: segment.writer.Template(),
Context: segment.writer, Context: segment.writer,
Env: segment.env, Env: segment.env,
} }
text, err := template.render() text, err := tmpl.Render()
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
@ -208,13 +210,13 @@ func (segment *Segment) getColor(templates []string, defaultColor string) string
if len(templates) == 0 { if len(templates) == 0 {
return defaultColor return defaultColor
} }
txtTemplate := &textTemplate{ txtTemplate := &template.Text{
Context: segment.writer, Context: segment.writer,
Env: segment.env, Env: segment.env,
} }
for _, template := range templates { for _, tmpl := range templates {
txtTemplate.Template = template txtTemplate.Template = tmpl
value, err := txtTemplate.render() value, err := txtTemplate.Render()
if err != nil || value == "" { if err != nil || value == "" {
continue continue
} }
@ -243,49 +245,49 @@ func (segment *Segment) background() string {
func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error { func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error {
segment.env = env segment.env = env
functions := map[SegmentType]SegmentWriter{ functions := map[SegmentType]SegmentWriter{
OWM: &Owm{}, OWM: &segments.Owm{},
SESSION: &Session{}, SESSION: &segments.Session{},
PATH: &Path{}, PATH: &segments.Path{},
GIT: &Git{}, GIT: &segments.Git{},
PLASTIC: &Plastic{}, PLASTIC: &segments.Plastic{},
EXIT: &Exit{}, EXIT: &segments.Exit{},
PYTHON: &Python{}, PYTHON: &segments.Python{},
ROOT: &Root{}, ROOT: &segments.Root{},
TEXT: &Text{}, TEXT: &segments.Text{},
TIME: &Time{}, TIME: &segments.Time{},
CMD: &Cmd{}, CMD: &segments.Cmd{},
BATTERY: &Battery{}, BATTERY: &segments.Battery{},
SPOTIFY: &Spotify{}, SPOTIFY: &segments.Spotify{},
SHELL: &Shell{}, SHELL: &segments.Shell{},
NODE: &Node{}, NODE: &segments.Node{},
OS: &Os{}, OS: &segments.Os{},
AZ: &Az{}, AZ: &segments.Az{},
KUBECTL: &Kubectl{}, KUBECTL: &segments.Kubectl{},
DOTNET: &Dotnet{}, DOTNET: &segments.Dotnet{},
TERRAFORM: &Terraform{}, TERRAFORM: &segments.Terraform{},
GOLANG: &Golang{}, GOLANG: &segments.Golang{},
JULIA: &Julia{}, JULIA: &segments.Julia{},
YTM: &Ytm{}, YTM: &segments.Ytm{},
EXECUTIONTIME: &Executiontime{}, EXECUTIONTIME: &segments.Executiontime{},
RUBY: &Ruby{}, RUBY: &segments.Ruby{},
AWS: &Aws{}, AWS: &segments.Aws{},
JAVA: &Java{}, JAVA: &segments.Java{},
POSHGIT: &PoshGit{}, POSHGIT: &segments.PoshGit{},
AZFUNC: &AzFunc{}, AZFUNC: &segments.AzFunc{},
CRYSTAL: &Crystal{}, CRYSTAL: &segments.Crystal{},
DART: &Dart{}, DART: &segments.Dart{},
NBGV: &Nbgv{}, NBGV: &segments.Nbgv{},
RUST: &Rust{}, RUST: &segments.Rust{},
SYSTEMINFO: &SystemInfo{}, SYSTEMINFO: &segments.SystemInfo{},
ANGULAR: &Angular{}, ANGULAR: &segments.Angular{},
PHP: &Php{}, PHP: &segments.Php{},
NIGHTSCOUT: &Nightscout{}, NIGHTSCOUT: &segments.Nightscout{},
STRAVA: &Strava{}, STRAVA: &segments.Strava{},
WAKATIME: &Wakatime{}, WAKATIME: &segments.Wakatime{},
WIFI: &Wifi{}, WIFI: &segments.Wifi{},
WINREG: &WindowsRegistry{}, WINREG: &segments.WindowsRegistry{},
BREWFATHER: &Brewfather{}, BREWFATHER: &segments.Brewfather{},
IPIFY: &IPify{}, IPIFY: &segments.IPify{},
} }
if segment.Properties == nil { if segment.Properties == nil {
segment.Properties = make(properties.Map) segment.Properties = make(properties.Map)

View file

@ -5,6 +5,7 @@ import (
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/segments"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -198,7 +199,7 @@ func TestGetColors(t *testing.T) {
Env: make(map[string]string), Env: make(map[string]string),
}) })
segment := &Segment{ segment := &Segment{
writer: &Aws{ writer: &segments.Aws{
Profile: tc.Profile, Profile: tc.Profile,
Region: tc.Region, Region: tc.Region,
}, },

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,8 +1,9 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/template"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -44,7 +45,7 @@ func TestAWSSegment(t *testing.T) {
Region: "eu-west", Region: "eu-west",
Template: "profile: {{.Profile}}{{if .Region}} in {{.Region}}{{end}}", Template: "profile: {{.Profile}}{{if .Region}} in {{.Region}}{{end}}",
}, },
{Case: "template: invalid", ExpectedString: invalidTemplate, ExpectedEnabled: true, Profile: "c", Template: "{{ .Burp"}, {Case: "template: invalid", ExpectedString: template.InvalidTemplate, ExpectedEnabled: true, Profile: "c", Template: "{{ .Burp"},
} }
for _, tc := range cases { for _, tc := range cases {

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,10 +1,11 @@
package main package segments
import ( import (
"io/ioutil" "io/ioutil"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/template"
"path/filepath" "path/filepath"
"testing" "testing"
@ -49,7 +50,7 @@ func TestAzSegment(t *testing.T) {
{ {
Case: "Faulty template", Case: "Faulty template",
ExpectedEnabled: true, ExpectedEnabled: true,
ExpectedString: incorrectTemplate, ExpectedString: template.IncorrectTemplate,
Template: "{{ .Burp }}", Template: "{{ .Burp }}",
HasPowerShell: true, HasPowerShell: true,
}, },
@ -75,15 +76,15 @@ func TestAzSegment(t *testing.T) {
env.On("Home").Return(home) env.On("Home").Return(home)
var azureProfile, azureRmContext, azureRMContext string var azureProfile, azureRmContext, azureRMContext string
if tc.HasCLI { if tc.HasCLI {
content, _ := ioutil.ReadFile("./test/azureProfile.json") content, _ := ioutil.ReadFile("../test/azureProfile.json")
azureProfile = string(content) azureProfile = string(content)
} }
if tc.HasPowerShell { if tc.HasPowerShell {
content, _ := ioutil.ReadFile("./test/AzureRmContext.json") content, _ := ioutil.ReadFile("../test/AzureRmContext.json")
azureRmContext = string(content) azureRmContext = string(content)
} }
if tc.HasPowerShellUnix { if tc.HasPowerShellUnix {
content, _ := ioutil.ReadFile("./test/AzureRmContext.json") content, _ := ioutil.ReadFile("../test/AzureRmContext.json")
azureRMContext = string(content) azureRMContext = string(content)
} }
env.On("GOOS").Return(environment.LinuxPlatform) env.On("GOOS").Return(environment.LinuxPlatform)

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"math" "math"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"testing" "testing"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/base64" "encoding/base64"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,6 +1,7 @@
package main package segments
import ( import (
"oh-my-posh/constants"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
) )
@ -37,6 +38,6 @@ func (d *Dotnet) Enabled() bool {
if !enabled { if !enabled {
return false return false
} }
d.Unsupported = d.language.exitCode == dotnetExitCode d.Unsupported = d.language.exitCode == constants.DotnetExitCode
return true return true
} }

View file

@ -1,6 +1,7 @@
package main package segments
import ( import (
"oh-my-posh/constants"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
@ -18,7 +19,7 @@ func TestDotnetSegment(t *testing.T) {
Version string Version string
FetchVersion bool FetchVersion bool
}{ }{
{Case: "Unsupported version", Expected: "\uf071", HasCommand: true, FetchVersion: true, ExitCode: dotnetExitCode, Version: "3.1.402"}, {Case: "Unsupported version", Expected: "\uf071", HasCommand: true, FetchVersion: true, ExitCode: constants.DotnetExitCode, Version: "3.1.402"},
{Case: "Regular version", Expected: "3.1.402", HasCommand: true, FetchVersion: true, Version: "3.1.402"}, {Case: "Regular version", Expected: "3.1.402", HasCommand: true, FetchVersion: true, Version: "3.1.402"},
{Case: "Regular version", Expected: "", HasCommand: true, FetchVersion: false, Version: "3.1.402"}, {Case: "Regular version", Expected: "", HasCommand: true, FetchVersion: false, Version: "3.1.402"},
{Case: "Regular version", Expected: "", HasCommand: false, FetchVersion: false, Version: "3.1.402"}, {Case: "Regular version", Expected: "", HasCommand: false, FetchVersion: false, Version: "3.1.402"},

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"errors" "errors"
@ -53,7 +53,7 @@ func TestGolang(t *testing.T) {
ParseModFile: true, ParseModFile: true,
HasModFileInParentDir: true, HasModFileInParentDir: true,
InvalidModfile: true, InvalidModfile: true,
ExpectedString: "./go.mod:1: unknown directive: invalid", ExpectedString: "../go.mod:1: unknown directive: invalid",
Version: "go version go1.16 darwin/amd64", Version: "go version go1.16 darwin/amd64",
}, },
} }
@ -68,7 +68,7 @@ func TestGolang(t *testing.T) {
if tc.ParseModFile { if tc.ParseModFile {
props[ParseModFile] = tc.ParseModFile props[ParseModFile] = tc.ParseModFile
fileInfo := &environment.FileInfo{ fileInfo := &environment.FileInfo{
Path: "./go.mod", Path: "../go.mod",
ParentFolder: "./", ParentFolder: "./",
IsDir: false, IsDir: false,
} }

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"
@ -111,7 +111,7 @@ func TestKubectlSegment(t *testing.T) {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("HasCommand", "kubectl").Return(tc.KubectlExists) env.On("HasCommand", "kubectl").Return(tc.KubectlExists)
var kubeconfig string var kubeconfig string
content, err := ioutil.ReadFile("./test/kubectl.yml") content, err := ioutil.ReadFile("../test/kubectl.yml")
if err == nil { if err == nil {
kubeconfig = fmt.Sprintf(string(content), tc.Cluster, tc.UserName, tc.Namespace, tc.Context) kubeconfig = fmt.Sprintf(string(content), tc.Cluster, tc.UserName, tc.Namespace, tc.Context)
} }

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"errors" "errors"
@ -6,6 +6,7 @@ import (
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/regex" "oh-my-posh/regex"
"oh-my-posh/template"
) )
const ( const (
@ -196,12 +197,12 @@ func (l *language) buildVersionURL() {
if len(versionURLTemplate) == 0 { if len(versionURLTemplate) == 0 {
return return
} }
template := &textTemplate{ tmpl := &template.Text{
Template: versionURLTemplate, Template: versionURLTemplate,
Context: l.version, Context: l.version,
Env: l.env, Env: l.env,
} }
url, err := template.render() url, err := tmpl.Render()
if err != nil { if err != nil {
return return
} }

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,9 +1,10 @@
package main package segments
import ( import (
"errors" "errors"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/template"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -124,7 +125,7 @@ func TestNSSegment(t *testing.T) {
JSONResponse: ` JSONResponse: `
[{"sgv":50,"direction":"DoubleDown"}]`, [{"sgv":50,"direction":"DoubleDown"}]`,
Template: " {{.Sgv}}{{.Burp}}", Template: " {{.Sgv}}{{.Burp}}",
ExpectedString: incorrectTemplate, ExpectedString: template.IncorrectTemplate,
ExpectedEnabled: true, ExpectedEnabled: true,
CacheTimeout: 10, CacheTimeout: 10,
}, },

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,13 +1,12 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/template"
"testing" "testing"
"github.com/gookit/config/v2"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -24,12 +23,12 @@ func renderTemplate(env *mock.MockedEnvironment, segmentTemplate string, context
Env: make(map[string]string), Env: make(map[string]string),
}) })
} }
template := &textTemplate{ tmpl := &template.Text{
Template: segmentTemplate, Template: segmentTemplate,
Context: context, Context: context,
Env: env, Env: env,
} }
text, err := template.render() text, err := tmpl.Render()
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
@ -628,29 +627,3 @@ func TestGetPwd(t *testing.T) {
assert.Equal(t, tc.Expected, got) assert.Equal(t, tc.Expected, got)
} }
} }
func TestParseMappedLocations(t *testing.T) {
defer testClearDefaultConfig()
cases := []struct {
Case string
JSON string
}{
{Case: "new format", JSON: `{ "properties": { "mapped_locations": {"folder1": "one","folder2": "two"} } }`},
{Case: "old format", JSON: `{ "properties": { "mapped_locations": [["folder1", "one"], ["folder2", "two"]] } }`},
}
for _, tc := range cases {
config.ClearAll()
config.WithOptions(func(opt *config.Options) {
opt.DecoderConfig = &mapstructure.DecoderConfig{
TagName: "config",
}
})
err := config.LoadStrings(config.JSON, tc.JSON)
assert.NoError(t, err)
var segment Segment
err = config.BindStruct("", &segment)
assert.NoError(t, err)
mappedLocations := segment.Properties.GetKeyValueMap(MappedLocations, make(map[string]string))
assert.Equal(t, "two", mappedLocations["folder2"])
}
}

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -0,0 +1,12 @@
package segments
import "oh-my-posh/properties"
const (
// HTTPTimeout timeout used when executing http request
HTTPTimeout properties.Property = "http_timeout"
// DefaultHTTPTimeout default timeout used when executing http request
DefaultHTTPTimeout = 20
// DefaultCacheTimeout default timeout used when caching data
DefaultCacheTimeout = 10
)

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,6 +1,6 @@
//go:build darwin //go:build darwin
package main package segments
func (s *Spotify) Enabled() bool { func (s *Spotify) Enabled() bool {
var err error var err error

View file

@ -1,6 +1,6 @@
//go:build darwin //go:build darwin
package main package segments
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,6 +1,6 @@
//go:build windows //go:build windows
package main package segments
import ( import (
"strings" "strings"

View file

@ -1,6 +1,6 @@
//go:build windows //go:build windows
package main package segments
import ( import (
"errors" "errors"

View file

@ -1,13 +1,13 @@
//go:build !darwin && !windows //go:build !darwin && !windows
package main package segments
import ( import (
"encoding/csv" "encoding/csv"
"strings" "strings"
) )
func (s *spotify) Enabled() bool { func (s *Spotify) Enabled() bool {
if !s.env.IsWsl() { if !s.env.IsWsl() {
return false return false
} }

View file

@ -1,6 +1,6 @@
//go:build !darwin && !windows //go:build !darwin && !windows
package main package segments
import ( import (
"fmt" "fmt"
@ -57,7 +57,7 @@ func TestSpotifyWsl(t *testing.T) {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("IsWsl").Return(true) env.On("IsWsl").Return(true)
env.On("RunCommand", "tasklist.exe", []string{"/V", "/FI", "Imagename eq Spotify.exe", "/FO", "CSV", "/NH"}).Return(tc.ExecOutput, nil) env.On("RunCommand", "tasklist.exe", []string{"/V", "/FI", "Imagename eq Spotify.exe", "/FO", "CSV", "/NH"}).Return(tc.ExecOutput, nil)
s := &spotify{ s := &Spotify{
env: env, env: env,
props: properties.Map{}, props: properties.Map{},
} }

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,10 +1,11 @@
package main package segments
import ( import (
"errors" "errors"
"fmt" "fmt"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/template"
"testing" "testing"
"time" "time"
@ -130,7 +131,7 @@ func TestStravaSegment(t *testing.T) {
JSONResponse: ` JSONResponse: `
[{"sgv":50,"direction":"DoubleDown"}]`, [{"sgv":50,"direction":"DoubleDown"}]`,
Template: "{{.Ago}}{{.Burp}}", Template: "{{.Ago}}{{.Burp}}",
ExpectedString: incorrectTemplate, ExpectedString: template.IncorrectTemplate,
ExpectedEnabled: true, ExpectedEnabled: true,
CacheTimeout: 10, CacheTimeout: 10,
}, },

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/mock" "oh-my-posh/mock"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

View file

@ -1,4 +1,4 @@
package main package segments
import ( import (
"oh-my-posh/environment" "oh-my-posh/environment"

Some files were not shown because too many files have changed in this diff Show more