-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
196 lines (162 loc) · 5.23 KB
/
info.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/mitchellh/go-ps"
)
// SysInfo is a store for system info
type SysInfo struct {
user string
host string
os string
kernel string
bootTime string
shell string
de string
wm string
terminal string
cpu string
gpu string
memUsed int
memTotal int
terminalHeight int
}
// FormatInfo formats the given SysInfo into a multiline string
func FormatInfo(info SysInfo) string {
userAtHost := fmt.Sprintf("%s%s@%s%s", info.user, ResetAnsii, AccentAnsii, info.host)
underline := ResetAnsii + strings.Repeat("-", len(userAtHost)-len(AccentAnsii)-len(ResetAnsii))
s := fmt.Sprintf(
`%s
%s
OS: %s
Host: %s
Kernel: %s
Uptime: %s
DE: %s
WM: %s
Shell: %s
Terminal: %s
CPU: %s
GPU: %s
Memory: %v MB / %v MB`,
userAtHost, underline, info.os, info.host, info.kernel, info.bootTime, info.de, info.wm, info.shell, info.terminal, info.cpu, info.gpu, info.memUsed, info.memTotal)
colorPalette := createColorPalette()
return "\n" + strings.Replace(s, ": ", ResetAnsii+": ", -1) + "\n\n" + colorPalette
}
func createColorPalette() string {
csrEscapeChar := "\033["
var dark string
var bright string
for i := 0; i < 8; i++ {
dark += fmt.Sprintf("%s%sm %s", csrEscapeChar, fmt.Sprint(40+i), ResetAnsii)
bright += fmt.Sprintf("%s%sm %s", csrEscapeChar, fmt.Sprint(100+i), ResetAnsii)
}
return fmt.Sprintf("%s\n%s\n", dark, bright)
}
// GetInfo gets system info for the current users environment
func GetInfo() SysInfo {
info := SysInfo{}
// get computersystem info
computerSystemInfo := getValuesFromList(exec.Command("wmic", "computersystem", "get", "name,username", "/value").Output())
info.host = computerSystemInfo[0]
info.user = strings.Split(computerSystemInfo[1], "\\")[1]
// get os info
osInfo := getValuesFromList(exec.Command("wmic", "os", "get", "caption,freephysicalmemory,lastbootuptime,totalvisiblememorysize,version", "/value").Output())
info.os = osInfo[0]
info.kernel = osInfo[4]
info.bootTime = bootTimeToUptime(osInfo[2])
info.memTotal = memToInt(osInfo[3])
info.memUsed = info.memTotal - memToInt(osInfo[1])
// hardware info
info.cpu = getValuesFromList(exec.Command("wmic", "cpu", "get", "name", "/value").Output())[0]
if strings.Contains(info.cpu, "Intel") {
info.cpu = info.cpu[18:]
}
info.gpu = getValuesFromList(exec.Command("wmic", "path", "win32_VideoController", "get", "caption", "/value").Output())[0]
// windows specific values
info.de = "Aero"
info.wm = "Explorer"
// terminal
// terminalLines, _ := exec.Command("mode", "con", "/status", "|", "findstr", "Lines:").Output()
// info.terminalHeight, _ = strconv.Atoi(strings.Split(string(terminalLines), ":")[1])
pProcess, _ := ps.FindProcess(os.Getppid())
ppProcess, _ := ps.FindProcess(pProcess.PPid())
if ppProcess.Executable() == "explorer.exe" {
info.terminal = pProcess.Executable()[:len(pProcess.Executable())-4]
info.shell = getShell(info.terminal, info.kernel)
} else {
info.terminal = ppProcess.Executable()[:len(ppProcess.Executable())-4]
info.shell = getShell(pProcess.Executable()[:len(pProcess.Executable())-4], info.kernel)
}
return info
}
func getShell(term string, kern string) string {
switch term {
case "cmd":
return fmt.Sprintf("%s %s", term, kern)
case "pwsh":
fallthrough
case "powershell":
version := getPowershellVersion()
return fmt.Sprintf("%s %s", term, version)
}
return ""
}
func getPowershellVersion() string {
major, _ := exec.Command("powershell", "(Get-Variable PSVersionTable -ValueOnly).PSVersion.Major").Output()
minor, _ := exec.Command("powershell", "(Get-Variable PSVersionTable -ValueOnly).PSVersion.Minor").Output()
patch, _ := exec.Command("powershell", "(Get-Variable PSVersionTable -ValueOnly).PSVersion.Patch").Output()
versionSlice := make([]string, 0, 3)
if len(major) > 0 {
versionSlice = append(versionSlice, string(major))
}
if len(minor) > 0 {
versionSlice = append(versionSlice, string(minor))
}
if len(patch) > 0 {
versionSlice = append(versionSlice, string(patch))
}
var version string
for _, s := range versionSlice {
version += "." + strings.Trim(s, "\r\n")
}
return version[1:]
}
func getValuesFromList(out []byte, _ error) []string {
list := strings.Split(string(out), "\r")
values := make([]string, 0)
for _, s := range list {
if strings.Contains(s, "=") {
values = append(values, strings.Split(s, "=")[1])
}
}
return values
}
func bootTimeToUptime(bootTime string) string {
layout := "20060102150405"
bootTimeWMI := strings.Split(bootTime, ".")[0]
bootTimeParsed, _ := time.Parse(layout, bootTimeWMI)
duration := time.Since(bootTimeParsed)
days := int(duration.Hours() / 24)
hours := int(duration.Hours()) - (24 * days)
minutes := int(duration.Minutes()) - int(duration.Hours())*60
uptime := make([]string, 0, 3)
if days != 0 {
uptime = append(uptime, fmt.Sprintf("%v days", days))
}
if hours != 0 {
uptime = append(uptime, fmt.Sprintf("%v hours", hours))
}
if minutes != 0 {
uptime = append(uptime, fmt.Sprintf("%v mins", minutes))
}
return strings.Join(uptime, ", ")
}
func memToInt(memStr string) int {
memInt, _ := strconv.Atoi(memStr)
return memInt / 1024
}