master window.go
  1package senpai
  2
  3import (
  4	"fmt"
  5	"strconv"
  6	"strings"
  7	"time"
  8
  9	"git.sr.ht/~delthas/senpai/ui"
 10)
 11
 12func (app *App) initWindow() {
 13	version, ok := BuildVersion()
 14	if !ok {
 15		version = "(unknown version)"
 16	}
 17	app.win.AddBuffer("", "(home)", "")
 18	app.win.AddLine("", "", ui.Line{
 19		Head: ui.PlainString("--"),
 20		Body: ui.PlainString(fmt.Sprintf("Welcome to senpai %v! To get started, use the Help buttons, or enter /help for a list of commands.", version)),
 21		At:   time.Now(),
 22	})
 23}
 24
 25type statusLine struct {
 26	netID string
 27	line  ui.Line
 28}
 29
 30func (app *App) queueStatusLine(netID string, line ui.Line) {
 31	if line.At.IsZero() {
 32		line.At = time.Now()
 33	}
 34	app.postEvent(event{
 35		src: "*",
 36		content: statusLine{
 37			netID: netID,
 38			line:  line,
 39		},
 40	})
 41}
 42
 43func (app *App) addStatusLine(netID string, line ui.Line) {
 44	currentNetID, buffer := app.win.CurrentBuffer()
 45	if currentNetID == netID && buffer != "" {
 46		app.win.AddLine(netID, buffer, line)
 47	}
 48	app.win.AddLine(netID, "", line)
 49}
 50
 51func (app *App) setStatus() {
 52	if app.uploadingProgress != nil {
 53		app.win.SetStatus(fmt.Sprintf("Uploading file (%02.1f%%)...", *app.uploadingProgress*100))
 54		return
 55	}
 56
 57	netID, buffer := app.win.CurrentBuffer()
 58	s := app.sessions[netID]
 59	if s == nil {
 60		return
 61	}
 62	status := ""
 63	if app.cfg.ShowTypings {
 64		ts := s.Typings(buffer)
 65		if 3 < len(ts) {
 66			status = "several people are typing..."
 67		} else {
 68			verb := " is typing..."
 69			if 1 < len(ts) {
 70				verb = " are typing..."
 71				status = strings.Join(ts[:len(ts)-1], ", ") + " and "
 72			}
 73			if 0 < len(ts) {
 74				status += ts[len(ts)-1] + verb
 75			}
 76		}
 77	}
 78	app.win.SetStatus(status)
 79}
 80
 81func (app *App) setBufferNumbers() {
 82	input := app.win.InputContent()
 83	if !isCommand(input) {
 84		app.win.FilterBuffers(false, "")
 85		return
 86	}
 87	cmd, arg, _ := strings.Cut(string(input[1:]), " ")
 88	if cmd == "" || !strings.HasPrefix("buffer", cmd) {
 89		app.win.FilterBuffers(false, "")
 90		return
 91	}
 92	if _, err := strconv.Atoi(arg); err == nil {
 93		// Do not filter buffers if we are passing a buffer index
 94		arg = ""
 95	}
 96	app.win.FilterBuffers(true, arg)
 97}
 98
 99func (app *App) clearBufferCommand() {
100	input := app.win.InputContent()
101	if !isCommand(input) {
102		return
103	}
104	cmd, _, _ := strings.Cut(string(input[1:]), " ")
105	if cmd == "" || !strings.HasPrefix("buffer", cmd) {
106		return
107	}
108	app.win.InputClear()
109}