mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: add node version segment
This commit is contained in:
parent
34094b0480
commit
a639240f9c
|
@ -50,6 +50,8 @@ const (
|
||||||
Spotify SegmentType = "spotify"
|
Spotify SegmentType = "spotify"
|
||||||
//ShellInfo writes which shell we're currently in
|
//ShellInfo writes which shell we're currently in
|
||||||
ShellInfo SegmentType = "shell"
|
ShellInfo SegmentType = "shell"
|
||||||
|
//NVM writes which node version is currently active using nvm
|
||||||
|
NVM SegmentType = "nvm"
|
||||||
//Powerline writes it Powerline style
|
//Powerline writes it Powerline style
|
||||||
Powerline SegmentStyle = "powerline"
|
Powerline SegmentStyle = "powerline"
|
||||||
//Plain writes it without ornaments
|
//Plain writes it without ornaments
|
||||||
|
@ -80,6 +82,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) *properties {
|
||||||
Battery: &batt{},
|
Battery: &batt{},
|
||||||
Spotify: &spotify{},
|
Spotify: &spotify{},
|
||||||
ShellInfo: &shell{},
|
ShellInfo: &shell{},
|
||||||
|
NVM: &nvm{},
|
||||||
}
|
}
|
||||||
if writer, ok := functions[segment.Type]; ok {
|
if writer, ok := functions[segment.Type]; ok {
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
|
24
segment_nvm.go
Normal file
24
segment_nvm.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type nvm struct {
|
||||||
|
props *properties
|
||||||
|
env environmentInfo
|
||||||
|
nodeVersion string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *nvm) string() string {
|
||||||
|
return n.nodeVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *nvm) init(props *properties, env environmentInfo) {
|
||||||
|
n.props = props
|
||||||
|
n.env = env
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *nvm) enabled() bool {
|
||||||
|
if !n.env.hasCommand("node") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.nodeVersion = n.env.runCommand("node", "--version")
|
||||||
|
return true
|
||||||
|
}
|
41
segment_nvm_test.go
Executable file
41
segment_nvm_test.go
Executable file
|
@ -0,0 +1,41 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type nvmArgs struct {
|
||||||
|
enabled bool
|
||||||
|
nodeVersion string
|
||||||
|
}
|
||||||
|
|
||||||
|
func bootStrapNVMTest(args *nvmArgs) *nvm {
|
||||||
|
env := new(MockedEnvironment)
|
||||||
|
env.On("hasCommand", "node").Return(args.enabled)
|
||||||
|
env.On("runCommand", "node", []string{"--version"}).Return(args.nodeVersion)
|
||||||
|
nvm := &nvm{
|
||||||
|
env: env,
|
||||||
|
}
|
||||||
|
return nvm
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNVMWriterDisabled(t *testing.T) {
|
||||||
|
args := &nvmArgs{
|
||||||
|
enabled: false,
|
||||||
|
}
|
||||||
|
nvm := bootStrapNVMTest(args)
|
||||||
|
assert.False(t, nvm.enabled(), "the nvm command is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNVMWriterEnabled(t *testing.T) {
|
||||||
|
expected := "1.14"
|
||||||
|
args := &nvmArgs{
|
||||||
|
enabled: true,
|
||||||
|
nodeVersion: expected,
|
||||||
|
}
|
||||||
|
nvm := bootStrapNVMTest(args)
|
||||||
|
assert.True(t, nvm.enabled(), "the nvm command is available")
|
||||||
|
assert.Equal(t, expected, nvm.string(), "the nvm command is available")
|
||||||
|
}
|
Loading…
Reference in a new issue