add scripts
This commit is contained in:
parent
3412dd7900
commit
70d95cdcce
4 changed files with 81 additions and 8 deletions
23
command.go
23
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"]
|
||||
}
|
||||
|
|
|
|||
11
event.go
11
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
|
||||
}
|
||||
|
|
|
|||
7
main.go
7
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 {
|
||||
|
|
|
|||
48
script.go
Normal file
48
script.go
Normal file
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue