1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 18:30:08 +00:00

fix(status): hide swap size text when card is narrow

This commit is contained in:
tw93
2026-02-27 10:02:06 +08:00
parent 0341ae6648
commit 15f698c606
2 changed files with 49 additions and 3 deletions

View File

@@ -266,7 +266,7 @@ func renderCPUCard(cpu CPUStatus, thermal ThermalStatus) cardData {
return cardData{icon: iconCPU, title: "CPU", lines: lines}
}
func renderMemoryCard(mem MemoryStatus) cardData {
func renderMemoryCard(mem MemoryStatus, cardWidth int) cardData {
// Check if swap is being used (or at least allocated).
hasSwap := mem.SwapTotal > 0 || mem.SwapUsed > 0
@@ -287,8 +287,16 @@ func renderMemoryCard(mem MemoryStatus) cardData {
if mem.SwapTotal > 0 {
swapPercent = (float64(mem.SwapUsed) / float64(mem.SwapTotal)) * 100.0
}
swapLine := fmt.Sprintf("Swap %s %5.1f%%", progressBar(swapPercent), swapPercent)
swapText := fmt.Sprintf("%s/%s", humanBytesCompact(mem.SwapUsed), humanBytesCompact(mem.SwapTotal))
lines = append(lines, fmt.Sprintf("Swap %s %5.1f%% %s", progressBar(swapPercent), swapPercent, swapText))
swapLineWithText := swapLine + " " + swapText
if cardWidth > 0 && lipgloss.Width(swapLineWithText) <= cardWidth {
lines = append(lines, swapLineWithText)
} else if cardWidth <= 0 {
lines = append(lines, swapLineWithText)
} else {
lines = append(lines, swapLine)
}
lines = append(lines, fmt.Sprintf("Total %s / %s", humanBytes(mem.Used), humanBytes(mem.Total)))
lines = append(lines, fmt.Sprintf("Avail %s", humanBytes(mem.Total-mem.Used))) // Simplified avail logic for consistency
@@ -416,7 +424,7 @@ func renderProcessCard(procs []ProcessInfo) cardData {
func buildCards(m MetricsSnapshot, width int) []cardData {
cards := []cardData{
renderCPUCard(m.CPU, m.Thermal),
renderMemoryCard(m.Memory),
renderMemoryCard(m.Memory, width),
renderDiskCard(m.Disks, m.DiskIO),
renderBatteryCard(m.Batteries, m.Thermal),
renderProcessCard(m.TopProcesses),

View File

@@ -976,6 +976,44 @@ func TestRenderCardWrapsOnNarrowWidth(t *testing.T) {
}
}
func TestRenderMemoryCardHidesSwapSizeOnNarrowWidth(t *testing.T) {
card := renderMemoryCard(MemoryStatus{
Used: 8 << 30,
Total: 16 << 30,
UsedPercent: 50.0,
SwapUsed: 482,
SwapTotal: 1000,
}, 38)
if len(card.lines) < 3 {
t.Fatalf("renderMemoryCard() expected at least 3 lines, got %d", len(card.lines))
}
swapLine := stripANSI(card.lines[2])
if strings.Contains(swapLine, "/") {
t.Fatalf("renderMemoryCard() narrow width should hide swap size, got %q", swapLine)
}
}
func TestRenderMemoryCardShowsSwapSizeOnWideWidth(t *testing.T) {
card := renderMemoryCard(MemoryStatus{
Used: 8 << 30,
Total: 16 << 30,
UsedPercent: 50.0,
SwapUsed: 482,
SwapTotal: 1000,
}, 60)
if len(card.lines) < 3 {
t.Fatalf("renderMemoryCard() expected at least 3 lines, got %d", len(card.lines))
}
swapLine := stripANSI(card.lines[2])
if !strings.Contains(swapLine, "/") {
t.Fatalf("renderMemoryCard() wide width should include swap size, got %q", swapLine)
}
}
func TestModelViewErrorRendersSingleMole(t *testing.T) {
m := model{
width: 120,