From 9830065f5cb9d5cf85802a3c86380601f843f376 Mon Sep 17 00:00:00 2001 From: "Javier B. Torres" Date: Mon, 2 Feb 2026 13:56:16 -0300 Subject: [PATCH] * --- .gitignore | 2 ++ event.go | 6 ++++-- main.go | 55 +++++++++++++++++++++++++++++++++++------------------- shell.nix | 8 ++++++++ 4 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore index 898bace..7ad239c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /nanite /exp/ +/.envrc +/result diff --git a/event.go b/event.go index 7a5be88..fc0dc1f 100644 --- a/event.go +++ b/event.go @@ -96,8 +96,10 @@ func (_ HangupEvent) HandleOutgoing(app *App) error { app.conn.stop() app.conn.Write([]byte("QUIT\n")) app.conn.Close() - app.conn.ticker.Stop() - app.conn.ticker = nil + if app.conn.ticker != nil { + app.conn.ticker.Stop() + app.conn.ticker = nil + } app.conn = nil app.incoming <- HangupEvent{host, port} diff --git a/main.go b/main.go index 94a51ff..f72efe7 100644 --- a/main.go +++ b/main.go @@ -102,6 +102,41 @@ func (app *App) EnsureConfigDir() error { return nil } +func (app *App) Error(err error) { + if app.conn != nil { + host := app.conn.host + port := app.conn.port + app.conn.stop() + app.conn.Close() + if app.conn.ticker != nil { + app.conn.ticker.Stop() + } + app.conn = nil + app.incoming <- SystemMessageEvent(fmt.Sprintf("disconnected from %s:%s: %v", host, port, err)) + } else { + app.incoming <- SystemMessageEvent(fmt.Sprintf("error: %v", err)) + } +} + +func (app *App) Run() { + for { + select { + case ev := <-app.outgoing: + _, dial := ev.(DialEvent) + if dial || app.conn != nil { + if err := ev.HandleOutgoing(app); err != nil { + app.Error(err) + continue + } + } else { + app.incoming <- SystemMessageEvent("not connected to any server") + } + case <-app.ctx.Done(): + return + } + } +} + func NewApp() *App { app := &App{} app.ctx, app.stop = context.WithCancel(context.Background()) @@ -117,25 +152,7 @@ func NewApp() *App { panic(err) } - go func() { - for { - select { - case ev := <-app.outgoing: - _, dial := ev.(DialEvent) - if dial || app.conn != nil { - if err := ev.HandleOutgoing(app); err != nil { - app.error <- err - return - } - } else { - app.incoming <- SystemMessageEvent("not connected to any server") - } - case <-app.ctx.Done(): - return - } - } - }() - + go app.Run() app.InitUI() return app diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..1a27d56 --- /dev/null +++ b/shell.nix @@ -0,0 +1,8 @@ +{ pkgs ? import {} }: + +pkgs.mkShell { + buildInputs = [ + pkgs.go + pkgs.gopls + ]; +}