mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: node segment
This commit is contained in:
parent
48be3f4c40
commit
758afdd83e
|
@ -73,6 +73,16 @@
|
||||||
"postfix": " "
|
"postfix": " "
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"style": "powerline",
|
||||||
|
"powerline_symbol": "",
|
||||||
|
"foreground": "#ffffff",
|
||||||
|
"background": "#6CA35E",
|
||||||
|
"properties": {
|
||||||
|
"prefix": " "
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"style": "powerline",
|
"style": "powerline",
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ import (
|
||||||
type environmentInfo interface {
|
type environmentInfo interface {
|
||||||
getenv(key string) string
|
getenv(key string) string
|
||||||
getwd() (string, error)
|
getwd() (string, error)
|
||||||
|
hasFiles(pattern string) bool
|
||||||
getPathSeperator() string
|
getPathSeperator() string
|
||||||
getCurrentUser() (*user.User, error)
|
getCurrentUser() (*user.User, error)
|
||||||
isRunningAsRoot() bool
|
isRunningAsRoot() bool
|
||||||
|
@ -41,6 +43,16 @@ func (env *environment) getwd() (string, error) {
|
||||||
return os.Getwd()
|
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 {
|
func (env *environment) getPathSeperator() string {
|
||||||
return string(os.PathSeparator)
|
return string(os.PathSeparator)
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,8 +54,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
|
//Node writes which node version is currently active
|
||||||
NVM SegmentType = "nvm"
|
Node SegmentType = "node"
|
||||||
//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
|
||||||
|
@ -86,7 +86,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties,
|
||||||
Battery: &batt{},
|
Battery: &batt{},
|
||||||
Spotify: &spotify{},
|
Spotify: &spotify{},
|
||||||
ShellInfo: &shell{},
|
ShellInfo: &shell{},
|
||||||
NVM: &nvm{},
|
Node: &node{},
|
||||||
}
|
}
|
||||||
if writer, ok := functions[segment.Type]; ok {
|
if writer, ok := functions[segment.Type]; ok {
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
|
|
@ -1,21 +1,24 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type nvm struct {
|
type node struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
nodeVersion string
|
nodeVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *nvm) string() string {
|
func (n *node) string() string {
|
||||||
return n.nodeVersion
|
return n.nodeVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *nvm) init(props *properties, env environmentInfo) {
|
func (n *node) init(props *properties, env environmentInfo) {
|
||||||
n.props = props
|
n.props = props
|
||||||
n.env = env
|
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") {
|
if !n.env.hasCommand("node") {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
79
segment_node_test.go
Executable file
79
segment_node_test.go
Executable 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")
|
||||||
|
}
|
|
@ -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")
|
|
||||||
}
|
|
|
@ -25,6 +25,11 @@ func (env *MockedEnvironment) getwd() (string, error) {
|
||||||
return args.String(0), args.Error(1)
|
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 {
|
func (env *MockedEnvironment) getPathSeperator() string {
|
||||||
args := env.Called(nil)
|
args := env.Called(nil)
|
||||||
return args.String(0)
|
return args.String(0)
|
||||||
|
|
Loading…
Reference in a new issue