-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (84 loc) · 2.62 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
package main
import (
"fmt"
"os"
"errors"
"encoding/gob"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/pkg/browser"
)
var docStyle = lipgloss.NewStyle().Margin(1, 2)
type item struct {
Titl, Desc, URL string
}
func (i item) Title() string { return i.Titl }
func (i item) Description() string { return i.Desc }
func (i item) FilterValue() string { return i.Titl }
type model struct {
list list.Model
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, nil
case "enter", "space":
if !m.list.SettingFilter() && m.list.SelectedItem() != nil {
err := browser.OpenURL(m.list.SelectedItem().(item).URL)
if err != nil {
fmt.Println("Couldn't open URL in browser: ")
fmt.Println(err)
}
}
}
case tea.WindowSizeMsg:
top, right, bottom, left := docStyle.GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
return docStyle.Render(m.list.View())
}
func main() {
ssrpath := "saved_search_results.binary"
var hits []item
// Use saved search results when available, else crawl
saved_search_results, err := os.Open(ssrpath)
if errors.Is(err, os.ErrNotExist) {
saved_search_results, err = os.OpenFile(ssrpath, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil { panic(err) }
enc := gob.NewEncoder(saved_search_results)
hits = search();
err = enc.Encode(hits); if err != nil { panic(err) }
} else {
fmt.Println("Using previously saved search results.\nTo search again remove the 'saved_search_results.binary' file.")
enc := gob.NewDecoder(saved_search_results)
err = enc.Decode(&hits)
if err != nil {
fmt.Println("Error reading from saved_search_results.binary file.\nTry deleting it to re-search.")
os.Exit(1)
}
}
// TODO: How to cast list of type to list of interface
items := make([]list.Item, len(hits))
for i := range hits {
items[i] = hits[i]
}
m := model{list: list.NewModel(items, list.NewDefaultDelegate(), 0, 0)}
m.list.Title = "LLVM C"
p := tea.NewProgram(m)
p.EnterAltScreen()
if err := p.Start(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}