-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtruefetch_unix.go
199 lines (175 loc) · 4.38 KB
/
truefetch_unix.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
197
198
199
//go:build !plan9
// Unix specific instructions that are used to fetch system information in [truefetch](https://github.com/peajack/truefetch)
package main
import (
"fmt"
"os"
"os/exec"
"path"
"strings"
"sync"
"time"
"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/process"
"golang.org/x/sys/unix"
)
// some consts
const (
RESET = "\033[0;m"
)
func getShellFromEnv() string {
return path.Base(os.Getenv("SHELL"))
}
func getShell() string {
pid := os.Getppid()
proc, err := process.NewProcess(int32(pid))
if err != nil {
return getShellFromEnv()
}
exe, err := proc.Exe()
if err != nil {
cmd, err := proc.Cmdline()
if err != nil {
return path.Base(cmd)
}
return getShellFromEnv()
}
return path.Base(exe)
}
func convertRelease(raw []byte) string {
sane := make([]byte, 0, len(raw))
for _, char := range raw {
if char == 0x00 {
break
}
sane = append(sane, byte(char))
}
return string(sane)
}
func getKernel() string {
uname := unix.Utsname{}
err := unix.Uname(&uname)
if err != nil {
return "unknown"
}
version, _, _ := strings.Cut(convertRelease(uname.Release[:]), "-")
return strings.TrimSpace(version)
}
func getMemory() string {
v, err := mem.VirtualMemory()
if err != nil {
return "0/0 MB (∞ %)"
}
total := v.Total / 1024 / 1024
used := (v.Total - v.Available) / 1024 / 1024
usedPercent := (float32(used) / float32(total)) * 100
return fmt.Sprintf("%d/%d MB (%.2f%%)", used, total, usedPercent)
}
func getUptime() string {
u, err := host.Uptime()
if err != nil {
return "∞ "
}
return fmt.Sprint(time.Duration(u * uint64(time.Second)))
}
// thanks dheison, but it didnt work :(
//func getMemory() string {
// return getSysinfoField(func(s *unix.Sysinfo_t) string {
// totalMemory := s.Totalram
// usedMemory := totalMemory - s.Freeram - s.Bufferram - s.Sharedram
// return fmt.Sprintf("%dMiB / %dMiB", usedMemory/MIBIBYTE, totalMemory/MIBIBYTE)
// })
//}
func getInit() string {
var cmdline string
proc, err := process.NewProcess(1)
if err == nil {
cmdline, err = proc.Cmdline()
if err != nil {
return ""
}
} else {
cmdline, err = os.Readlink("/proc/1/exe")
if err != nil {
return ""
}
}
exe := path.Base(cmdline)
if exe == "runit" {
return "runit"
} else if exe == "launchd" {
return "launchd"
} else if _, err := os.Stat("/run/systemd/system"); err == nil {
return "systemd"
} else if _, err := os.Stat("/run/s6/current"); err == nil {
return "s6"
} else if exe == "init" {
if _, err := os.Stat("/etc/init.d"); err == nil {
if doesExist("openrc") {
return "openrc"
}
return "SysV-style"
} else if _, err := os.Stat("/etc/rc.d"); err == nil {
return "BSD-style rc.d"
}
}
return ""
}
var packageManagers = map[string]string{
"unknown": "",
"pacman": "pacman -Qq",
"dpkg": "dpkg -l | tail -n+6",
// drop this for now, 'cause it's bash-specific
// "rpm": "[[ $(which sqlite3 2>/dev/null) && $? -ne 1 ]] && (sqlite3 /var/lib/rpm/rpmdb.sqlite \"select * from Name\") || rpm -qa",
"rpm": "rpm -qa",
"portage": "qlist -IRv",
"xbps": "xbps-query -l",
"apk": "grep 'P:' /lib/apk/db/installed",
"flatpak": "flatpak list --app",
"snap": "snap list",
"freebsd": "pkg info",
"openbsd": "/bin/ls -1 /var/db/pkg/",
"pkgsrc": "pkg_info",
"android": "echo \"$(pm list packages --user 0 2>&1 </dev/null)\" | tr ' ' '\n'",
"Cellar": "ls /usr/local/Cellar/* | grep ':'",
"homebrew": "brew list",
}
func getPkgs(packageManager string) string {
neededManagers := map[string]string{}
if packageManager == "" {
neededManagers = packageManagers
} else {
neededManagers["flatpak"] = packageManagers["flatpak"]
neededManagers["snap"] = packageManagers["snap"]
neededManagers[packageManager] = packageManagers[packageManager]
}
packageCounts := make(chan string, len(neededManagers))
var wg sync.WaitGroup
for manager, command := range neededManagers {
if command == "" {
continue
}
wait(&wg, func() {
cmd := exec.Command("/bin/sh", "-c", command)
stdout, err := cmd.Output()
if err != nil {
return
}
packageCounts <- fmt.Sprintf(
"%d (%s)",
wcL(string(stdout)),
manager,
)
})
}
go func() {
wg.Wait()
close(packageCounts)
}()
var countStrings []string
for count := range packageCounts {
countStrings = append(countStrings, count)
}
return strings.Join(countStrings, ", ")
}