diff --git a/src/segments/language.go b/src/segments/language.go index 4ab55d2b..1cc89ce2 100644 --- a/src/segments/language.go +++ b/src/segments/language.go @@ -20,7 +20,7 @@ type loadContext func() type inContext func() bool type getVersion func() (string, error) -type matchesVersionFile func() bool +type matchesVersionFile func() (string, bool) type version struct { Full string @@ -31,6 +31,7 @@ type version struct { BuildMetadata string URL string Executable string + Expected string } type cmd struct { @@ -128,13 +129,19 @@ func (l *language) Enabled() bool { enabled = l.hasLanguageFiles() || l.hasLanguageFolders() || l.inLanguageContext() } } + if !enabled || !l.props.GetBool(properties.FetchVersion, true) { return enabled } + err := l.setVersion() if err != nil { l.Error = err.Error() } + + if l.matchesVersionFile != nil { + l.version.Expected, l.Mismatch = l.matchesVersionFile() + } return enabled } diff --git a/src/segments/node.go b/src/segments/node.go index 06a31b65..3ebb1c51 100644 --- a/src/segments/node.go +++ b/src/segments/node.go @@ -2,6 +2,7 @@ package segments import ( "fmt" + "strings" "github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/properties" @@ -46,12 +47,7 @@ func (n *Node) Init(props properties.Properties, env platform.Environment) { } func (n *Node) Enabled() bool { - if n.language.Enabled() { - n.Mismatch = !n.matchesVersionFile() - return true - } - - return false + return n.language.Enabled() } func (n *Node) loadContext() { @@ -67,10 +63,10 @@ func (n *Node) loadContext() { } } -func (n *Node) matchesVersionFile() bool { +func (n *Node) matchesVersionFile() (string, bool) { fileVersion := n.language.env.FileContent(".nvmrc") if len(fileVersion) == 0 { - return true + return "", true } re := fmt.Sprintf( @@ -80,5 +76,7 @@ func (n *Node) matchesVersionFile() bool { n.language.version.Patch, ) - return regex.MatchString(re, fileVersion) + version := strings.TrimPrefix(fileVersion, "v") + + return version, regex.MatchString(re, fileVersion) } diff --git a/src/segments/node_test.go b/src/segments/node_test.go index d5ee3078..ca4d14a2 100644 --- a/src/segments/node_test.go +++ b/src/segments/node_test.go @@ -17,18 +17,19 @@ func TestNodeMatchesVersionFile(t *testing.T) { Patch: "3", } cases := []struct { - Case string - Expected bool - RCVersion string + Case string + Expected bool + ExpectedVersion string + RCVersion string }{ {Case: "no file context", Expected: true, RCVersion: ""}, - {Case: "version match", Expected: true, RCVersion: "1.2.3"}, - {Case: "version mismatch", Expected: false, RCVersion: "3.2.1"}, - {Case: "version match in other format", Expected: true, RCVersion: "v1.2.3"}, - {Case: "version match without patch", Expected: true, RCVersion: "1.2"}, - {Case: "version match without patch in other format", Expected: true, RCVersion: "v1.2"}, - {Case: "version match without minor", Expected: true, RCVersion: "1"}, - {Case: "version match without minor in other format", Expected: true, RCVersion: "v1"}, + {Case: "version match", Expected: true, ExpectedVersion: "1.2.3", RCVersion: "1.2.3"}, + {Case: "version mismatch", Expected: false, ExpectedVersion: "3.2.1", RCVersion: "3.2.1"}, + {Case: "version match in other format", Expected: true, ExpectedVersion: "1.2.3", RCVersion: "v1.2.3"}, + {Case: "version match without patch", Expected: true, ExpectedVersion: "1.2", RCVersion: "1.2"}, + {Case: "version match without patch in other format", Expected: true, ExpectedVersion: "1.2", RCVersion: "v1.2"}, + {Case: "version match without minor", Expected: true, ExpectedVersion: "1", RCVersion: "1"}, + {Case: "version match without minor in other format", Expected: true, ExpectedVersion: "1", RCVersion: "v1"}, } for _, tc := range cases { @@ -41,7 +42,9 @@ func TestNodeMatchesVersionFile(t *testing.T) { version: nodeVersion, }, } - assert.Equal(t, tc.Expected, node.matchesVersionFile(), tc.Case) + version, match := node.matchesVersionFile() + assert.Equal(t, tc.Expected, match, tc.Case) + assert.Equal(t, tc.ExpectedVersion, version, tc.Case) } } diff --git a/website/docs/segments/node.mdx b/website/docs/segments/node.mdx index 6b6c7439..c7b4efb0 100644 --- a/website/docs/segments/node.mdx +++ b/website/docs/segments/node.mdx @@ -23,16 +23,16 @@ Display the currently active [Node.js][node-js] version. ## Properties -| Name | Type | Description | -| ----------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` | -| `fetch_version` | `boolean` | display the Node.js version - defaults to `true` | -| `missing_command_text` | `string` | text to display when the command is missing - defaults to empty | +| Name | Type | Description | +| ----------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` | +| `fetch_version` | `boolean` | display the Node.js version - defaults to `true` | +| `missing_command_text` | `string` | text to display when the command is missing - defaults to empty | | `display_mode` | `string` | | -| `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes | -| `fetch_package_manager` | `boolean` | define if the current project uses Yarn or NPM - defaults to `false` | -| `yarn_icon` | `string` | the icon/text to display when using Yarn - defaults to ` \uF61A` | -| `npm_icon` | `string` | the icon/text to display when using NPM - defaults to ` \uE71E` | +| `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes | +| `fetch_package_manager` | `boolean` | define if the current project uses Yarn or NPM - defaults to `false` | +| `yarn_icon` | `string` | the icon/text to display when using Yarn - defaults to ` \uF61A` | +| `npm_icon` | `string` | the icon/text to display when using NPM - defaults to ` \uE71E` | ## Template ([info][templates]) @@ -56,6 +56,7 @@ Display the currently active [Node.js][node-js] version. | `.Error` | `string` | error encountered when fetching the version string | | `.PackageManagerIcon` | `string` | the Yarn or NPM icon when setting `fetch_package_manager` to `true` | | `.Mismatch` | `boolean` | if the version in `.nvmrc` matches with `.Full` | +| `.Expected` | `string` | the expected version set in `.nvmrc` | [go-text-template]: https://golang.org/pkg/text/template/ [templates]: /docs/configuration/templates