welcome/app/settings.go

114 lines
3.5 KiB
Go
Raw Normal View History

2025-01-18 22:06:25 +00:00
package app
import (
2025-01-19 12:01:37 +00:00
"fmt"
"os"
"strings"
2025-01-18 22:06:25 +00:00
"git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/textinput"
"git.tilde.town/nbsp/welcome/ui"
)
type Settings struct {
index int
inputs [3]textinput.Model
}
func (view *Settings) Event(state *ui.State, event vaxis.Event) (processed bool) {
if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
switch key.String() {
case "Ctrl+c", "Ctrl+d":
close(ui.Quit)
case "Up":
if view.index > 0 {
view.index--
}
case "Down":
if view.index < 3 {
view.index++
}
case "Enter":
if view.index == 3 {
2025-01-19 12:01:37 +00:00
pronouns := strings.TrimSpace(view.inputs[0].String())
birthday := strings.TrimSpace(view.inputs[1].String())
timezone := strings.TrimSpace(view.inputs[2].String())
// XXX: this assumes nothing will break
if pronouns != "" {
2025-01-19 13:29:07 +00:00
os.WriteFile(".pronouns", []byte(pronouns+"\n"), 0644)
2025-01-19 12:01:37 +00:00
}
if birthday != "" {
2025-01-19 13:29:07 +00:00
os.WriteFile(".birthday", []byte(birthday+"\n"), 0644)
2025-01-19 12:01:37 +00:00
}
if timezone != "" {
f, _ := os.OpenFile(".bashrc", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f.Write([]byte(fmt.Sprintf("export TZ='%s'\n", timezone)))
f.Close()
}
2025-01-18 22:54:05 +00:00
ui.ViewChange <- &Shell{}
2025-01-18 22:06:25 +00:00
}
if view.index < 3 {
view.index++
}
default:
if view.index != 3 {
view.inputs[view.index].Update(event)
}
}
processed = true
}
enterStyle := vaxis.AttributeMask(vaxis.AttrNone)
if view.index == 3 {
enterStyle = vaxis.AttrReverse
}
win := state.Window().New(0, 0, 80, state.Window().Height)
_, q1 := win.Wrap(vaxis.Segment{Text: ` initial settings
2025-01-18 22:06:25 +00:00
`, Style: vaxis.Style{Foreground: vaxis.IndexColor(5)}}, vaxis.Segment{Text: `let's get you set up with some useful settings. you can press or to move between the inputs. feel free to leave any of them empty! you can always set them later, or leave them unset.
2025-01-18 22:06:25 +00:00
1. `}, vaxis.Segment{Text: "what are your pronouns?", Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}}, vaxis.Segment{Text: ` this is useful information for folks to be able to refer to you in the third person. common pronoun sets are he/him, she/her, and they/them.
2025-01-18 22:06:25 +00:00
>`})
_, q2 := win.New(0, q1, 80, state.Window().Height).Wrap(vaxis.Segment{Text: `
2025-01-18 22:06:25 +00:00
2. `}, vaxis.Segment{Text: "when's your birthday?", Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}}, vaxis.Segment{Text: ` this will show up in certain places, so people can wish you a happy birthday :) format is MM/DD.
2025-01-18 22:06:25 +00:00
>`})
_, q3 := win.New(0, q1+q2, 80, state.Window().Height).Wrap(vaxis.Segment{Text: `
2025-01-18 22:06:25 +00:00
3. `}, vaxis.Segment{Text: "what's your timezone?", Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}}, vaxis.Segment{Text: ` this will change all times you view in town to your local timezone, so you don't have to convert from utc in your head. the format for timezones can be found here:
`},
2025-01-18 22:06:25 +00:00
vaxis.Segment{
Text: "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List",
Style: vaxis.Style{
2025-01-18 23:47:22 +00:00
Foreground: vaxis.IndexColor(4),
2025-01-18 22:06:25 +00:00
UnderlineStyle: vaxis.UnderlineSingle,
},
},
vaxis.Segment{Text: `
it should look something like America/New_York.
2025-01-18 22:06:25 +00:00
>
`},
vaxis.Segment{Text: " press ↲ to continue ", Style: vaxis.Style{Attribute: enterStyle}},
)
rows := [3]int{q1, q1 + q2, q1 + q2 + q3 - 2}
2025-01-18 22:06:25 +00:00
for i, input := range view.inputs {
input.Draw(vaxis.Window{win.Vx, &win, 5, rows[i], 74, 1})
if i == view.index {
win.SetStyle(3, rows[i], vaxis.Style{Attribute: vaxis.AttrReverse})
}
}
state.HideCursor()
return
}