Skip to content

Commit 6e98120

Browse files
committed
Add Proccesses, CPU, Uptime
1 parent 152182a commit 6e98120

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed

config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package main
33
type Config struct {
44
ShowUser bool `json:"showUser"`
55
ShowSep bool `json:"showSep"`
6+
ShowUptime bool `json:"showUptime"`
67
ShowMem bool `json:"showMem"`
8+
ShowCPU bool `json:"showCPU"`
79
ShowTotalCPUCores bool `json:"showTotalCPUCores"`
810
ShowTotalCPUThreads bool `json:"showTotalCPUThreads"`
911
ShowGPUS bool `json:"showGPUS"`
1012
ShowTotalDiskSize bool `json:"showTotalDiskSize"`
1113
ShowWindowsVersion bool `json:"showWindowsVersion"`
1214
ShowBios bool `json:"showBios"`
1315
ShowBaseboard bool `json:"showBaseboard"`
16+
ShowProcessCount bool `json:"showProcessCount"`
1417
ShowAscii bool `json:"showAscii"`
1518
UseDefaultColors bool `json:"useDefaultColors"`
1619
UseSmallAscii bool `json:"useSmallAscii"`
@@ -26,20 +29,25 @@ type Config struct {
2629

2730
type TitleValues struct {
2831
Memory string `json:"memory"`
32+
CPU string `json:"cpu"`
2933
CPUCores string `json:"cpuCores"`
3034
CPUThreads string `json:"cpuThreads"`
3135
GPUs string `json:"gpus"`
3236
DiskSize string `json:"diskSize"`
3337
WindowsVersion string `json:"windowsVersion"`
3438
Bios string `json:"bios"`
3539
Baseboard string `json:"baseboard"`
40+
ProcessCount string `json:"processCount"`
41+
Uptime string `json:"uptime"`
3642
}
3743

3844
func newConfig() Config {
3945
config := Config{}
4046
config.ShowUser = true
47+
config.ShowUptime = true
4148
config.ShowSep = true
4249
config.ShowMem = true
50+
config.ShowCPU = true
4351
config.ShowTotalCPUCores = true
4452
config.ShowTotalCPUThreads = true
4553
config.ShowGPUS = true
@@ -48,6 +56,7 @@ func newConfig() Config {
4856
config.ShowBios = true
4957
config.ShowBaseboard = true
5058
config.ShowAscii = true
59+
config.ShowProcessCount = true
5160
config.UseSmallAscii = false
5261
config.UseCustomAscii = false
5362
config.CustomAsciiPath = ""
@@ -65,5 +74,8 @@ func newConfig() Config {
6574
config.Titles.WindowsVersion = "Windows Ver."
6675
config.Titles.Bios = "BIOS"
6776
config.Titles.Baseboard = "Baseboard"
77+
config.Titles.Uptime = "Uptime"
78+
config.Titles.ProcessCount = "Processes"
79+
config.Titles.CPU = "CPU #"
6880
return config
6981
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ go 1.14
55
require (
66
github.com/gilliek/go-xterm256 v1.0.0
77
github.com/jaypipes/ghw v0.6.0
8+
github.com/mitchellh/go-ps v1.0.0
9+
github.com/shirou/gopsutil v2.20.4+incompatible
810
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299
911
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1919
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2020
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
2121
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
22+
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
23+
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
2224
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
2325
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
26+
github.com/shirou/gopsutil v2.20.4+incompatible h1:cMT4rxS55zx9NVUnCkrmXCsEB/RNfG9SwHY9evtX8Ng=
27+
github.com/shirou/gopsutil v2.20.4+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
2428
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
2529
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
2630
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=

helpers.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,46 @@ func ByteFormat(inputNum float64, precision int) string {
5454

5555
return strconv.FormatFloat(returnVal, 'f', precision, 64) + unit
5656

57+
}
58+
59+
func plural(count int, singular string) (result string) {
60+
if (count == 1) || (count == 0) {
61+
result = strconv.Itoa(count) + " " + singular + " "
62+
} else {
63+
result = strconv.Itoa(count) + " " + singular + "s "
64+
}
65+
return
66+
}
67+
68+
func secondsToHuman(input int) (result string) {
69+
years := math.Floor(float64(input) / 60 / 60 / 24 / 7 / 30 / 12)
70+
seconds := input % (60 * 60 * 24 * 7 * 30 * 12)
71+
months := math.Floor(float64(seconds) / 60 / 60 / 24 / 7 / 30)
72+
seconds = input % (60 * 60 * 24 * 7 * 30)
73+
weeks := math.Floor(float64(seconds) / 60 / 60 / 24 / 7)
74+
seconds = input % (60 * 60 * 24 * 7)
75+
days := math.Floor(float64(seconds) / 60 / 60 / 24)
76+
seconds = input % (60 * 60 * 24)
77+
hours := math.Floor(float64(seconds) / 60 / 60)
78+
seconds = input % (60 * 60)
79+
minutes := math.Floor(float64(seconds) / 60)
80+
seconds = input % 60
81+
82+
if years > 0 {
83+
result = plural(int(years), "year") + plural(int(months), "month") + plural(int(weeks), "week") + plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
84+
} else if months > 0 {
85+
result = plural(int(months), "month") + plural(int(weeks), "week") + plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
86+
} else if weeks > 0 {
87+
result = plural(int(weeks), "week") + plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
88+
} else if days > 0 {
89+
result = plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
90+
} else if hours > 0 {
91+
result = plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
92+
} else if minutes > 0 {
93+
result = plural(int(minutes), "minute") + plural(int(seconds), "second")
94+
} else {
95+
result = plural(int(seconds), "second")
96+
}
97+
98+
return
5799
}

main.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"encoding/json"
1111
"io/ioutil"
1212

13-
13+
ps "github.com/shirou/gopsutil/process"
14+
hst "github.com/shirou/gopsutil/host"
15+
// cp "github.com/shirou/gopsutil/cpu"
1416
"github.com/jaypipes/ghw"
1517
"golang.org/x/sys/windows/registry"
1618
"github.com/gilliek/go-xterm256/xterm256"
@@ -160,6 +162,15 @@ llllllllllll lllllllllllllllll
160162

161163
s = append(s, xterm256.Sprint(userc, strings.ReplaceAll(user.Username, "\\", "@")))
162164
s = append(s, xterm256.Sprint(sep, "--------------------------------"))
165+
166+
if (config.ShowUptime){
167+
uptime, err := hst.Uptime()
168+
if (err != nil) {
169+
log.Fatal("Failed to Get Uptime!")
170+
}
171+
uptimes := secondsToHuman(int(uptime))
172+
s = append(s, xterm256.Sprint(title, config.Titles.Uptime + ": ") + xterm256.Sprint(info, uptimes))
173+
}
163174

164175
memorySplit := strings.Split(memory.String(), "(")
165176
mem := strings.Split(memorySplit[1], ",")
@@ -168,18 +179,33 @@ llllllllllll lllllllllllllllll
168179
if (config.ShowMem) {
169180
s = append(s, xterm256.Sprint(title, config.Titles.Memory + ": ") + xterm256.Sprint(info, strings.ReplaceAll(usableMem[0], "MB ", "GB") + "/" + strings.ReplaceAll(physMem[0], "MB", "GB")))
170181
}
171-
if (config.ShowTotalCPUCores || config.ShowTotalCPUThreads){
182+
if (config.ShowTotalCPUCores || config.ShowTotalCPUThreads || config.ShowCPU){
172183
cpu, err := ghw.CPU()
173184
if err != nil {
174185
fmt.Printf("Error getting CPU info: %v", err)
175186
}
187+
if (config.ShowCPU){
188+
in := 0
189+
for x := range cpu.Processors {
190+
s = append(s, xterm256.Sprint(title, "CPU #" + fmt.Sprint(in) + ": ") + xterm256.Sprint(info, cpu.Processors[x].Model))
191+
}
192+
}
176193
if (config.ShowTotalCPUCores){
177194
s = append(s, xterm256.Sprint(title, config.Titles.CPUCores + ": ") + xterm256.Sprint(info, fmt.Sprint(cpu.TotalCores)))
178195
}
179196
if (config.ShowTotalCPUThreads){
180197
s = append(s, xterm256.Sprint(title, config.Titles.CPUThreads + ": ") + xterm256.Sprint(info, fmt.Sprint(cpu.TotalThreads)))
181198
}
182199
}
200+
if (config.ShowProcessCount){
201+
pids, err := ps.Pids()
202+
203+
if err != nil {
204+
log.Fatal("Couldn't get Processes!")
205+
}
206+
207+
s = append(s, xterm256.Sprint(title, "Proccesses Running: ") + xterm256.Sprint(info, int64(len(pids))))
208+
}
183209
if (config.ShowWindowsVersion){
184210
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
185211

@@ -203,12 +229,9 @@ llllllllllll lllllllllllllllll
203229
}
204230
gpuin := 0
205231
for _, c := range gpu.GraphicsCards {
206-
s = append(s, xterm256.Sprint(title, config.Titles.GPUs + ": " + fmt.Sprint(gpuin) + ": ") + xterm256.Sprint(info, c.DeviceInfo.Product.Name))
232+
s = append(s, xterm256.Sprint(title, config.Titles.GPUs + fmt.Sprint(gpuin) + ": ") + xterm256.Sprint(info, c.DeviceInfo.Product.Name))
207233
gpuin++
208234
}
209-
}
210-
if (config.ShowWindowsVersion){
211-
212235
}
213236
if (config.ShowBios){
214237
bios, err := ghw.BIOS()

0 commit comments

Comments
 (0)