Skip to content

Commit

Permalink
Added segment to display current node version
Browse files Browse the repository at this point in the history
Added an additional segment to the node module to display the current
node version.
  • Loading branch information
caleb-artifact committed Sep 30, 2021
1 parent 05ad40e commit 14bfc26
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
12 changes: 10 additions & 2 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ var defaults = Config{
ShEnvFg: 15,
ShEnvBg: 130,

NodeFg: 15,
NodeBg: 40,
NodeFg: 15,
NodeBg: 40,
NodeVersionFg: 40,
NodeVersionBg: 15,

LoadFg: 15,
LoadBg: 22,
Expand Down Expand Up @@ -955,6 +957,8 @@ var defaults = Config{
ShEnvBg: 9,
NodeFg: 15,
NodeBg: 40,
NodeVersionFg: 40,
NodeVersionBg: 15,
LoadFg: 15,
LoadBg: 2,
LoadHighBg: 5,
Expand Down Expand Up @@ -1287,6 +1291,8 @@ var defaults = Config{
ShEnvBg: 9,
NodeFg: 15,
NodeBg: 40,
NodeVersionFg: 40,
NodeVersionBg: 15,
LoadFg: 15,
LoadBg: 2,
LoadHighBg: 5,
Expand Down Expand Up @@ -1618,6 +1624,8 @@ var defaults = Config{
ShellVarBg: gruvbox_faded_purple, // match ssh-bg
NodeFg: gruvbox_light0, // match virtualenv
NodeBg: gruvbox_faded_green, // match virtualenv
NodeVersionFg: gruvbox_faded_green, // match virtualenv
NodeVersionBg: gruvbox_light0, // match virtualenv
LoadFg: gruvbox_light0,
LoadBg: gruvbox_faded_purple,
LoadHighBg: gruvbox_neutral_red,
Expand Down
55 changes: 44 additions & 11 deletions segment-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"
)
Expand All @@ -14,27 +16,58 @@ type packageJSON struct {
Version string `json:"version"`
}

func segmentNode(p *powerline) []pwl.Segment {
func getNodeVersion() string {
out, err := exec.Command("node", "--version").Output()
if err != nil {
return ""
}
return strings.TrimSuffix(string(out), "\n")
}

func getPackageVersion() string {
stat, err := os.Stat(pkgfile)
if err != nil {
return []pwl.Segment{}
return ""
}
if stat.IsDir() {
return []pwl.Segment{}
return ""
}
pkg := packageJSON{"!"}
raw, err := ioutil.ReadFile(pkgfile)
if err != nil {
return []pwl.Segment{}
return ""
}
err = json.Unmarshal(raw, &pkg)
if err != nil {
return []pwl.Segment{}
return ""
}
return []pwl.Segment{{
Name: "node-segment",
Content: pkg.Version + " \u2B22",
Foreground: p.theme.NodeFg,
Background: p.theme.NodeBg,
}}

return strings.TrimSpace(pkg.Version)
}

func segmentNode(p *powerline) []pwl.Segment {
nodeVersion := getNodeVersion()
packageVersion := getPackageVersion()

segments := []pwl.Segment{}

if nodeVersion != "" {
segments = append(segments, pwl.Segment{
Name: "node",
Content: "\u2B22 " + nodeVersion,
Foreground: p.theme.NodeVersionFg,
Background: p.theme.NodeVersionBg,
})
}

if packageVersion != "" {
segments = append(segments, pwl.Segment{
Name: "node-segment",
Content: packageVersion + " \u2B22",
Foreground: p.theme.NodeFg,
Background: p.theme.NodeBg,
})
}

return segments
}
6 changes: 4 additions & 2 deletions themes.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ type Theme struct {
ShEnvFg uint8
ShEnvBg uint8

NodeFg uint8
NodeBg uint8
NodeFg uint8
NodeBg uint8
NodeVersionFg uint8
NodeVersionBg uint8

LoadFg uint8
LoadBg uint8
Expand Down
2 changes: 2 additions & 0 deletions themes/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"ShEnvBg": 130,
"NodeFg": 15,
"NodeBg": 40,
"NodeVersionFg": 40,
"NodeVersionBg": 15,
"LoadFg": 15,
"LoadBg": 22,
"LoadHighBg": 161,
Expand Down

0 comments on commit 14bfc26

Please sign in to comment.