-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterm_helpers.go
92 lines (81 loc) · 1.66 KB
/
term_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"strings"
"github.com/blugelabs/bluge"
"github.com/muesli/reflow/padding"
"github.com/muesli/termenv"
)
const (
headerColor = "#ffb236"
colPadding = 20
)
var filterField = func(name string) bool {
switch name {
case "_id", "album", "genre", "year", "filename", "title", "artist":
return false
default:
return true
}
}
var filterFieldPlay = func(name string) bool {
switch name {
case "_id", "cached metadata", "album", "genre", "year", "filename", "title", "artist":
return false
default:
return true
}
}
func colorize(str, color string) string {
out := termenv.String(str)
p := termenv.ColorProfile()
return out.Foreground(p.Color(color)).String()
}
func printRow(header, value, color string) {
fmt.Printf("%s %s\n", padding.String(colorize(header+":", color), colPadding), value)
}
func printMetadata(field string, value []byte, color string) {
var f string
if field == "_id" {
f = "ID"
} else if field == "repository_id" {
f = "Repository ID"
} else {
f = strings.Title(strings.ReplaceAll(field, "_", " "))
}
v := ""
switch field {
case "mtime":
t, err := bluge.DecodeDateTime(value)
if err != nil {
v = "error"
} else {
v = t.Format("2006-1-2")
}
case "size":
t, err := bluge.DecodeNumericFloat64(value)
if err != nil {
v = "error"
} else {
v = fmt.Sprintf("%0.f", t)
}
case "year":
y, err := bluge.DecodeNumericFloat64(value)
if err != nil {
v = "error"
}
if y != 0 {
v = fmt.Sprintf("%0.f", y)
}
case "metadata source":
v = "🌍"
if string(value) == "true" {
v = "💾"
}
default:
v = string(value)
}
if v != "" {
printRow(f, v, headerColor)
}
}