Skip to content

Commit 567b043

Browse files
committed
Color node size attributes in long format.
Color config from https://github.com/garabik/grc/blob/master/colourfiles/conf.ls Resolves #13
1 parent 75bf49b commit 567b043

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

format/long.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package format
22

33
import (
44
"fmt"
5+
"math"
56
"regexp"
67
"strings"
78
"strconv"
@@ -25,8 +26,8 @@ func Long(nodes []node.Node) {
2526
formatMode(node.Mode),
2627
strconv.Itoa(node.LinkCount),
2728
fmt.Sprintf("%s ", node.User),
28-
fmt.Sprintf("%s ", node.Group),
29-
humanize.Bytes(node.Size),
29+
fmt.Sprintf("%s ", node.Group),
30+
colorSize(node.Size, humanize.Bytes(node.Size)),
3031
node.Time.Month().String()[:3],
3132
fmt.Sprintf("%2d", node.Time.Day()),
3233
fmt.Sprintf("%02d:%02d", node.Time.Hour(), node.Time.Minute()),
@@ -63,6 +64,21 @@ func Long(nodes []node.Node) {
6364
}
6465
}
6566

67+
func colorSize(sizeInt int, str string) string {
68+
size := float64(sizeInt)
69+
base := float64(1024)
70+
// less than 1K
71+
if size < base { return str }
72+
// less than 10M
73+
if size < math.Pow(base, 2) * 10 { return color.GreenString(str) }
74+
// less than 100M
75+
if size < math.Pow(base, 2) * 100 { return color.YellowString(str) }
76+
// less than 1G
77+
if size < math.Pow(base, 3) { return color.RedString(str) }
78+
// above 1G
79+
return color.New(color.FgRed, color.Bold).Sprint(str)
80+
}
81+
6682
// strips ANSI color codes from string
6783
func strip(str string) string {
6884
return ansiRegex.ReplaceAllString(str, "")
@@ -72,7 +88,7 @@ func formatMode(mode string) string {
7288
runes := []rune(mode)
7389

7490
return fmt.Sprintf(
75-
"%s%s%s%s%s%s%s%s%s%s",
91+
"%s%s%s%s%s%s%s%s%s%s ",
7692
colorize(runes[0], color.New(color.FgWhite, color.Bold)),
7793
colorize(runes[1], color.New(color.FgGreen)),
7894
colorize(runes[2], color.New(color.FgGreen)),

0 commit comments

Comments
 (0)