Skip to content

Commit d0135cc

Browse files
committed
feat: use unicode chars for bar
1 parent e5e7637 commit d0135cc

File tree

2 files changed

+81
-24
lines changed

2 files changed

+81
-24
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ func main() {
343343

344344
// print tables
345345
renderTables(m, filters, TableOptions{
346-
Columns: columns,
347-
SortBy: sortCol,
348-
Style: style,
346+
Columns: columns,
347+
SortBy: sortCol,
348+
Style: style,
349+
StyleName: *styleOpt,
349350
})
350351
}

table.go

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515

1616
// TableOptions contains all options for the table.
1717
type TableOptions struct {
18-
Columns []int
19-
SortBy int
20-
Style table.Style
18+
Columns []int
19+
SortBy int
20+
Style table.Style
21+
StyleName string
2122
}
2223

2324
// Column defines a column.
@@ -330,29 +331,84 @@ func printTable(title string, m []Mount, opts TableOptions) {
330331
s := termenv.String()
331332
if usage > 0 {
332333
if barWidth > 0 {
333-
bw := barWidth - 2
334-
s = termenv.String(fmt.Sprintf("[%s%s] %5.1f%%",
335-
strings.Repeat("#", int(usage*float64(bw))),
336-
strings.Repeat(".", bw-int(usage*float64(bw))),
337-
usage*100,
338-
))
334+
bw := barWidth
335+
var filledChar, halfChar, emptyChar string
336+
if opts.StyleName == "unicode" {
337+
filledChar = "█"
338+
halfChar = "▌"
339+
emptyChar = " "
340+
} else {
341+
bw -= 2
342+
filledChar = "#"
343+
halfChar = "#"
344+
emptyChar = "."
345+
}
346+
347+
filled := int(usage * float64(bw))
348+
partial := usage*float64(bw) - float64(filled)
349+
empty := bw - filled
350+
351+
var filledStr, emptyStr string
352+
if partial >= 0.5 {
353+
if filled > 0 {
354+
filledStr = strings.Repeat(filledChar, filled-1) + halfChar
355+
emptyStr = strings.Repeat(emptyChar, empty)
356+
} else {
357+
filledStr = halfChar
358+
emptyStr = strings.Repeat(emptyChar, empty-1)
359+
empty -= 1
360+
}
361+
} else {
362+
filledStr = strings.Repeat(filledChar, filled)
363+
emptyStr = strings.Repeat(emptyChar, empty)
364+
}
365+
366+
var format string
367+
if opts.StyleName == "unicode" {
368+
format = "%s%s %5.1f%%"
369+
} else {
370+
format = "[%s%s] %5.1f%%"
371+
}
372+
373+
// Apply colors
374+
redUsage, _ := strconv.ParseFloat(strings.Split(*usageThreshold, ",")[1], 64)
375+
yellowUsage, _ := strconv.ParseFloat(strings.Split(*usageThreshold, ",")[0], 64)
376+
377+
var fgColor termenv.Color
378+
switch {
379+
case usage >= redUsage:
380+
fgColor = theme.colorRed
381+
case usage >= yellowUsage:
382+
fgColor = theme.colorYellow
383+
default:
384+
fgColor = theme.colorGreen
385+
}
386+
387+
filledPart := termenv.String(filledStr).Foreground(fgColor)
388+
emptyPart := termenv.String(emptyStr)
389+
if opts.StyleName == "unicode" {
390+
// Add background to filled part to prevent black spaces in half blocks
391+
// Use a background color that complements the foreground
392+
var bgColor termenv.Color
393+
switch {
394+
case usage >= redUsage:
395+
bgColor = env.Color("#2d1b1b") // dark red background
396+
case usage >= yellowUsage:
397+
bgColor = env.Color("#2d2d1b") // dark yellow background
398+
default:
399+
bgColor = env.Color("#1b2d1b") // dark green background
400+
}
401+
filledPart = filledPart.Background(bgColor).Foreground(fgColor)
402+
// Use a neutral background for empty areas
403+
emptyPart = emptyPart.Background(bgColor)
404+
}
405+
406+
s = termenv.String(fmt.Sprintf(format, filledPart, emptyPart, usage*100))
339407
} else {
340408
s = termenv.String(fmt.Sprintf("%5.1f%%", usage*100))
341409
}
342410
}
343411

344-
// apply color to progress-bar
345-
redUsage, _ := strconv.ParseFloat(strings.Split(*usageThreshold, ",")[1], 64)
346-
yellowUsage, _ := strconv.ParseFloat(strings.Split(*usageThreshold, ",")[0], 64)
347-
switch {
348-
case usage >= redUsage:
349-
s = s.Foreground(theme.colorRed)
350-
case usage >= yellowUsage:
351-
s = s.Foreground(theme.colorYellow)
352-
default:
353-
s = s.Foreground(theme.colorGreen)
354-
}
355-
356412
return s.String()
357413
}
358414

0 commit comments

Comments
 (0)