-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (145 loc) · 5.05 KB
/
main.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
package main
import (
"context"
"encoding/json"
"errors"
"io"
"log"
"github.com/gcla/gowid"
"github.com/gcla/gowid/examples"
"github.com/gcla/gowid/widgets/edit"
"github.com/gcla/gowid/widgets/framed"
"github.com/gcla/gowid/widgets/holder"
"github.com/gcla/gowid/widgets/list"
"github.com/gcla/gowid/widgets/pile"
"github.com/gcla/gowid/widgets/styled"
"github.com/gcla/gowid/widgets/text"
"github.com/gcla/gowid/widgets/vpadding"
"github.com/utagai/look/config"
"github.com/utagai/look/config/custom"
"github.com/utagai/look/data"
"github.com/utagai/look/datum"
"github.com/utagai/look/query"
)
// DefaultBufSizeBytes is 100 MB.
const DefaultBufSizeBytes = 100 * (2 << 20)
func main() {
cfg, err := config.Get()
if err != nil {
log.Fatalf("failed to get a configuration: %v", err)
}
var src io.Reader = cfg.Source
if cfg.CustomFields != nil {
src, err = custom.NewFieldsReader(src, cfg.CustomFields, DefaultBufSizeBytes)
if err != nil {
log.Fatalf("failed to create a custom fields reader: %v", err)
}
}
// FIXME: This is dangerous if the source is large.
bytes, err := io.ReadAll(src)
if err != nil {
log.Fatalf("failed to read all the bytes from the source (%q): %v", cfg.Source.Name(), err)
}
datums := []datum.Datum{}
if err := json.Unmarshal(bytes, &datums); err != nil {
log.Fatalf("failed to unmarshal data into JSON: %v", err)
}
var d data.Data
switch cfg.Backend.Type {
case config.BackendTypeMemory:
d = data.NewMemoryData(datums, query.NewLiquidQueryExecutor())
case config.BackendTypeMongoDB:
d, err = data.NewMongoDBData(cfg.Backend.MongoDB, "look", cfg.Source.Name(), datums)
if err != nil {
log.Fatalf("failed to create the MongoDB backend: %v", err)
}
default:
log.Fatalf("unexpected backend type %q", cfg.Backend.Type)
}
initializeGowid(d)
}
func initializeGowid(d data.Data) {
palette := gowid.Palette{
"title": gowid.MakePaletteEntry(gowid.ColorWhite, gowid.ColorBlack),
"key": gowid.MakePaletteEntry(gowid.ColorCyan, gowid.ColorBlack),
"foot": gowid.MakePaletteEntry(gowid.ColorWhite, gowid.ColorBlack),
"body": gowid.MakePaletteEntry(gowid.ColorWhite, gowid.ColorNone),
"fbody": gowid.MakePaletteEntry(gowid.ColorWhite, gowid.ColorBlack),
}
key := gowid.MakePaletteRef("key")
foot := gowid.MakePaletteRef("foot")
title := gowid.MakePaletteRef("title")
body := gowid.MakePaletteRef("body")
footerContent := []text.ContentSegment{
text.StyledContent("look | ", title),
text.StyledContent("ESC", key),
text.StringContent(" exits."),
}
footerText := styled.New(text.NewFromContent(text.NewContent(footerContent)), foot)
walker := data.NewDataWalker(d)
lb := list.NewBounded(walker)
styledLb := styled.New(lb, body)
queryTextbox := edit.New(edit.Options{Caption: "Query: "})
framedQueryTextboxValid := framed.New(queryTextbox, framed.Options{
Frame: framed.UnicodeFrame,
Style: gowid.MakeForeground(gowid.ColorGreen),
})
framedQueryTextboxInvalid := framed.New(queryTextbox, framed.Options{
Frame: framed.UnicodeFrame,
Style: gowid.MakeForeground(gowid.ColorRed),
})
queryStatusTextbox := edit.New(edit.Options{Text: "Done."})
framedQueryStatusTextboxValid := framed.New(queryStatusTextbox, framed.Options{
Frame: framed.UnicodeFrame,
Style: gowid.MakeForeground(gowid.ColorGreen),
})
framedQueryStatusTextboxInvalid := framed.New(queryStatusTextbox, framed.Options{
Frame: framed.UnicodeFrame,
Style: gowid.MakeForeground(gowid.ColorRed),
})
queryStatusTextboxHolder := holder.New(framedQueryStatusTextboxValid)
queryTextboxHolder := holder.New(framedQueryTextboxValid)
queryTextbox.OnTextSet(gowid.WidgetCallback{
Name: "on query text change",
WidgetChangedFunction: func(app gowid.IApp, w gowid.IWidget) {
newData, err := d.Find(context.Background(), queryTextbox.Text())
if errors.Is(err, query.ErrUnableToParseQuery) {
log.Printf("incomplete query: %q", queryTextbox.Text())
queryTextboxHolder.SetSubWidget(framedQueryTextboxInvalid, app)
queryStatusTextboxHolder.SetSubWidget(framedQueryStatusTextboxInvalid, app)
queryStatusTextbox.SetText(err.Error(), app)
return
} else if err != nil {
log.Fatalf("failed to construct the new data: %v", err)
}
queryTextboxHolder.SetSubWidget(framedQueryTextboxValid, app)
queryStatusTextboxHolder.SetSubWidget(framedQueryStatusTextboxValid, app)
queryStatusTextbox.SetText("Done.", app)
lb.SetWalker(data.NewDataWalker(newData), app)
},
})
view := pile.New([]gowid.IContainerWidget{
&gowid.ContainerWidget{
IWidget: vpadding.New(queryTextboxHolder, gowid.VAlignMiddle{}, gowid.RenderFlow{}),
D: gowid.RenderFlow{},
},
&gowid.ContainerWidget{
IWidget: styledLb,
D: gowid.RenderWithWeight{W: 1},
},
&gowid.ContainerWidget{
IWidget: vpadding.New(queryStatusTextboxHolder, gowid.VAlignMiddle{}, gowid.RenderFlow{}),
D: gowid.RenderFlow{},
},
&gowid.ContainerWidget{
IWidget: footerText,
D: gowid.RenderFlow{},
},
})
app, err := gowid.NewApp(gowid.AppArgs{
View: view,
Palette: &palette,
})
examples.ExitOnErr(err)
app.SimpleMainLoop()
}