mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 11:59:40 -08:00
parent
701d46cb73
commit
c03ee35107
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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` | <ul><li>`always`: The segment is always displayed</li><li>`files`: The segment is only displayed when one of the following files is present (**default**):<ul><li>`*.js`</li><li>`*.ts`</li><li>`package.json`</li><li>`.nvmrc`</li><li>`pnpm-workspace.yaml`</li><li>`.pnpmfile.cjs`</li><li>`.npmrc`</li><li>`.vue`</li></ul></li></ul> |
|
||||
| `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
|
||||
|
|
Loading…
Reference in a new issue