-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
207 lines (178 loc) · 4.8 KB
/
table.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package table
import (
"bytes"
"fmt"
"strings"
"github.com/juju/ansiterm"
"golang.org/x/term"
"github.com/elgopher/noteo/date"
"github.com/elgopher/noteo/notes"
"github.com/elgopher/noteo/output"
)
var mapping = map[string]column{
"FILE": fileColumn{},
"BEGINNING": beginningColumn{},
"MODIFIED": modifiedColumn{},
"CREATED": createdColumn{},
"TAGS": tagsColumn{},
}
func NewFormatter(columns []string, dateFormat date.Format) (*Formatter, error) {
w, h, err := term.GetSize(0)
if err != nil {
w = 80
h = 25
}
var cols []string
for _, c := range columns {
c = strings.ToUpper(c)
if _, ok := mapping[c]; !ok {
return nil, fmt.Errorf("unsupported output column: %s", c)
}
cols = append(cols, c)
}
buffer := bytes.NewBuffer([]byte{})
writer := ansiterm.NewTabWriter(buffer, 0, 8, 1, '\t', 0)
writer.SetColorCapable(true)
return &Formatter{
columns: cols,
dateFormat: dateFormat,
width: w,
height: h,
buffer: buffer,
writer: writer,
},
nil
}
type Formatter struct {
columns []string
dateFormat date.Format
width int
height int
line int
writer *ansiterm.TabWriter
buffer *bytes.Buffer
}
func (o *Formatter) flush() string {
_ = o.writer.Flush()
out := o.buffer.String()
o.buffer.Reset()
o.line = 0
return out
}
func (o *Formatter) Header() string {
o.line++
for _, c := range o.columns {
options := opts{dateFormat: o.dateFormat}
column := mapping[c]
column.printHeader(options, o.writer)
_, _ = o.writer.Write([]byte("\t"))
}
_, _ = o.writer.Write([]byte("\n"))
return ""
}
func (o *Formatter) Footer() string {
return o.flush()
}
func (o *Formatter) Note(note notes.Note) string {
out := ""
if o.line == o.height {
out = o.flush()
}
o.line++
for _, c := range o.columns {
options := opts{dateFormat: o.dateFormat}
column := mapping[c]
column.printValue(note, options, o.writer)
_, _ = o.writer.Write([]byte("\t"))
}
_, _ = o.writer.Write([]byte("\n"))
return out
}
func format(text string, limit int) string {
runes := []rune(text) // cast is need to make len work
if len(runes) > limit {
runes = runes[:limit-1]
runes = append(runes, '…')
}
return fmt.Sprintf("%-*s", limit, string(runes))
}
func beginning(text string) string {
t := strings.Trim(text, "\n")
if strings.Contains(t, "\n") {
t = t[:strings.IndexRune(t, '\n')]
}
t = strings.ReplaceAll(t, "\t", " ")
for i := 0; i < 5; i++ {
t = strings.TrimPrefix(t, "#")
}
t = strings.TrimPrefix(t, "*")
t = strings.ReplaceAll(t, "\r", "")
t = strings.Trim(t, " ")
return t
}
type column interface {
printHeader(opts opts, writer *ansiterm.TabWriter)
printValue(note notes.Note, opts opts, writer *ansiterm.TabWriter)
}
type opts struct {
dateFormat date.Format
}
type fileColumn struct{}
func (f fileColumn) printHeader(opts opts, writer *ansiterm.TabWriter) {
_, _ = writer.Write([]byte("FILE"))
}
func (f fileColumn) printValue(note notes.Note, opts opts, writer *ansiterm.TabWriter) {
writer.SetForeground(ansiterm.BrightBlue)
defer writer.Reset()
_, _ = fmt.Fprint(writer, note.Path())
}
type beginningColumn struct{}
func (s beginningColumn) printHeader(opts opts, writer *ansiterm.TabWriter) {
_, _ = fmt.Fprint(writer, format("BEGINNING", 34))
}
func (s beginningColumn) printValue(note notes.Note, opts opts, writer *ansiterm.TabWriter) {
body, _ := note.Body()
writer.SetStyle(ansiterm.Bold)
defer writer.Reset()
_, _ = fmt.Fprint(writer, format(beginning(body), 34))
}
type modifiedColumn struct{}
func (m modifiedColumn) printHeader(_ opts, writer *ansiterm.TabWriter) {
_, _ = writer.Write([]byte("MODIFIED"))
}
func (m modifiedColumn) printValue(note notes.Note, opts opts, writer *ansiterm.TabWriter) {
modified, err := note.Modified()
if err != nil {
writeError(err, writer)
return
}
formatted := date.FormatWithType(modified, opts.dateFormat)
_, _ = fmt.Fprint(writer, formatted)
}
func writeError(err error, writer *ansiterm.TabWriter) {
errString := err.Error()
errString = strings.ReplaceAll(errString, "\t", " ")
_, _ = fmt.Fprint(writer, errString)
}
type createdColumn struct{}
func (c createdColumn) printHeader(_ opts, writer *ansiterm.TabWriter) {
_, _ = writer.Write([]byte("CREATED"))
}
func (c createdColumn) printValue(note notes.Note, opts opts, writer *ansiterm.TabWriter) {
created, err := note.Created()
if err != nil {
writeError(err, writer)
return
}
modified := date.FormatWithType(created, opts.dateFormat)
_, _ = fmt.Fprint(writer, modified)
}
type tagsColumn struct{}
func (t tagsColumn) printHeader(_ opts, writer *ansiterm.TabWriter) {
_, _ = writer.Write([]byte("TAGS"))
}
func (t tagsColumn) printValue(note notes.Note, opts opts, writer *ansiterm.TabWriter) {
tags, _ := output.StringTags(note)
tagsString := strings.Join(tags, " ")
_, _ = fmt.Fprint(writer, tagsString)
}