-
Notifications
You must be signed in to change notification settings - Fork 14
/
view.go
196 lines (164 loc) · 3.97 KB
/
view.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
package myqlib
import (
"bytes"
"fmt"
)
// All Views must implement the following
type View interface {
// Column help
Help() chan string
ShortHelp() chan string
// Header/Data functions return a channel of strings
Header(state *MyqState) chan string
Data(state *MyqState) chan string
// Use this timecol in the output
SetTimeCol(timecol *Col)
// All the cols (including time col)
all_cols() []Col
}
// NormalView
type NormalView struct {
DefaultCol // Views are columns too
cols []Col // slice of columns in this view
timecol *Col // timecol to use
}
func NewNormalView(help string, cols ...Col) *NormalView {
return &NormalView{DefaultCol: DefaultCol{help: help}, cols: cols}
}
func (v *NormalView) Help() chan string {
ch := make(chan string)
go func() {
defer close(ch)
for shortst := range v.ShortHelp() {
ch <- shortst
}
for _, col := range v.cols {
for colst := range col.Help() {
ch <- fmt.Sprint("\t", colst)
}
}
}()
return ch
}
func (v *NormalView) ShortHelp() chan string {
ch := make(chan string, 1)
defer close(ch)
ch <- fmt.Sprint(v.help)
return ch
}
func (v *NormalView) SetTimeCol(timecol *Col) {
v.timecol = timecol
}
func (v *NormalView) Header(state *MyqState) chan string {
return v.ordered_col_output(func(c Col) chan string {
return c.Header(state)
})
}
func (v *NormalView) Data(state *MyqState) chan string {
return v.ordered_col_output(func(c Col) chan string {
return c.Data(state)
})
}
func (v *NormalView) ordered_col_output(get_col_chan func(c Col) chan string) chan string {
var column_channels []chan string
for _, col := range v.all_cols() {
column_channels = append(column_channels, get_col_chan(col))
}
ch := make(chan string)
go func() {
defer close(ch)
for {
var hdrline bytes.Buffer
got_something := false
space := false
for i, col := range v.all_cols() {
if space {
hdrline.WriteString(" ")
} else {
space = true
}
if str, more := <-column_channels[i]; more {
hdrline.WriteString(str)
got_something = true
} else {
hdrline.WriteString(column_blank(col))
}
}
if got_something {
ch <- hdrline.String()
} else {
break
}
}
}()
return ch
}
// All columns preceeded by the time column
func (v *NormalView) all_cols() []Col {
if v.timecol == nil {
return v.cols
} else {
return append([]Col{*v.timecol}, v.cols...)
}
}
func (v *NormalView) Width() (w int64) {
for _, col := range v.all_cols() {
w += col.Width() + 1
}
w -= 1
return
}
// ExtraHeaderView
type ExtraHeaderView struct {
NormalView
extra_header func(state *MyqState) chan string
}
func NewExtraHeaderView(help string, extra_header func(state *MyqState) chan string, cols ...Col) *ExtraHeaderView {
return &ExtraHeaderView{NormalView{DefaultCol: DefaultCol{help: help}, cols: cols}, extra_header}
}
func (v *ExtraHeaderView) Header(state *MyqState) chan string {
ch := make(chan string)
go func() {
defer close(ch)
normalch := v.NormalView.Header(state)
for normalstr := range normalch {
ch <- normalstr
}
// Extra headers come out above normal headers, which means we send them later
extrach := v.extra_header(state)
for extrastr := range extrach {
ch <- extrastr
}
}()
return ch
}
// ExtraHeaderView
type GroupCol struct {
NormalView
title string
}
func NewGroupCol(title, help string, cols ...Col) *GroupCol {
return &GroupCol{NormalView{DefaultCol: DefaultCol{help: help}, cols: cols}, title}
}
// All columns preceeded by the time column
func (v *GroupCol) all_cols() []Col {
return v.cols
}
func (v *GroupCol) Header(state *MyqState) chan string {
ch := make(chan string)
go func() {
defer close(ch)
// Output the columns first
viewch := v.NormalView.Header(state)
for viewstr := range viewch {
ch <- viewstr
}
// Then our title (reverse order)
str := v.title
if len(str) > int(v.Width()) {
str = v.title[0:v.Width()]
}
ch <- fmt.Sprintf(fmt.Sprint(`%-`, v.Width(), `s`), str)
}()
return ch
}