unfuck input collection

pull/1/head
vilmibm 2023-02-13 06:13:07 +00:00
parent 462e8772ec
commit 45dd8efbae
1 changed files with 34 additions and 62 deletions

View File

@ -1,19 +1,18 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"path" "path"
"strings" "strings"
"time" "time"
"encoding/json"
"github.com/MakeNowJust/heredoc/v2" "github.com/MakeNowJust/heredoc/v2"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
"gopkg.in/yaml.v3"
) )
const ( const (
@ -25,17 +24,13 @@ const (
type scene struct { type scene struct {
Name string Name string
Description string Description string
InBuff io.ReadWriter Key string
InLength int
Host *character Host *character
} }
type townApplication struct { type townApplication struct {
Email io.ReadWriter When time.Time
How io.ReadWriter Answers map[string]string
Why io.ReadWriter
Where io.ReadWriter
Extra io.ReadWriter
} }
type character struct { type character struct {
@ -70,14 +65,15 @@ func main() {
} }
logger := log.New(logF, "", log.Ldate|log.Ltime) logger := log.New(logF, "", log.Ldate|log.Ltime)
err = cli(logger) err = _main(logger)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(2) os.Exit(2)
} }
} }
func cli(l *log.Logger) error { func _main(l *log.Logger) error {
l.Println("starting a session")
pages := tview.NewPages() pages := tview.NewPages()
mainFlex := tview.NewFlex() mainFlex := tview.NewFlex()
innerFlex := tview.NewFlex() innerFlex := tview.NewFlex()
@ -130,43 +126,20 @@ func cli(l *log.Logger) error {
player := newCharacter("you", "TODO") player := newCharacter("you", "TODO")
answers := &townApplication{ townApp := &townApplication{
Email: bytes.NewBuffer([]byte{}), Answers: map[string]string{},
How: bytes.NewBuffer([]byte{}),
Why: bytes.NewBuffer([]byte{}),
Where: bytes.NewBuffer([]byte{}),
Extra: bytes.NewBuffer([]byte{}),
} }
save := func() { save := func() {
when := time.Now() townApp.When = time.Now()
// TODO this code sucks output, err := json.Marshal(townApp)
email := []byte{}
how := []byte{}
why := []byte{}
where := []byte{}
extra := []byte{}
_, _ = answers.Email.Read(email)
_, _ = answers.Why.Read(why)
_, _ = answers.How.Read(how)
_, _ = answers.Where.Read(where)
_, _ = answers.Extra.Read(extra)
d := map[string]interface{}{
"when": when,
"email": email,
"how": how,
"why": why,
"where": where,
"extra": extra,
}
output, err := yaml.Marshal(d)
if err != nil { if err != nil {
l.Printf("could not serialize stuff: %s", err.Error()) l.Printf("could not serialize stuff: %s", err.Error())
l.Printf("dumping values: %v", d) l.Printf("dumping values: %v", townApp)
return return
} }
f, err := os.Create(path.Join(signupDirectory, fmt.Sprintf("%d", when.Unix()))) f, err := os.Create(path.Join(signupDirectory, fmt.Sprintf("%d.json", townApp.When.Unix())))
if err != nil { if err != nil {
l.Printf("could not open signup file: %s", err.Error()) l.Printf("could not open signup file: %s", err.Error())
l.Printf("dumping values: %s", string(output)) l.Printf("dumping values: %s", string(output))
@ -199,10 +172,11 @@ func cli(l *log.Logger) error {
hello, welcome to the application for membership in tilde town. hello, welcome to the application for membership in tilde town.
first, please let me know what a good [-:-:b]email address[-:-:-] is for you? first, please let me know what a good [-:-:b]email address[-:-:-] is for you?
just say it out loud. as many times as you need. to get it right. just say it out loud. as many times as you need. to get it right.
when you're ready to move on, [-:-:b]/nod[-:-:-] when you're ready to move on, [-:-:b]/nod[-:-:-]
`), `),
Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"), Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
InBuff: answers.Email, Key: "email",
}, },
{ {
Name: "nodded", Name: "nodded",
@ -224,8 +198,8 @@ func cli(l *log.Logger) error {
just say your answer out loud. when you've said what you want, [-:-:b]/lean[-:-:-] just say your answer out loud. when you've said what you want, [-:-:b]/lean[-:-:-]
against a tree. against a tree.
`), `),
InBuff: answers.How, Key: "how",
Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."), Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."),
}, },
{ {
Name: "leaned", Name: "leaned",
@ -243,8 +217,8 @@ func cli(l *log.Logger) error {
as usual, just say your answer. when you're satisfied, please [-:-:b]/spin[-:-:-] as usual, just say your answer. when you're satisfied, please [-:-:b]/spin[-:-:-]
around in this void. around in this void.
`), `),
InBuff: answers.Why, Key: "why",
Host: newCharacter("the vcr", "a black and grey VCR from 1991"), Host: newCharacter("the vcr", "a black and grey VCR from 1991"),
}, },
{ {
Name: "spun", Name: "spun",
@ -267,8 +241,8 @@ func cli(l *log.Logger) error {
when you're happy you can submit this whole experience by leaving the when you're happy you can submit this whole experience by leaving the
store. just [-:-:b]/open[-:-:-] the door. store. just [-:-:b]/open[-:-:-] the door.
`), `),
InBuff: answers.Where, Key: "where",
Host: newCharacter("the mop", "a greying mop with a wooden handle."), Host: newCharacter("the mop", "a greying mop with a wooden handle."),
}, },
{ {
Name: "done", Name: "done",
@ -281,7 +255,7 @@ func cli(l *log.Logger) error {
ok bye have a good one~ ok bye have a good one~
`), `),
InBuff: answers.Extra, Key: "extra",
}, },
} }
@ -292,11 +266,10 @@ func cli(l *log.Logger) error {
if currentScene.Name != fromScene { if currentScene.Name != fromScene {
return return
} }
if currentScene.InLength == 0 { if len(townApp.Answers[currentScene.Key]) == 0 {
fmt.Fprintln(msgScroll, currentScene.Host.Say(sorryMsg)) fmt.Fprintln(msgScroll, currentScene.Host.Say(sorryMsg))
return return
} }
// TODO put contents of InBuff into answers struct
sceneIx++ sceneIx++
currentScene = scenes[sceneIx] currentScene = scenes[sceneIx]
fmt.Fprintln(msgScroll, heredoc.Doc(` fmt.Fprintln(msgScroll, heredoc.Doc(`
@ -308,22 +281,22 @@ func cli(l *log.Logger) error {
} }
handleInput := func(msg string) { handleInput := func(msg string) {
trimmed := strings.TrimSpace(msg) msg = strings.TrimSpace(msg)
if trimmed == "" { if msg == "" {
return return
} }
if strings.HasPrefix(trimmed, "/") { if strings.HasPrefix(msg, "/") {
split := strings.Split(trimmed, " ") split := strings.Split(msg, " ")
if len(split) > 0 { if len(split) > 0 {
trimmed = split[0] msg = split[0]
} }
switch strings.TrimPrefix(trimmed, "/") { switch strings.TrimPrefix(msg, "/") {
case "quit": case "quit":
l.Println("got /quit")
app.Stop() app.Stop()
case "look": case "look":
fmt.Fprintln(msgScroll, "") fmt.Fprintln(msgScroll, "")
fmt.Fprintln(msgScroll, currentScene.Description) fmt.Fprintln(msgScroll, currentScene.Description)
// TODO refactor into a state machine
case "nod": case "nod":
advanceScene("start", advanceScene("start",
"i'm sorry, before going further could you share an email with me?") "i'm sorry, before going further could you share an email with me?")
@ -337,14 +310,13 @@ func cli(l *log.Logger) error {
} }
return return
} }
if currentScene.InLength+len(msg) > maxInputLength { if len(townApp.Answers[currentScene.Key]) > maxInputLength {
fmt.Fprintln(msgScroll, fmt.Fprintln(msgScroll,
currentScene.Host.Say("sorry I've heard more than I can remember :( maybe it's time to move on")) currentScene.Host.Say("sorry I've heard more than I can remember :( maybe it's time to move on"))
return return
} }
fmt.Fprintln(msgScroll, player.Say(msg)) fmt.Fprintln(msgScroll, player.Say(msg))
fmt.Fprintln(currentScene.InBuff, msg) townApp.Answers[currentScene.Key] += ("\n" + msg)
currentScene.InLength += len(msg)
} }
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {