Skip to content

Commit

Permalink
Add size percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
bastengao committed Jun 26, 2023
1 parent 2eef96b commit 298b672
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cmd/gncdu-*
gncdu-*
30 changes: 29 additions & 1 deletion ui/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ui

import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"

"github.com/bastengao/gncdu/scan"
Expand Down Expand Up @@ -171,25 +173,34 @@ func (p *ResultPage) Show() {
color := tcell.ColorYellow
table.SetCell(0, 0, tview.NewTableCell("Name").SetTextColor(color).SetSelectable(false))
table.SetCell(0, 1, tview.NewTableCell("Size").SetTextColor(color).SetSelectable(false))
table.SetCell(0, 2, tview.NewTableCell("Items").SetTextColor(color).SetSelectable(false))
table.SetCell(0, 2, tview.NewTableCell("").SetTextColor(color).SetSelectable(false))
table.SetCell(0, 3, tview.NewTableCell("Items").SetTextColor(color).SetSelectable(false))

if p.parent != nil && !p.parent.Root() {
table.SetCell(1, 0, tview.NewTableCell("/.."))
}

var maxSize int64
for i, file := range p.files {
nameColor := tcell.ColorWhite
if file.Info.IsDir() {
nameColor = tcell.ColorDeepSkyBlue
}

if i == 0 {
maxSize = file.Size()
}

table.SetCell(i+offset, 0,
tview.NewTableCell(file.Label()).
SetTextColor(nameColor))
table.SetCell(i+offset, 1,
tview.NewTableCell(scan.ToHumanSize(file.Size())).
SetAlign(tview.AlignRight))
table.SetCell(i+offset, 2,
tview.NewTableCell(percentageText(maxSize, file.Size())).
SetAlign(tview.AlignLeft))
table.SetCell(i+offset, 3,
tview.NewTableCell(strconv.Itoa((file.Count()))).
SetAlign(tview.AlignRight))
}
Expand All @@ -198,6 +209,23 @@ func (p *ResultPage) Show() {
p.app.SetRoot(layout, true).SetFocus(layout)
}

func percentageText(total int64, part int64) string {
var sb strings.Builder
sb.WriteString("[")

percentage := int(math.Round(float64(part) / float64(total) * 20))
for i := 1; i <= 20; i++ {
if i <= percentage {
sb.WriteString("#")
} else {
sb.WriteString(" ")
}
}

sb.WriteString("]")
return sb.String()
}

type HelpPage struct {
BasePage
}
Expand Down

0 comments on commit 298b672

Please sign in to comment.