From 70d95cdcce0691e189720e371d8680d6bb71367d Mon Sep 17 00:00:00 2001 From: Lobo Date: Fri, 24 Oct 2025 11:59:29 -0300 Subject: [PATCH] add scripts --- command.go | 23 +++++++++++++++++++---- event.go | 11 ++++++++--- main.go | 7 ++++++- script.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 script.go diff --git a/command.go b/command.go index d145aa7..727285c 100644 --- a/command.go +++ b/command.go @@ -26,6 +26,20 @@ func initCommandMap() { } app.AppendSystemMessage("commands: %s", s.String()) }, + "script": func(app *App, rest string) { + if rest == "" { + var s strings.Builder + for _, script := range app.scripts { + s.WriteString(script) + s.WriteRune(' ') + } + app.AppendSystemMessage("scripts: %s", s.String()) + } else { + if err := app.LoadScript(rest); err != nil { + app.AppendSystemMessage("error loading script `%s`: %s", rest, err) + } + } + }, "send": func(app *App, rest string) { app.AppendMessage(rest) app.outgoing <- MessageEvent(rest) @@ -51,7 +65,7 @@ func initCommandMap() { } else { app.SetNick(rest) app.AppendSystemMessage("nick: your nickname is now %s", app.nick) - if err := os.WriteFile(path.Join(app.cfgHome, "nick"), []byte(rest), 0o700); err != nil { + if err := os.WriteFile(path.Join(app.cfgHome, "nick"), []byte(rest), 0o600); err != nil { app.AppendSystemMessage("nick: failed to persist nickname: %s", err) } } @@ -90,8 +104,9 @@ func initCommandMap() { "quit": func(app *App, rest string) { app.stop() }, - "q": func(app *App, rest string) { - app.stop() - }, } + + // aliases + CommandMap["q"] = CommandMap["quit"] + CommandMap["."] = CommandMap["script"] } diff --git a/event.go b/event.go index 70c744d..358d5fa 100644 --- a/event.go +++ b/event.go @@ -11,6 +11,7 @@ import ( type IncomingEvent interface { HandleIncoming(*App) } + type OutgoingEvent interface { HandleOutgoing(*App) error } @@ -77,9 +78,13 @@ func (ev DialEvent) HandleOutgoing(app *App) error { return nil } -type HangupEvent struct{} +type HangupEvent struct{ host, port string } -func (ev HangupEvent) HandleOutgoing(app *App) error { +func (ev HangupEvent) HandleIncoming(app *App) { + app.AppendSystemMessage("disconnected from %s:%s", ev.host, ev.port) +} + +func (_ HangupEvent) HandleOutgoing(app *App) error { if app.conn == nil { app.incoming <- SystemMessageEvent("not connected to any server") return nil @@ -94,7 +99,7 @@ func (ev HangupEvent) HandleOutgoing(app *App) error { app.conn.ticker.Stop() app.conn.ticker = nil app.conn = nil - app.incoming <- SystemMessageEvent(fmt.Sprintf("disconnected from %s:%s", host, port)) + app.incoming <- HangupEvent{host, port} return nil } diff --git a/main.go b/main.go index 3422bc4..5197cd0 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,8 @@ type App struct { outgoing chan OutgoingEvent error chan error + scripts []string + vx *vaxis.Vaxis pager *pager.Model input *textinput.Model @@ -61,7 +63,6 @@ func (app *App) AppendMessage(data string) { vaxis.Segment{Text: data, Style: style}, vaxis.Segment{Text: "\n"}, ) - app.pager.Offset = math.MaxInt } @@ -112,6 +113,10 @@ func NewApp() *App { panic(err) } + if err := app.RefreshScripts(); err != nil { + panic(err) + } + go func() { for { select { diff --git a/script.go b/script.go new file mode 100644 index 0000000..039838c --- /dev/null +++ b/script.go @@ -0,0 +1,48 @@ +package main + +import ( + "errors" + "io/fs" + "os" + "path" + "path/filepath" + "strings" +) + +func (app *App) RefreshScripts() error { + scriptDir := path.Join(app.cfgHome, "scripts") + if _, err := os.Stat(scriptDir); errors.Is(err, fs.ErrNotExist) { + return nil + } + + return filepath.Walk(scriptDir, func(p string, i os.FileInfo, err error) error { + if err != nil { + return err + } + if i.IsDir() { + return nil + } + app.scripts = append(app.scripts, path.Base(p)) + return nil + }) +} + +func (app *App) LoadScript(name string) error { + scriptPath := path.Join(app.cfgHome, "scripts", name) + data, err := os.ReadFile(scriptPath) + if err != nil { + return err + } + + for line := range strings.Lines(string(data)) { + app.AppendSystemMessage("/%s", line) + name, rest, _ := strings.Cut(line, " ") + if cmd, ok := CommandMap[name]; ok { + cmd(app, rest) + } else { + app.AppendSystemMessage("unknown command \"%s\"", name) + } + } + + return nil +}