feat: node segment

This commit is contained in:
Jan De Dobbeleer 2020-10-01 20:57:02 +02:00 committed by Jan De Dobbeleer
parent 48be3f4c40
commit 758afdd83e
7 changed files with 116 additions and 48 deletions

View file

@ -73,6 +73,16 @@
"postfix": " "
}
},
{
"type": "node",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#6CA35E",
"properties": {
"prefix": "  "
}
},
{
"type": "shell",
"style": "powerline",

View file

@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
@ -15,6 +16,7 @@ import (
type environmentInfo interface {
getenv(key string) string
getwd() (string, error)
hasFiles(pattern string) bool
getPathSeperator() string
getCurrentUser() (*user.User, error)
isRunningAsRoot() bool
@ -41,6 +43,16 @@ func (env *environment) getwd() (string, error) {
return os.Getwd()
}
func (env *environment) hasFiles(pattern string) bool {
cwd, _ := env.getwd()
pattern = cwd + env.getPathSeperator() + pattern
matches, err := filepath.Glob(pattern)
if err != nil {
return false
}
return len(matches) > 0
}
func (env *environment) getPathSeperator() string {
return string(os.PathSeparator)
}

View file

@ -54,8 +54,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"
//Node writes which node version is currently active
Node SegmentType = "node"
//Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
@ -86,7 +86,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties,
Battery: &batt{},
Spotify: &spotify{},
ShellInfo: &shell{},
NVM: &nvm{},
Node: &node{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

View file

@ -1,21 +1,24 @@
package main
type nvm struct {
type node struct {
props *properties
env environmentInfo
nodeVersion string
}
func (n *nvm) string() string {
func (n *node) string() string {
return n.nodeVersion
}
func (n *nvm) init(props *properties, env environmentInfo) {
func (n *node) init(props *properties, env environmentInfo) {
n.props = props
n.env = env
}
func (n *nvm) enabled() bool {
func (n *node) enabled() bool {
if !n.env.hasFiles("*.js") && !n.env.hasFiles("*.ts") {
return false
}
if !n.env.hasCommand("node") {
return false
}

79
segment_node_test.go Executable file
View file

@ -0,0 +1,79 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
type nodeArgs struct {
enabled bool
nodeVersion string
hasJS bool
hasTS bool
}
func bootStrapNodeTest(args *nodeArgs) *node {
env := new(MockedEnvironment)
env.On("hasCommand", "node").Return(args.enabled)
env.On("runCommand", "node", []string{"--version"}).Return(args.nodeVersion)
env.On("hasFiles", "*.js").Return(args.hasJS)
env.On("hasFiles", "*.ts").Return(args.hasTS)
n := &node{
env: env,
}
return n
}
func TestNodeWriterDisabled(t *testing.T) {
args := &nodeArgs{
enabled: false,
}
node := bootStrapNodeTest(args)
assert.False(t, node.enabled(), "node is not available")
}
func TestNodeWriterDisabledNoJSorTSFiles(t *testing.T) {
args := &nodeArgs{
enabled: true,
}
node := bootStrapNodeTest(args)
assert.False(t, node.enabled(), "no JS or TS files in the current directory")
}
func TestNodeEnabledJSFiles(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasJS: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "node is available and JS files are found")
}
func TestNodeEnabledTsFiles(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasTS: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "node is available and TS files are found")
}
func TestNodeEnabledJsAndTsFiles(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasJS: true,
hasTS: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "node is available and JS and TS files are found")
}

View file

@ -1,41 +0,0 @@
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")
}

View file

@ -25,6 +25,11 @@ func (env *MockedEnvironment) getwd() (string, error) {
return args.String(0), args.Error(1)
}
func (env *MockedEnvironment) hasFiles(pattern string) bool {
args := env.Called(pattern)
return args.Bool(0)
}
func (env *MockedEnvironment) getPathSeperator() string {
args := env.Called(nil)
return args.String(0)