-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (135 loc) · 3.22 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
package main
//go:generate gotext update -lang en-US,pt -out catalog.go
import (
"bytes"
"fmt"
"io"
"log"
url2 "net/url"
"os"
"golang.org/x/text/message"
"github.com/Xuanwo/go-locale"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/parnurzeal/gorequest"
)
var p *message.Printer
var docStyle = lipgloss.NewStyle().Margin(1, 2)
type screen interface {
Reset()
Update(msg tea.Msg) (tea.Model, tea.Cmd)
View() string
}
var currentScreen screen
type model struct {
err error
list list.Model
queryTextInput textinput.Model
viewport viewport.Model
ready bool
selectedTopic string
selectedQuery string
resultContent string
}
func (m *model) Init() tea.Cmd {
return nil
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg, ok := msg.(tea.WindowSizeMsg); ok {
if !m.ready {
m.ready = true
m.viewport = viewport.Model{Width: msg.Width, Height: msg.Height - (headerHeight + footerHeight)}
m.viewport.YPosition = headerHeight + 1
}
}
return currentScreen.Update(msg)
}
func (m model) View() string {
return currentScreen.View()
}
func (m *model) loadContents() error {
query := m.selectedTopic
if m.selectedQuery != "" {
if m.selectedQuery[0] == ':' {
query += "/" + m.selectedQuery
} else if escapedQuery := url2.QueryEscape(m.selectedQuery); escapedQuery != "" {
query += "/" + escapedQuery
}
}
url := fmt.Sprintf("https://cht.sh/%s?style=paraiso-dark", query)
resp, _, errs := gorequest.New().
Get(url).
Set("User-Agent", "curl").
End()
if errs != nil {
for _, err := range errs {
log.Println("loadContents:", err)
}
return errs[0]
}
defer resp.Body.Close()
var all []byte
var err error
if all, err = io.ReadAll(resp.Body); err != nil {
return err
}
m.resultContent = string(all)
return nil
}
func InitialModel() *model {
resp, _, errs := gorequest.New().
Get("https://cht.sh/:list").
Set("User-Agent", "curl").
End()
if errs != nil {
for _, err := range errs {
log.Println("main:", err)
}
os.Exit(-1)
}
defer resp.Body.Close()
all, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln("main:", err)
}
lines := bytes.Split(all, []byte("\n"))
var items = make([]list.Item, 0, len(lines))
for _, each := range lines {
items = append(items, item(each))
}
tm := textinput.NewModel()
tm.Placeholder = p.Sprintf("Your query")
tm.Focus()
tm.CharLimit = 156
tm.Width = 20
lm := list.NewModel(items, itemDelegate{}, 0, 0)
// TODO: enable filtering by default when it's available
// https://github.com/charmbracelet/bubbles/issues/85
m := model{
list: lm,
queryTextInput: tm,
}
m.list.Title = p.Sprintf("Topics")
return &m
}
func setCurrentScreen(scr screen) {
currentScreen = scr
currentScreen.Reset()
}
func main() {
tag, err := locale.Detect()
if err != nil {
log.Fatal(err)
}
log.Println("Tag:", tag)
p = message.NewPrinter(tag)
m := InitialModel()
setCurrentScreen(&topicScreen{model: m})
p := tea.NewProgram(m, tea.WithAltScreen())
if err := p.Start(); err != nil {
log.Fatal("Error running program:", err)
}
}