2025-01-18 22:06:25 +00:00
package app
import (
2025-01-19 12:01:37 +00:00
"fmt"
2025-02-21 18:23:13 +00:00
"math"
2025-01-19 12:01:37 +00:00
"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" :
2025-02-21 18:23:13 +00:00
if view . index < 4 {
2025-01-18 22:06:25 +00:00
view . index ++
}
case "Enter" :
2025-02-21 18:23:13 +00:00
if view . index == 4 {
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
}
2025-02-21 18:23:13 +00:00
if view . index < 4 {
2025-01-18 22:06:25 +00:00
view . index ++
}
default :
2025-02-21 18:23:13 +00:00
if view . index != 4 && view . index != 0 {
view . inputs [ view . index - 1 ] . Update ( event )
2025-01-18 22:06:25 +00:00
}
}
processed = true
}
enterStyle := vaxis . AttributeMask ( vaxis . AttrNone )
2025-02-21 18:23:13 +00:00
if view . index == 4 {
2025-01-18 22:06:25 +00:00
enterStyle = vaxis . AttrReverse
}
2025-02-06 19:50:25 +00:00
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
2025-02-06 19:50: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
2025-02-06 19:50: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
2025-02-06 19:50:25 +00:00
> ` } )
_ , q2 := win . New ( 0 , q1 , 80 , state . Window ( ) . Height ) . Wrap ( vaxis . Segment { Text : `
2025-01-18 22:06:25 +00:00
2025-02-06 19:50: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
2025-02-06 19:50: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
2025-02-06 19:50: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 : `
2025-02-06 19:50:25 +00:00
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 } } ,
)
2025-02-21 18:23:13 +00:00
if q1 + q2 + q3 < win . Height {
if view . index == 0 {
view . index ++
}
rows := [ 3 ] int { q1 , q1 + q2 , q1 + q2 + q3 - 2 }
for i , input := range view . inputs {
input . Draw ( vaxis . Window { win . Vx , & win , 5 , rows [ i ] , int ( math . Min ( 73.0 , float64 ( win . Width ) - 7 ) ) , 1 } )
if i == view . index - 1 {
win . SetStyle ( 3 , rows [ i ] , vaxis . Style { Attribute : vaxis . AttrReverse } )
}
}
} else {
win . Clear ( )
switch view . index {
case 0 :
win . Wrap ( vaxis . Segment { Text : ` ─ ─ ─ ─ initial settings ─ ─ ─ ─
` , 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 . ` } )
case 1 :
_ , h := win . Wrap ( vaxis . Segment { Text : ` 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 .
> ` } )
view . inputs [ 0 ] . Draw ( vaxis . Window { win . Vx , & win , 5 , h , int ( math . Min ( 73.0 , float64 ( win . Width ) - 7 ) ) , 1 } )
win . SetStyle ( 3 , h , vaxis . Style { Attribute : vaxis . AttrReverse } )
case 2 :
_ , h := win . Wrap ( vaxis . Segment { Text : ` 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 .
> ` } )
view . inputs [ 1 ] . Draw ( vaxis . Window { win . Vx , & win , 5 , h , int ( math . Min ( 73.0 , float64 ( win . Width ) - 7 ) ) , 1 } )
win . SetStyle ( 3 , h , vaxis . Style { Attribute : vaxis . AttrReverse } )
case 3 :
_ , h := win . Wrap ( vaxis . Segment { Text : ` 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 :
` } ,
vaxis . Segment {
Text : "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List" ,
Style : vaxis . Style {
Foreground : vaxis . IndexColor ( 4 ) ,
UnderlineStyle : vaxis . UnderlineSingle ,
} ,
} ,
vaxis . Segment { Text : `
it should look something like America / New_York .
> ` } )
view . inputs [ 2 ] . Draw ( vaxis . Window { win . Vx , & win , 5 , h , int ( math . Min ( 73.0 , float64 ( win . Width ) - 7 ) ) , 1 } )
win . SetStyle ( 3 , h , vaxis . Style { Attribute : vaxis . AttrReverse } )
case 4 :
win . Wrap ( vaxis . Segment { Text : " press ↲ to continue " , Style : vaxis . Style { Attribute : enterStyle } } )
2025-01-18 22:06:25 +00:00
}
}
2025-02-21 18:23:13 +00:00
2025-01-18 22:06:25 +00:00
state . HideCursor ( )
return
}