feat: npm segment

This commit is contained in:
Elio Struyf 2022-03-10 18:41:48 +01:00 committed by Jan De Dobbeleer
parent b512b1d487
commit 1e3937f5f2
5 changed files with 125 additions and 1 deletions

44
docs/docs/segment-npm.md Normal file
View 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

View file

@ -164,6 +164,8 @@ const (
SWIFT SegmentType = "swift" SWIFT SegmentType = "swift"
// cds (SAP CAP) version // cds (SAP CAP) version
CDS SegmentType = "cds" CDS SegmentType = "cds"
// npm version
NPM SegmentType = "npm"
) )
func (segment *Segment) shouldIncludeFolder() bool { func (segment *Segment) shouldIncludeFolder() bool {
@ -243,6 +245,7 @@ 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{
NPM: &segments.Npm{},
OWM: &segments.Owm{}, OWM: &segments.Owm{},
SESSION: &segments.Session{}, SESSION: &segments.Session{},
PATH: &segments.Path{}, PATH: &segments.Path{},

34
src/segments/npm.go Normal file
View 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
View 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))
}
}

View file

@ -198,7 +198,8 @@
"haskell", "haskell",
"ui5tooling", "ui5tooling",
"kotlin", "kotlin",
"swift" "swift",
"npm"
] ]
}, },
"style": { "style": {
@ -264,6 +265,17 @@
} }
}, },
"allOf": [ "allOf": [
{
"if": {
"properties": {
"type": { "const": "npm" }
}
},
"then": {
"title": "NPM Segment",
"description": "https://ohmyposh.dev/docs/npm"
}
},
{ {
"if": { "if": {
"properties": { "properties": {