Skip to content

Commit 577e113

Browse files
modified: .gitignore
new file: README.md new file: go.mod new file: main.go
1 parent a914e9c commit 577e113

4 files changed

Lines changed: 171 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*.dll
88
*.so
99
*.dylib
10+
bin/*
11+
bin
1012

1113
# Test binary, built with `go test -c`
1214
*.test

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Storage Usage Monitor
2+
3+
A lightweight Go utility that displays storage usage information with a visual progress bar and color-coded output for easy monitoring.
4+
5+
## Features
6+
7+
- 📊 Visual storage usage representation with progress bars
8+
- 🎨 Color-coded output for better readability
9+
- 📱 Supports multiple storage devices (internal, external, RAID arrays)
10+
- 🔢 Displays total, used, and free space in appropriate units
11+
- 🖥️ Clean, formatted output for terminal viewing
12+
13+
## Supported Storage Types
14+
15+
- Android internal storage
16+
- SD cards
17+
- RAID arrays (md devices)
18+
- Any mountable storage device
19+
20+
## Installation
21+
22+
1. Ensure you have Go installed on your system
23+
2. Clone or download this repository
24+
3. Build the executable:
25+
```bash
26+
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=5 go build -trimpath -v -x -o ./bin/storage ./main.go
27+
```
28+
29+
Usage
30+
31+
Run the compiled binary:
32+
33+
```bash
34+
./storage
35+
```
36+
37+
Output Example
38+
39+
```
40+
=== Úložiště ===
41+
Interní úložiště
42+
############################## 85% used
43+
Total: 64 G | Used: 54 G | Free: 10240 M
44+
45+
SD karta
46+
################## 60% used
47+
Total: 128 G | Used: 77 G | Free: 51200 M
48+
```
49+
50+
Customization
51+
52+
To monitor different storage paths, edit the storages slice in the main() function:
53+
54+
```go
55+
storages := []Storage{
56+
{"/path/to/your/storage", "Custom Label"},
57+
{"/another/path", "Another Label"},
58+
}
59+
```
60+
61+
Requirements
62+
63+
· Go 1.11 or higher
64+
· Linux/Unix-like system with df command available
65+
· Terminal that supports ANSI color codes
66+

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/PhateValleyman/storage
2+
3+
go 1.17

main.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
// ANSI colors
11+
const (
12+
ORANGE = "\033[38;5;208m"
13+
GREEN = "\033[0;32m"
14+
CYAN = "\033[0;36m"
15+
RESET = "\033[0m"
16+
)
17+
18+
type Storage struct {
19+
Path string
20+
Label string
21+
}
22+
23+
func showStorage(path, label string) {
24+
cmd := exec.Command("df", path)
25+
out, err := cmd.Output()
26+
if err != nil {
27+
// Pokud cesta neexistuje nebo se nepodaří načíst, přeskočíme
28+
return
29+
}
30+
31+
lines := strings.Split(string(out), "\n")
32+
if len(lines) < 2 {
33+
return
34+
}
35+
36+
fields := strings.Fields(lines[1])
37+
if len(fields) < 4 {
38+
return
39+
}
40+
41+
total, _ := strconv.ParseInt(fields[1], 10, 64)
42+
used, _ := strconv.ParseInt(fields[2], 10, 64)
43+
free, _ := strconv.ParseInt(fields[3], 10, 64)
44+
45+
if total <= 0 {
46+
return
47+
}
48+
49+
totalGB := total / 1024 / 1024
50+
usedGB := used / 1024 / 1024
51+
freeMB := free / 1024
52+
percentUsed := used * 100 / total
53+
54+
// oříznout procenta na 0–100
55+
if percentUsed > 100 {
56+
percentUsed = 100
57+
} else if percentUsed < 0 {
58+
percentUsed = 0
59+
}
60+
61+
barWidth := 30
62+
usedChars := int(int64(barWidth) * percentUsed / 100)
63+
if usedChars < 1 && percentUsed > 0 {
64+
usedChars = 1
65+
}
66+
if usedChars > barWidth {
67+
usedChars = barWidth
68+
}
69+
freeChars := barWidth - usedChars
70+
if freeChars < 0 {
71+
freeChars = 0
72+
}
73+
74+
usedBar := strings.Repeat("#", usedChars)
75+
freeBar := strings.Repeat("#", freeChars)
76+
77+
fmt.Printf("%s%s%s\n", CYAN, label, RESET)
78+
fmt.Printf(" %s%s%s%s%s %s%d%% used%s\n",
79+
ORANGE, usedBar, RESET, GREEN, freeBar, CYAN, percentUsed, RESET)
80+
fmt.Printf(" Total: %s%d G%s | Used: %s%d G%s | Free: %s%d M%s\n\n",
81+
CYAN, totalGB, RESET,
82+
ORANGE, usedGB, RESET,
83+
GREEN, freeMB, RESET)
84+
}
85+
86+
func main() {
87+
storages := []Storage{
88+
// Android / Redmi
89+
{"/storage/emulated/0", "Interní úložiště"},
90+
{"/storage/65D9-1787", "SD karta"},
91+
// ZyXEL server
92+
{"/dev/md0", "HDD 1"},
93+
{"/dev/md1", "HDD 2"},
94+
}
95+
96+
fmt.Printf("%s=== Úložiště ===%s\n", CYAN, RESET)
97+
for _, s := range storages {
98+
showStorage(s.Path, s.Label)
99+
}
100+
}

0 commit comments

Comments
 (0)