forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-node.go
73 lines (61 loc) · 1.34 KB
/
segment-node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"strings"
pwl "github.com/justjanne/powerline-go/powerline"
)
const pkgfile = "./package.json"
type packageJSON struct {
Version string `json:"version"`
}
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 ""
}
if stat.IsDir() {
return ""
}
pkg := packageJSON{""}
raw, err := ioutil.ReadFile(pkgfile)
if err != nil {
return ""
}
err = json.Unmarshal(raw, &pkg)
if err != nil {
return ""
}
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: p.symbols.NodeIndicator + " " + nodeVersion,
Foreground: p.theme.NodeVersionFg,
Background: p.theme.NodeVersionBg,
})
}
if packageVersion != "" {
segments = append(segments, pwl.Segment{
Name: "node-segment",
Content: packageVersion + " " + p.symbols.NodeIndicator,
Foreground: p.theme.NodeFg,
Background: p.theme.NodeBg,
})
}
return segments
}