1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-16 03:46:11 +00:00

Supports multi-disk detection

This commit is contained in:
Tw93
2025-11-23 20:20:46 +08:00
parent 54c30d6b60
commit a3de827fde
2 changed files with 62 additions and 18 deletions

View File

@@ -254,14 +254,25 @@ func renderMemoryCard(mem MemoryStatus) cardData {
func renderDiskCard(disks []DiskStatus, io DiskIOStatus) cardData { func renderDiskCard(disks []DiskStatus, io DiskIOStatus) cardData {
var lines []string var lines []string
// Show main disk if len(disks) == 0 {
if len(disks) > 0 { lines = append(lines, subtleStyle.Render("Collecting..."))
d := disks[0] } else {
freeSpace := d.Total - d.Used internal, external := splitDisks(disks)
bar := diskBar(d.UsedPercent) addGroup := func(prefix string, list []DiskStatus) {
lines = append(lines, fmt.Sprintf("Used %s %4.0f%% (%s free)", bar, d.UsedPercent, humanBytes(freeSpace))) if len(list) == 0 {
return
}
for i, d := range list {
label := diskLabel(prefix, i, len(list))
lines = append(lines, formatDiskLine(label, d))
}
}
addGroup("INTR", internal)
addGroup("EXTR", external)
if len(lines) == 0 {
lines = append(lines, subtleStyle.Render("No disks detected"))
}
} }
// IO
readBar := ioBar(io.ReadRate) readBar := ioBar(io.ReadRate)
writeBar := ioBar(io.WriteRate) writeBar := ioBar(io.WriteRate)
lines = append(lines, fmt.Sprintf("Read %s %.1f MB/s", readBar, io.ReadRate)) lines = append(lines, fmt.Sprintf("Read %s %.1f MB/s", readBar, io.ReadRate))
@@ -269,14 +280,32 @@ func renderDiskCard(disks []DiskStatus, io DiskIOStatus) cardData {
return cardData{icon: iconDisk, title: "Disk", lines: lines} return cardData{icon: iconDisk, title: "Disk", lines: lines}
} }
func diskBar(percent float64) string { func splitDisks(disks []DiskStatus) (internal, external []DiskStatus) {
total := 16 for _, d := range disks {
filled := int(percent / 100 * float64(total)) if d.External {
if filled > total { external = append(external, d)
filled = total } else {
internal = append(internal, d)
} }
bar := strings.Repeat("█", filled) + strings.Repeat("░", total-filled) }
return colorizePercent(percent, bar) return internal, external
}
func diskLabel(prefix string, index int, total int) string {
if total <= 1 {
return prefix
}
return fmt.Sprintf("%s%d", prefix, index+1)
}
func formatDiskLine(label string, d DiskStatus) string {
if label == "" {
label = "DISK"
}
bar := progressBar(d.UsedPercent)
used := humanBytesShort(d.Used)
total := humanBytesShort(d.Total)
return fmt.Sprintf("%-6s %s %5.1f%% (%s/%s)", label, bar, d.UsedPercent, used, total)
} }
func ioBar(rate float64) string { func ioBar(rate float64) string {
@@ -459,7 +488,6 @@ func renderSensorsCard(sensors []SensorReading) cardData {
return cardData{icon: iconSensors, title: "Sensors", lines: lines} return cardData{icon: iconSensors, title: "Sensors", lines: lines}
} }
func renderCard(data cardData, width int, height int) string { func renderCard(data cardData, width int, height int) string {
titleText := data.icon + " " + data.title titleText := data.icon + " " + data.title
lineLen := width - lipgloss.Width(titleText) - 1 lineLen := width - lipgloss.Width(titleText) - 1
@@ -551,6 +579,21 @@ func humanBytes(v uint64) string {
} }
} }
func humanBytesShort(v uint64) string {
switch {
case v >= 1<<40:
return fmt.Sprintf("%.0fT", float64(v)/(1<<40))
case v >= 1<<30:
return fmt.Sprintf("%.0fG", float64(v)/(1<<30))
case v >= 1<<20:
return fmt.Sprintf("%.0fM", float64(v)/(1<<20))
case v >= 1<<10:
return fmt.Sprintf("%.0fK", float64(v)/(1<<10))
default:
return strconv.FormatUint(v, 10)
}
}
func shorten(s string, max int) string { func shorten(s string, max int) string {
if len(s) <= max { if len(s) <= max {
return s return s
@@ -591,4 +634,3 @@ func maxInt(a, b int) int {
} }
return b return b
} }

View File

@@ -132,8 +132,10 @@ check_cache_size() {
for cache_path in "${cache_paths[@]}"; do for cache_path in "${cache_paths[@]}"; do
if [[ -d "$cache_path" ]]; then if [[ -d "$cache_path" ]]; then
local size=$(du -sk "$cache_path" 2>/dev/null | awk '{print $1}' || echo "0") local size_output
cache_size_kb=$((cache_size_kb + size)) size_output=$(du -sk "$cache_path" 2>/dev/null | awk 'NR==1 {print $1}' | tr -d '[:space:]' || echo "")
[[ "$size_output" =~ ^[0-9]+$ ]] || size_output=0
cache_size_kb=$((cache_size_kb + size_output))
fi fi
done done