diff --git a/segment.go b/segment.go index 612e04b7..48e42db1 100644 --- a/segment.go +++ b/segment.go @@ -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{ diff --git a/segment_nvm.go b/segment_nvm.go new file mode 100644 index 00000000..943bf52b --- /dev/null +++ b/segment_nvm.go @@ -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 +} diff --git a/segment_nvm_test.go b/segment_nvm_test.go new file mode 100755 index 00000000..3216ec17 --- /dev/null +++ b/segment_nvm_test.go @@ -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") +}