-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (112 loc) · 2.8 KB
/
main.go
File metadata and controls
133 lines (112 loc) · 2.8 KB
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
// main.go
// Enhanced version that automatically detects connected USB devices on ZyXEL
// All original code preserved as much as possible.
package main
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
// ANSI colors
const (
ORANGE = "\033[38;5;208m"
GREEN = "\033[0;32m"
CYAN = "\033[0;36m"
RESET = "\033[0m"
)
type Storage struct {
Path string
Label string
}
// Function to display usage bar for given storage path
func showStorage(path, label string) {
cmd := exec.Command("df", path)
out, err := cmd.Output()
if err != nil {
return
}
lines := strings.Split(string(out), "\n")
if len(lines) < 2 {
return
}
fields := strings.Fields(lines[1])
if len(fields) < 4 {
return
}
total, _ := strconv.ParseInt(fields[1], 10, 64)
used, _ := strconv.ParseInt(fields[2], 10, 64)
free, _ := strconv.ParseInt(fields[3], 10, 64)
if total <= 0 {
return
}
totalGB := total / 1024 / 1024
usedGB := used / 1024 / 1024
freeMB := free / 1024
percentUsed := used * 100 / total
if percentUsed > 100 {
percentUsed = 100
} else if percentUsed < 0 {
percentUsed = 0
}
barWidth := 30
usedChars := int(int64(barWidth) * percentUsed / 100)
if usedChars < 1 && percentUsed > 0 {
usedChars = 1
}
if usedChars > barWidth {
usedChars = barWidth
}
freeChars := barWidth - usedChars
if freeChars < 0 {
freeChars = 0
}
usedBar := strings.Repeat("#", usedChars)
freeBar := strings.Repeat("#", freeChars)
fmt.Printf("%s%s%s\n", CYAN, label, RESET)
fmt.Printf(" %s%s%s%s%s %s%d%% used%s\n",
ORANGE, usedBar, RESET, GREEN, freeBar, CYAN, percentUsed, RESET)
fmt.Printf(" Total: %s%d G%s | Used: %s%d G%s | Free: %s%d M%s\n\n",
CYAN, totalGB, RESET,
ORANGE, usedGB, RESET,
GREEN, freeMB, RESET)
}
// Function to detect connected ZyXEL USB devices dynamically
func detectZyXELUSB() []Storage {
var usbStorages []Storage
// Try to list /e-data directories (typical for FFP ZyXEL USB mounts)
cmd := exec.Command("ls", "/e-data")
out, err := cmd.Output()
if err != nil {
return usbStorages
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
path := "/e-data/" + line
label := "USB: " + line
usbStorages = append(usbStorages, Storage{Path: path, Label: label})
}
return usbStorages
}
func main() {
storages := []Storage{
// Android / Redmi
{"/storage/emulated/0", "Internal Storage"},
{"/storage/65D9-1787", "SD Card"},
// Android / tablet
{"/storage/sdcard0", "Internal Storage"},
// ZyXEL server
{"/dev/md0", "HDD 1"},
{"/dev/md1", "HDD 2"},
}
// Append auto-detected ZyXEL USB devices
usbDevices := detectZyXELUSB()
storages = append(storages, usbDevices...)
fmt.Printf("%s=== Storage ===%s\n", CYAN, RESET)
for _, s := range storages {
showStorage(s.Path, s.Label)
}
}