summaryrefslogtreecommitdiffstats
path: root/tui/format.go
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2021-02-20 20:45:31 +0100
committerDaniel Milde <daniel@milde.cz>2021-02-20 20:45:31 +0100
commit3957e1ca2be7402b2e032b4de54193acd80def18 (patch)
treeec3abc3bf05c5ebf945b054e270e0ffffbd3bacc /tui/format.go
parent25309b070ba9463262563f10c67a242924ee4fdf (diff)
tui split into more files, keyPressed refactored
Diffstat (limited to 'tui/format.go')
-rw-r--r--tui/format.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/tui/format.go b/tui/format.go
new file mode 100644
index 0000000..d599201
--- /dev/null
+++ b/tui/format.go
@@ -0,0 +1,70 @@
+package tui
+
+import (
+ "fmt"
+ "math"
+
+ "github.com/dundee/gdu/analyze"
+)
+
+func (ui *UI) formatFileRow(item *analyze.File) string {
+ var part int
+
+ if ui.showApparentSize {
+ part = int(float64(item.Size) / float64(item.Parent.Size) * 10.0)
+ } else {
+ part = int(float64(item.Usage) / float64(item.Parent.Usage) * 10.0)
+ }
+
+ row := string(item.Flag)
+
+ if ui.useColors {
+ row += "[#e67100:-:b]"
+ } else {
+ row += "[white:-:b]"
+ }
+
+ if ui.showApparentSize {
+ row += fmt.Sprintf("%21s", ui.formatSize(item.Size, false))
+ } else {
+ row += fmt.Sprintf("%21s", ui.formatSize(item.Usage, false))
+ }
+
+ row += getUsageGraph(part)
+
+ if item.IsDir {
+ if ui.useColors {
+ row += "[#3498db::b]/"
+ } else {
+ row += "[::b]/"
+ }
+ }
+ row += item.Name
+ return row
+}
+
+func (ui *UI) formatSize(size int64, reverseColor bool) string {
+ var color string
+ if reverseColor {
+ if ui.useColors {
+ color = "[black:#2479d0:-]"
+ } else {
+ color = "[black:white:-]"
+ }
+ } else {
+ color = "[white:-:-]"
+ }
+
+ switch {
+ case size > 1e12:
+ return fmt.Sprintf("%.1f%s TiB", float64(size)/math.Pow(2, 40), color)
+ case size > 1e9:
+ return fmt.Sprintf("%.1f%s GiB", float64(size)/math.Pow(2, 30), color)
+ case size > 1e6:
+ return fmt.Sprintf("%.1f%s MiB", float64(size)/math.Pow(2, 20), color)
+ case size > 1e3:
+ return fmt.Sprintf("%.1f%s KiB", float64(size)/math.Pow(2, 10), color)
+ default:
+ return fmt.Sprintf("%d%s B", size, color)
+ }
+}