add STAT command and readme stuff

This commit is contained in:
Lobo 2025-10-12 13:39:47 -03:00
parent 4213730506
commit 484c3db565
5 changed files with 132 additions and 58 deletions

View file

@ -1,14 +1,32 @@
# nanite # nanite
it ony a [nanochat] client! `nanite` is a terminal [Nanochat] client.
wrote this while simultaneously learning how to use [vaxis] and it sure was an experience
## use it ## build
``` ```
$ go build . $ go build .
```
## usage
```
$ ./nanite
usage: ./nanite host [port]
$ ./nanite very.real-server.com $ ./nanite very.real-server.com
``` ```
[nanochat]: https://git.phial.org/d6/nanochat keybindings:
[vaxis]: https://git.sr.ht/~rockorager/vaxis
- `Ctrl+C`: quit
- `Ctrl+L`: refresh screen
- `Ctrl+P`: poll
commands:
- `/q`, `/quit`: quit
- `/nick [nickname]`: change nick, if no arguments, show current nick
- `/me [is listening to music]`: IRC `/me` alike
- `/poll [n]`: change polling interval, if no arguments, poll manually
[Nanochat]: https://git.phial.org/d6/nanochat

View file

@ -3,8 +3,26 @@ package main
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
) )
func (app *App) Stat() (res string, err error) {
if _, err := app.conn.Write([]byte("STAT\n")); err != nil {
return "", err
}
var str strings.Builder
for range 3 {
if !app.scanner.Scan() {
return "", app.scanner.Err()
}
str.Write(app.scanner.Bytes())
str.WriteRune(' ')
}
return str.String(), nil
}
func (app *App) Send(data string) (num int, err error) { func (app *App) Send(data string) (num int, err error) {
if _, err := fmt.Fprintf(app.conn, "SEND %s\n", data); err != nil { if _, err := fmt.Fprintf(app.conn, "SEND %s\n", data); err != nil {
return 0, err return 0, err
@ -37,29 +55,29 @@ func (app *App) Poll(since int) (num int, err error) {
return num, nil return num, nil
} }
func (app *App) Last(n int) (err error) { func (app *App) Last(n int) (num int, err error) {
if n == 0 { if n == 0 {
return nil return 0, nil
} }
if _, err := fmt.Fprintf(app.conn, "LAST %d\n", n); err != nil { if _, err := fmt.Fprintf(app.conn, "LAST %d\n", n); err != nil {
return err return 0, err
} }
var nsrv int var nsrv int
if !app.scanner.Scan() { if !app.scanner.Scan() {
return app.scanner.Err() return 0, app.scanner.Err()
} }
nsrvRaw := app.scanner.Text() nsrvRaw := app.scanner.Text()
nsrv, err = strconv.Atoi(nsrvRaw) nsrv, err = strconv.Atoi(nsrvRaw)
if err != nil { if err != nil {
return err return 0, err
} }
if nsrv != 0 { if nsrv != 0 {
for range nsrv { for range nsrv {
if !app.scanner.Scan() { if !app.scanner.Scan() {
return app.scanner.Err() return 0, app.scanner.Err()
} }
app.incoming <- Message(app.scanner.Text()) app.incoming <- Message(app.scanner.Text())
} }
@ -67,14 +85,14 @@ func (app *App) Last(n int) (err error) {
var last int var last int
if !app.scanner.Scan() { if !app.scanner.Scan() {
return app.scanner.Err() return 0, app.scanner.Err()
} }
lastRaw := app.scanner.Text() lastRaw := app.scanner.Text()
last, err = strconv.Atoi(lastRaw) last, err = strconv.Atoi(lastRaw)
if err != nil { if err != nil {
return err return 0, err
} }
app.incoming <- SetLast(last) app.incoming <- Last(last)
return nil return nsrv, nil
} }

View file

@ -43,50 +43,43 @@ func (app *App) Redraw() {
return return
} }
app.dirty = false app.dirty = false
// set window title and draw titlebar
app.w.title.Clear() app.w.title.Clear()
titlebarStyle := vaxis.Style{Attribute: vaxis.AttrBold} titlebarStyle := vaxis.Style{Attribute: vaxis.AttrBold}
delimiterStyle := vaxis.Style{Attribute: vaxis.AttrDim}
if app.conn != nil { if app.conn != nil {
app.vx.SetTitle(fmt.Sprintf("%s:%s", app.host, app.port)) titleString := fmt.Sprintf("%s:%s", app.host, app.port)
rateString := "n/a" app.vx.SetTitle(titleString)
rateString := "manual"
if app.rate != 0 { if app.rate != 0 {
rateString = app.rate.String() rateString = app.rate.String()
} }
app.w.title.PrintTruncate(0,
vaxis.Segment{
Text: "• ",
Style: titlebarStyle,
},
vaxis.Segment{
Text: app.host,
Style: titlebarStyle,
},
vaxis.Segment{
Text: ":",
Style: titlebarStyle,
},
vaxis.Segment{
Text: app.port,
Style: titlebarStyle,
},
vaxis.Segment{
Text: fmt.Sprintf(" |  %s", rateString),
},
)
// let the widgets draw themselves segments := []vaxis.Segment{
{Text: "• "},
{Text: titleString, Style: titlebarStyle},
{Text: " │ ", Style: delimiterStyle},
{Text: fmt.Sprintf("↻ %s", rateString)},
}
if app.stats != "" {
segments = append(segments,
vaxis.Segment{Text: " │ ", Style: delimiterStyle},
vaxis.Segment{Text: app.stats},
)
}
app.w.title.PrintTruncate(0, segments...)
app.pager.Layout() app.pager.Layout()
app.pager.Draw(app.w.log) app.pager.Draw(app.w.log)
app.input.Draw(app.w.input) app.input.Draw(app.w.input)
} else { } else {
app.vx.SetTitle("nanite (disconnected)") app.vx.SetTitle("nanite (disconnected)")
app.w.title.PrintTruncate(0, app.w.title.PrintTruncate(0,
vaxis.Segment{ vaxis.Segment{Text: "✕ "},
Text: "✖ (disconnected)", vaxis.Segment{Text: "disconnected", Style: titlebarStyle},
Style: titlebarStyle,
},
) )
} }
app.vx.Render() app.vx.Render()
@ -115,8 +108,7 @@ func (app *App) HandleTerminalEvent(ev vaxis.Event) {
case ev.MatchString("down"): case ev.MatchString("down"):
app.pager.ScrollDown() app.pager.ScrollDown()
case ev.MatchString("ctrl+p"): case ev.MatchString("ctrl+p"):
app.AppendSystemMessage("polling for new messages") app.outgoing <- ManualPoll(app.last)
app.outgoing <- Poll(app.last)
case ev.MatchString("ctrl+l"): case ev.MatchString("ctrl+l"):
app.Redraw() app.Redraw()
app.vx.Refresh() app.vx.Refresh()
@ -136,6 +128,7 @@ func (app *App) HandleTerminalEvent(ev vaxis.Event) {
message := fmt.Sprintf("%s: %s", app.nick, app.input.String()) message := fmt.Sprintf("%s: %s", app.nick, app.input.String())
app.AppendMessage(message) app.AppendMessage(message)
app.outgoing <- Message(message) app.outgoing <- Message(message)
app.outgoing <- Stat("")
} }
app.input.SetContent("") app.input.SetContent("")

View file

@ -18,7 +18,7 @@ func (m Message) HandleOutgoing(app *App) error {
if err != nil { if err != nil {
return err return err
} }
app.incoming <- SetLast(num) app.incoming <- Last(num)
return nil return nil
} }
@ -30,15 +30,59 @@ func (p Poll) HandleOutgoing(app *App) error {
if err != nil { if err != nil {
return err return err
} }
err = app.Last(num) num, err = app.Last(num)
if err != nil { if err != nil {
return err return err
} }
if num != 0 {
app.outgoing <- Stat("")
}
return nil return nil
} }
type SetLast int type ManualPoll int
func (m SetLast) HandleIncoming(app *App) { func (p ManualPoll) HandleOutgoing(app *App) error {
num, err := app.Poll(int(p))
if err != nil {
return err
}
app.incoming <- ManualPoll(num)
num, err = app.Last(num)
if err != nil {
return err
}
if num != 0 {
app.outgoing <- Stat("")
}
return nil
}
func (p ManualPoll) HandleIncoming(app *App) {
if int(p) == 0 {
app.AppendSystemMessage("poll: no new messages")
} else {
app.AppendSystemMessage("poll: retrieving %d messages", p)
}
}
type Last int
func (m Last) HandleIncoming(app *App) {
app.last = int(m) app.last = int(m)
} }
type Stat string
func (data Stat) HandleIncoming(app *App) {
app.stats = string(data)
app.dirty = true
}
func (_ Stat) HandleOutgoing(app *App) error {
res, err := app.Stat()
if err != nil {
return err
}
app.incoming <- Stat(res)
return nil
}

15
main.go
View file

@ -26,30 +26,29 @@ var CommandMap = map[string]func(*App, string){
args := strings.Fields(rest) args := strings.Fields(rest)
switch len(args) { switch len(args) {
case 0: case 0:
app.AppendSystemMessage("your nickname is %s", app.nick) app.AppendSystemMessage("nick: your nickname is %s", app.nick)
default: default:
app.SetNick(args[0]) app.SetNick(args[0])
app.AppendSystemMessage("your nickname is now %s", app.nick) app.AppendSystemMessage("nick: your nickname is now %s", app.nick)
} }
}, },
"poll": func(app *App, rest string) { "poll": func(app *App, rest string) {
if rest == "" { if rest == "" {
app.AppendSystemMessage("polling for new messages") app.outgoing <- ManualPoll(app.last)
app.outgoing <- Poll(app.last)
} else { } else {
num, err := strconv.Atoi(rest) num, err := strconv.Atoi(rest)
if err != nil { if err != nil {
app.AppendSystemMessage("invalid number %s", rest) app.AppendSystemMessage("poll: invalid number %s", rest)
} else { } else {
if num == 0 { if num == 0 {
app.ticker.Stop() app.ticker.Stop()
app.rate = 0 app.rate = 0
app.AppendSystemMessage("disabled automatic polling") app.AppendSystemMessage("poll: disabled automatic polling")
} else { } else {
app.rate = time.Second * time.Duration(num) app.rate = time.Second * time.Duration(num)
app.ticker.Stop() app.ticker.Stop()
app.ticker = time.NewTicker(app.rate) app.ticker = time.NewTicker(app.rate)
app.AppendSystemMessage("polling every %s", app.rate.String()) app.AppendSystemMessage("poll: polling every %s", app.rate.String())
} }
} }
} }
@ -123,7 +122,9 @@ func (app *App) Connect(host, port string) (err error) {
app.ticker = time.NewTicker(delta) app.ticker = time.NewTicker(delta)
go func() { go func() {
app.outgoing <- Stat("")
app.Last(20) app.Last(20)
for { for {
select { select {
case ev := <-app.outgoing: case ev := <-app.outgoing: