feat: add node version segment

This commit is contained in:
Jan De Dobbeleer 2020-09-17 16:43:45 +02:00 committed by Jan De Dobbeleer
parent 34094b0480
commit a639240f9c
3 changed files with 68 additions and 0 deletions

View file

@ -50,6 +50,8 @@ const (
Spotify SegmentType = "spotify"
//ShellInfo writes which shell we're currently in
ShellInfo SegmentType = "shell"
//NVM writes which node version is currently active using nvm
NVM SegmentType = "nvm"
//Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
@ -80,6 +82,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) *properties {
Battery: &batt{},
Spotify: &spotify{},
ShellInfo: &shell{},
NVM: &nvm{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

24
segment_nvm.go Normal file
View 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
View 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")
}