mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 20:09:39 -08:00
feat: npm segment
This commit is contained in:
parent
b512b1d487
commit
1e3937f5f2
44
docs/docs/segment-npm.md
Normal file
44
docs/docs/segment-npm.md
Normal file
|
@ -0,0 +1,44 @@
|
|||
---
|
||||
id: npm
|
||||
title: npm
|
||||
sidebar_label: NPM
|
||||
---
|
||||
|
||||
## What
|
||||
|
||||
Display the currently active npm version.
|
||||
|
||||
## Sample Configuration
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "npm",
|
||||
"style": "powerline",
|
||||
"powerline_symbol": "\uE0B0",
|
||||
"foreground": "#193549",
|
||||
"background": "#ffeb3b",
|
||||
"properties": {
|
||||
"template": "\ue71e {{ .Full }} "
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Template ([info][templates])
|
||||
|
||||
:::note default template
|
||||
|
||||
``` template
|
||||
\ue71e {{.Full}}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
### Properties
|
||||
|
||||
- `.Full`: `string` - the full version
|
||||
- `.Major`: `string` - major number
|
||||
- `.Minor`: `string` - minor number
|
||||
- `.Patch`: `string` - patch number
|
||||
- `.Error`: `string` - when fetching the version string errors
|
||||
|
||||
[templates]: /docs/config-templates
|
|
@ -164,6 +164,8 @@ const (
|
|||
SWIFT SegmentType = "swift"
|
||||
// cds (SAP CAP) version
|
||||
CDS SegmentType = "cds"
|
||||
// npm version
|
||||
NPM SegmentType = "npm"
|
||||
)
|
||||
|
||||
func (segment *Segment) shouldIncludeFolder() bool {
|
||||
|
@ -243,6 +245,7 @@ func (segment *Segment) background() string {
|
|||
func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error {
|
||||
segment.env = env
|
||||
functions := map[SegmentType]SegmentWriter{
|
||||
NPM: &segments.Npm{},
|
||||
OWM: &segments.Owm{},
|
||||
SESSION: &segments.Session{},
|
||||
PATH: &segments.Path{},
|
||||
|
|
34
src/segments/npm.go
Normal file
34
src/segments/npm.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package segments
|
||||
|
||||
import (
|
||||
"oh-my-posh/environment"
|
||||
"oh-my-posh/properties"
|
||||
)
|
||||
|
||||
type Npm struct {
|
||||
language
|
||||
}
|
||||
|
||||
func (n *Npm) Enabled() bool {
|
||||
return n.language.Enabled()
|
||||
}
|
||||
|
||||
func (n *Npm) Template() string {
|
||||
return " \ue71e {{.Full}} "
|
||||
}
|
||||
|
||||
func (n *Npm) Init(props properties.Properties, env environment.Environment) {
|
||||
n.language = language{
|
||||
env: env,
|
||||
props: props,
|
||||
extensions: []string{"package.json", "package-lock.json"},
|
||||
commands: []*cmd{
|
||||
{
|
||||
executable: "npm",
|
||||
args: []string{"--version"},
|
||||
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
|
||||
},
|
||||
},
|
||||
versionURLTemplate: "https://github.com/npm/npm/releases/tag/v{{ .Full }}",
|
||||
}
|
||||
}
|
31
src/segments/npm_test.go
Normal file
31
src/segments/npm_test.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package segments
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
)
|
||||
|
||||
func TestNpm(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
ExpectedString string
|
||||
Version string
|
||||
}{
|
||||
{Case: "1.0.0", ExpectedString: "\ue71e 1.0.0", Version: "1.0.0"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
params := &mockedLanguageParams{
|
||||
cmd: "npm",
|
||||
versionParam: "--version",
|
||||
versionOutput: tc.Version,
|
||||
extension: "package.json",
|
||||
}
|
||||
env, props := getMockedLanguageEnv(params)
|
||||
npm := &Npm{}
|
||||
npm.Init(props, env)
|
||||
assert.True(t, npm.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
|
||||
assert.Equal(t, tc.ExpectedString, renderTemplate(env, npm.Template(), npm), fmt.Sprintf("Failed in case: %s", tc.Case))
|
||||
}
|
||||
}
|
|
@ -198,7 +198,8 @@
|
|||
"haskell",
|
||||
"ui5tooling",
|
||||
"kotlin",
|
||||
"swift"
|
||||
"swift",
|
||||
"npm"
|
||||
]
|
||||
},
|
||||
"style": {
|
||||
|
@ -264,6 +265,17 @@
|
|||
}
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"type": { "const": "npm" }
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"title": "NPM Segment",
|
||||
"description": "https://ohmyposh.dev/docs/npm"
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
|
|
Loading…
Reference in a new issue