-
Notifications
You must be signed in to change notification settings - Fork 0
/
t-plot.go
191 lines (155 loc) · 3.83 KB
/
t-plot.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Options:
- `-k N` - column number for plot (default: 1)
- `-c "#"` - chart character (default: `#`)
- `-w N` - width of chart (default: rest of terminal width using $COLUMNS)
- `-h` - print help and exit
*/
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"unicode/utf8"
"github.com/msoap/byline"
"github.com/msoap/tcg"
"golang.org/x/term"
)
const (
defaultTermWidth = 80
maxTermWidth = 150
minChartWidth = 10
widthReserve = 8
)
type opt struct {
columnN int
barChar string
width int
}
type lineData struct {
num int
width int
}
func main() {
cfg := parseArgs()
lines, err := readStdin()
if err != nil {
printErr("read stdin: %s\n", err)
}
info := getTextInfo(cfg, lines)
maxs := getAllMax(info)
chartLines := addChart(cfg, lines, info, maxs)
fmt.Println(strings.Join(chartLines, "\n"))
}
func printErr(frmt string, args ...any) {
fmt.Fprintf(os.Stderr, frmt, args...)
os.Exit(1)
}
func parseArgs() opt {
res := opt{}
doHelp := flag.Bool("h", false, "print help and exit")
flag.IntVar(&res.columnN, "k", 1, "column number for plot")
flag.StringVar(&res.barChar, "c", "#", "bar chart character")
flag.IntVar(&res.width, "w", 0, "width of chart")
flag.Parse()
if *doHelp {
flag.PrintDefaults()
os.Exit(0)
}
if len(res.barChar) == 0 {
printErr("bar chart character is empty\n")
}
return res
}
func readStdin() ([]string, error) {
return byline.
NewReader(os.Stdin).
MapString(func(in string) string {
// strip trailing newline
return strings.TrimRight(in, "\n")
}).
ReadAllSliceString()
}
func getTextInfo(cfg opt, lines []string) []lineData {
res := make([]lineData, len(lines))
for i, line := range lines {
res[i].width = utf8.RuneCountInString(line) // TODO: use graphic symbol length
fields := strings.Fields(line)
if cfg.columnN > len(fields) {
continue
}
res[i].num, _ = strconv.Atoi(fields[cfg.columnN-1])
}
return res
}
func getAllMax(info []lineData) lineData {
maxNum, maxWidth := 0, 0
for _, item := range info {
if item.num > maxNum {
maxNum = item.num
}
if item.width > maxWidth {
maxWidth = item.width
}
}
return lineData{maxNum, maxWidth}
}
func addChart(cfg opt, lines []string, info []lineData, maxs lineData) []string {
termWidth := getTermWidth()
lines = alignTextLines(lines, maxs)
chartWidth := 0
if cfg.width > 0 {
chartWidth = cfg.width
} else {
chartWidth = termWidth - maxs.width - widthReserve
if chartWidth < minChartWidth {
chartWidth = minChartWidth
}
}
barChart := renderChart(cfg.barChar, chartWidth, info, maxs)
res := make([]string, len(lines))
for i := range lines {
res[i] = lines[i] + "\t" + barChart[i]
}
return res
}
func getTermWidth() int {
// "tput cols"/"stty size"/$COLUMNS is not working in programs
width, _, _ := term.GetSize(int(os.Stdout.Fd()))
if width == 0 {
width = defaultTermWidth
}
if width > maxTermWidth {
width = maxTermWidth
}
return width
}
func alignTextLines(lines []string, maxs lineData) []string {
res := make([]string, len(lines))
for i, line := range lines {
if l := utf8.RuneCountInString(line); l < maxs.width {
line += strings.Repeat(" ", maxs.width-l)
}
res[i] = line
}
return res
}
func renderChart(barChar string, width int, info []lineData, maxs lineData) []string {
canvas := tcg.NewBuffer(width, len(info))
for i, item := range info {
chartWidth := int(float64(item.num) / float64(maxs.num) * float64(width))
canvas.HLine(0, i, chartWidth, tcg.Black)
}
firstRune, _ := utf8.DecodeRuneInString(barChar)
mode, err := tcg.NewPixelMode(1, 1, []rune{' ', firstRune})
if err != nil {
printErr("create pixel mode for %q: %s\n", barChar, err)
}
res := canvas.RenderAsStrings(*mode)
if len(info) != len(res) {
printErr("something went wrong, len(info) != len(res), %d != %d\n", len(info), len(res))
}
return res
}