-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
185 lines (164 loc) · 5.15 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"cmp"
"context"
"errors"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/bubbletea"
"github.com/charmbracelet/wish/logging"
"github.com/muesli/termenv"
"net"
"os"
"os/signal"
"ssh-news/provider"
"syscall"
"time"
)
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
defer func() {
switch m.selected {
case selectedSources:
// Set style of news-selected item to normal
delegate := list.NewDefaultDelegate()
delegate.SetSpacing(0)
delegate.Styles.SelectedTitle = delegate.Styles.NormalTitle
delegate.Styles.SelectedDesc = delegate.Styles.NormalDesc
selectedNews := m.news[m.sources.SelectedItem().(sourcesItem).Title()]
selectedNews.SetDelegate(delegate)
selectedNews.ResetSelected()
m.news[m.sources.SelectedItem().(sourcesItem).Title()] = selectedNews
case selectedNews:
// Set style of news-selected item to default
selectedNews := m.news[m.sources.SelectedItem().(sourcesItem).Title()]
delegate := list.NewDefaultDelegate()
delegate.SetSpacing(0)
selectedNews.SetDelegate(delegate)
m.news[m.sources.SelectedItem().(sourcesItem).Title()] = selectedNews
}
// Set height of news and sources to current window height
selectedNews := m.news[m.sources.SelectedItem().(sourcesItem).Title()]
selectedNews.SetHeight(m.currWindowsHeight)
selectedNews.SetWidth(m.currWindowsWidth)
m.news[m.sources.SelectedItem().(sourcesItem).Title()] = selectedNews
m.sources.SetHeight(m.currWindowsHeight)
}()
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "left", "h":
if m.news[m.sources.SelectedItem().(sourcesItem).Title()].Paginator.Page == 0 {
m.selected = selectedSources
selectedSourcesIdx := m.sources.Index()
newM := newModel(provider.Get(), m.session)
m.news = newM.news
m.sources = newM.sources
m.sources.Select(selectedSourcesIdx)
}
case "right", "l":
if m.selected == selectedSources {
m.selected = selectedNews
return m, cmd
}
case "enter", " ":
if m.selected == selectedNews {
news := m.news[m.sources.SelectedItem().(sourcesItem).Title()]
selected := news.SelectedItem().(newsItem)
selectedIdx := news.Index()
copyURL(selected.url, m.session)
selected.desc = "已复制链接到剪贴板"
news.SetItem(selectedIdx, selected)
m.news[m.sources.SelectedItem().(sourcesItem).Title()] = news
}
}
case tea.WindowSizeMsg:
m.currWindowsHeight = msg.Height
m.currWindowsWidth = msg.Width
}
// pass the message to the selected model
switch m.selected {
case selectedSources:
m.sources, cmd = m.sources.Update(msg)
case selectedNews:
m.news[m.sources.SelectedItem().(sourcesItem).Title()], cmd = m.news[m.sources.SelectedItem().(sourcesItem).Title()].Update(msg)
}
return m, cmd
}
func (m model) View() string {
return lipgloss.JoinHorizontal(lipgloss.Left, m.sources.View(), m.news[m.sources.SelectedItem().(sourcesItem).Title()].View())
}
func init() {
//f, err := tea.LogToFile("debug.log", "")
//if err != nil {
// // log.Fatal(err)
//}
//defer f.Close()
}
var (
host = cmp.Or(os.Getenv("HOST"), "0.0.0.0")
port = cmp.Or(os.Getenv("PORT"), "32000")
)
func main() {
if os.Getenv("LOG_FILE") != "" {
os.MkdirAll("logs", 0755)
file, err := os.OpenFile("logs/logs.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Error("Could not open log file", "error", err)
} else {
log.SetOutput(file)
}
}
cronFetch()
s, err := wish.NewServer(
wish.WithAddress(net.JoinHostPort(host, port)),
wish.WithHostKeyPath(".ssh/id_ed25519"),
wish.WithMiddleware(
myCustomBubbleteaMiddleware(),
logging.Middleware(),
),
)
if err != nil {
log.Error("Could not start server", "error", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Info("Starting SSH server", "host", host, "port", port)
go func() {
if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("Could not start server", "error", err)
done <- nil
}
}()
<-done
log.Info("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("Could not stop server", "error", err)
}
}
func copyURL(url string, s ssh.Session) {
bubbletea.MakeRenderer(s).Output().Copy(url)
}
func myCustomBubbleteaMiddleware() wish.Middleware {
newProg := func(m tea.Model, opts ...tea.ProgramOption) *tea.Program {
p := tea.NewProgram(m, opts...)
return p
}
teaHandler := func(s ssh.Session) *tea.Program {
m := newModel(provider.Get(), s)
bubbletea.MakeRenderer(s).Output().SetWindowTitle("SSH News")
return newProg(m, append(bubbletea.MakeOptions(s), tea.WithAltScreen())...)
}
return bubbletea.MiddlewareWithProgramHandler(teaHandler, termenv.ANSI256)
}