1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 14:26:46 +00:00

feat: dynamically adjust column widths in UI and analysis output based on terminal size for improved readability

This commit is contained in:
Tw93
2025-12-20 22:18:06 +08:00
parent 499a1ff646
commit 2a0fe88fd3
6 changed files with 217 additions and 25 deletions

View File

@@ -161,9 +161,28 @@ func displayWidth(s string) int {
return width
}
// calculateNameWidth computes the optimal name column width based on terminal width.
// Fixed elements: prefix(3) + num(3) + bar(24) + percent(7) + sep(5) + icon(3) + size(12) + hint(4) = 61
func calculateNameWidth(termWidth int) int {
const fixedWidth = 61
available := termWidth - fixedWidth
// Constrain to reasonable bounds
if available < 24 {
return 24 // Minimum for readability
}
if available > 60 {
return 60 // Maximum to avoid overly wide columns
}
return available
}
func trimName(name string) string {
return trimNameWithWidth(name, 45) // Default width for backward compatibility
}
func trimNameWithWidth(name string, maxWidth int) string {
const (
maxWidth = 28
ellipsis = "..."
ellipsisWidth = 3
)

View File

@@ -119,11 +119,12 @@ func (m model) View() string {
maxLargeSize = file.Size
}
}
nameWidth := calculateNameWidth(m.width)
for idx := start; idx < end; idx++ {
file := m.largeFiles[idx]
shortPath := displayPath(file.Path)
shortPath = truncateMiddle(shortPath, 35)
paddedPath := padName(shortPath, 35)
shortPath = truncateMiddle(shortPath, nameWidth)
paddedPath := padName(shortPath, nameWidth)
entryPrefix := " "
nameColor := ""
sizeColor := colorGray
@@ -152,6 +153,7 @@ func (m model) View() string {
}
}
totalSize := m.totalSize
nameWidth := calculateNameWidth(m.width)
for idx, entry := range m.entries {
icon := "📁"
sizeVal := entry.Size
@@ -188,8 +190,8 @@ func (m model) View() string {
}
}
entryPrefix := " "
name := trimName(entry.Name)
paddedName := padName(name, 28)
name := trimNameWithWidth(entry.Name, nameWidth)
paddedName := padName(name, nameWidth)
nameSegment := fmt.Sprintf("%s %s", icon, paddedName)
numColor := ""
percentColor := ""
@@ -237,6 +239,7 @@ func (m model) View() string {
}
viewport := calculateViewport(m.height, false)
nameWidth := calculateNameWidth(m.width)
start := m.offset
if start < 0 {
start = 0
@@ -253,8 +256,8 @@ func (m model) View() string {
icon = "📁"
}
size := humanizeBytes(entry.Size)
name := trimName(entry.Name)
paddedName := padName(name, 28)
name := trimNameWithWidth(entry.Name, nameWidth)
paddedName := padName(name, nameWidth)
// Calculate percentage
percent := float64(entry.Size) / float64(m.totalSize) * 100