commit b0a5b6c
Hubert Hirtz
·
2020-08-19 14:01:25 +0000 UTC
parent 4d806b7
ui: Don't panic when the connection fails
2 files changed,
+65,
-70
M
app.go
M
app.go
+60,
-65
1@@ -14,7 +14,7 @@ import (
2
3 type App struct {
4 win *ui.UI
5- s irc.Session
6+ s *irc.Session
7
8 cfg Config
9 highlights []string
10@@ -25,6 +25,13 @@ type App struct {
11 func NewApp(cfg Config) (app *App, err error) {
12 app = &App{}
13
14+ if cfg.Highlights != nil {
15+ app.highlights = make([]string, len(cfg.Highlights))
16+ for i := range app.highlights {
17+ app.highlights[i] = strings.ToLower(cfg.Highlights[i])
18+ }
19+ }
20+
21 app.win, err = ui.New(ui.Config{
22 NickColWidth: cfg.NickColWidth,
23 })
24@@ -36,7 +43,8 @@ func NewApp(cfg Config) (app *App, err error) {
25 app.win.AddLine(ui.Home, ui.NewLineNow("--", fmt.Sprintf("Connecting to %s...", cfg.Addr)))
26 conn, err = tls.Dial("tcp", cfg.Addr, nil)
27 if err != nil {
28- app.win.Close()
29+ app.win.AddLine(ui.Home, ui.NewLineNow("ERROR --", "Connection failed"))
30+ err = nil
31 return
32 }
33
34@@ -52,34 +60,32 @@ func NewApp(cfg Config) (app *App, err error) {
35 Debug: cfg.Debug,
36 })
37 if err != nil {
38- app.win.Close()
39- _ = conn.Close()
40- return
41+ app.win.AddLine(ui.Home, ui.NewLineNow("ERROR --", "Registration failed"))
42 }
43
44- if cfg.Highlights != nil {
45- app.highlights = cfg.Highlights
46- for i := range app.highlights {
47- app.highlights[i] = strings.ToLower(app.highlights[i])
48- }
49- } else {
50- app.highlights = []string{app.s.NickCf()}
51- }
52+ app.highlights = append(app.highlights, app.s.NickCf())
53
54 return
55 }
56
57 func (app *App) Close() {
58 app.win.Close()
59- app.s.Stop()
60+ if app.s != nil {
61+ app.s.Stop()
62+ }
63 }
64
65 func (app *App) Run() {
66 for !app.win.ShouldExit() {
67- select {
68- case ev := <-app.s.Poll():
69- app.handleIRCEvent(ev)
70- case ev := <-app.win.Events:
71+ if app.s != nil {
72+ select {
73+ case ev := <-app.s.Poll():
74+ app.handleIRCEvent(ev)
75+ case ev := <-app.win.Events:
76+ app.handleUIEvent(ev)
77+ }
78+ } else {
79+ ev := <-app.win.Events
80 app.handleUIEvent(ev)
81 }
82 }
83@@ -176,61 +182,26 @@ func (app *App) handleUIEvent(ev tcell.Event) {
84 app.win.Resize()
85 case tcell.KeyCtrlU, tcell.KeyPgUp:
86 app.win.ScrollUp()
87- buffer := app.win.CurrentBuffer()
88- if app.win.IsAtTop() && buffer != ui.Home {
89- at := time.Now()
90- if t := app.win.CurrentBufferOldestTime(); t != nil {
91- at = *t
92- }
93- app.s.RequestHistory(buffer, at)
94- }
95+ app.requestHistory()
96 case tcell.KeyCtrlD, tcell.KeyPgDn:
97 app.win.ScrollDown()
98 case tcell.KeyCtrlN:
99 app.win.NextBuffer()
100- buffer := app.win.CurrentBuffer()
101- if app.win.IsAtTop() && buffer != ui.Home {
102- at := time.Now()
103- if t := app.win.CurrentBufferOldestTime(); t != nil {
104- at = *t
105- }
106- app.s.RequestHistory(buffer, at)
107- }
108+ app.requestHistory()
109 case tcell.KeyCtrlP:
110 app.win.PreviousBuffer()
111- buffer := app.win.CurrentBuffer()
112- if app.win.IsAtTop() && buffer != ui.Home {
113- at := time.Now()
114- if t := app.win.CurrentBufferOldestTime(); t != nil {
115- at = *t
116- }
117- app.s.RequestHistory(buffer, at)
118- }
119+ app.requestHistory()
120 case tcell.KeyRight:
121 if ev.Modifiers() == tcell.ModAlt {
122 app.win.NextBuffer()
123- buffer := app.win.CurrentBuffer()
124- if app.win.IsAtTop() && buffer != ui.Home {
125- at := time.Now()
126- if t := app.win.CurrentBufferOldestTime(); t != nil {
127- at = *t
128- }
129- app.s.RequestHistory(buffer, at)
130- }
131+ app.requestHistory()
132 } else {
133 app.win.InputRight()
134 }
135 case tcell.KeyLeft:
136 if ev.Modifiers() == tcell.ModAlt {
137 app.win.PreviousBuffer()
138- buffer := app.win.CurrentBuffer()
139- if app.win.IsAtTop() && buffer != ui.Home {
140- at := time.Now()
141- if t := app.win.CurrentBufferOldestTime(); t != nil {
142- at = *t
143- }
144- app.s.RequestHistory(buffer, at)
145- }
146+ app.requestHistory()
147 } else {
148 app.win.InputLeft()
149 }
150@@ -244,13 +215,13 @@ func (app *App) handleUIEvent(ev tcell.Event) {
151 app.win.InputEnd()
152 case tcell.KeyBackspace2:
153 ok := app.win.InputBackspace()
154- if ok && app.win.InputLen() == 0 {
155- app.s.TypingStop(app.win.CurrentBuffer())
156+ if ok {
157+ app.typing()
158 }
159 case tcell.KeyDelete:
160 ok := app.win.InputDelete()
161- if ok && app.win.InputLen() == 0 {
162- app.s.TypingStop(app.win.CurrentBuffer())
163+ if ok {
164+ app.typing()
165 }
166 case tcell.KeyCR, tcell.KeyLF:
167 buffer := app.win.CurrentBuffer()
168@@ -258,9 +229,7 @@ func (app *App) handleUIEvent(ev tcell.Event) {
169 app.handleInput(buffer, input)
170 case tcell.KeyRune:
171 app.win.InputRune(ev.Rune())
172- if app.win.CurrentBuffer() != ui.Home && !app.win.InputIsCommand() && !(app.win.InputLen() == 0 && ev.Rune() == '/') {
173- app.s.Typing(app.win.CurrentBuffer())
174- }
175+ app.typing()
176 }
177 }
178 }
179@@ -278,6 +247,32 @@ func (app *App) isHighlight(nick, content string) bool {
180 return false
181 }
182
183+func (app *App) requestHistory() {
184+ if app.s == nil {
185+ return
186+ }
187+ buffer := app.win.CurrentBuffer()
188+ if app.win.IsAtTop() && buffer != ui.Home {
189+ at := time.Now()
190+ if t := app.win.CurrentBufferOldestTime(); t != nil {
191+ at = *t
192+ }
193+ app.s.RequestHistory(buffer, at)
194+ }
195+}
196+
197+func (app *App) typing() {
198+ if app.s == nil {
199+ return
200+ }
201+ buffer := app.win.CurrentBuffer()
202+ if app.win.InputLen() == 0 {
203+ app.s.TypingStop(buffer)
204+ } else if buffer != ui.Home && !app.win.InputIsCommand() {
205+ app.s.Typing(app.win.CurrentBuffer())
206+ }
207+}
208+
209 func parseCommand(s string) (command, args string) {
210 if s == "" {
211 return
+5,
-5
1@@ -157,8 +157,8 @@ type Session struct {
2 chBatches map[string]HistoryEvent
3 }
4
5-func NewSession(conn io.ReadWriteCloser, params SessionParams) (s Session, err error) {
6- s = Session{
7+func NewSession(conn io.ReadWriteCloser, params SessionParams) (*Session, error) {
8+ s := &Session{
9 conn: conn,
10 msgs: make(chan Message, 16),
11 acts: make(chan action, 16),
12@@ -180,9 +180,9 @@ func NewSession(conn io.ReadWriteCloser, params SessionParams) (s Session, err e
13
14 s.running.Store(true)
15
16- err = s.send("CAP LS 302\r\nNICK %s\r\nUSER %s 0 * :%s\r\n", s.nick, s.user, s.real)
17+ err := s.send("CAP LS 302\r\nNICK %s\r\nUSER %s 0 * :%s\r\n", s.nick, s.user, s.real)
18 if err != nil {
19- return
20+ return nil, err
21 }
22
23 go func() {
24@@ -206,7 +206,7 @@ func NewSession(conn io.ReadWriteCloser, params SessionParams) (s Session, err e
25
26 go s.run()
27
28- return
29+ return s, nil
30 }
31
32 func (s *Session) Running() bool {