2025-01-18 22:54:05 +00:00
package app
import (
2025-01-19 10:32:59 +00:00
"os"
2025-01-18 22:54:05 +00:00
"os/exec"
"git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/term"
"git.tilde.town/nbsp/welcome/ui"
)
2025-01-19 10:32:59 +00:00
type Shell struct {
loaded bool
}
2025-01-18 22:54:05 +00:00
2025-02-06 19:50:25 +00:00
func ( view * Shell ) Draw ( state * ui . State ) ( row int ) {
win := state . Window ( ) . New ( 0 , 0 , 80 , state . Window ( ) . Height )
_ , row = win . Wrap ( vaxis . Segment { Text : ` ─ ─ ─ ─ using the shell ─ ─ ─ ─
2025-01-18 22:54:05 +00:00
2025-02-06 19:50:25 +00:00
` , Style: vaxis.Style { Foreground: vaxis.IndexColor(5)}}, vaxis.Segment { Text: ` as mentioned before , interaction with stuff on tilde town is done primarily via the ` } ,
2025-01-18 22:54:05 +00:00
vaxis . Segment { Text : "terminal" , Style : vaxis . Style { Foreground : vaxis . IndexColor ( 3 ) } } ,
2025-02-06 19:50:25 +00:00
vaxis . Segment { Text : ` . good news , you ' ve been using the terminal this whole time ! it ' s the text application with which you ' ve been talking to the server so far .
2025-01-18 22:54:05 +00:00
2025-02-06 19:50:25 +00:00
the method of navigation between programs inside our shared computer , however , is called a ` } ,
2025-01-18 22:54:05 +00:00
vaxis . Segment { Text : "shell" , Style : vaxis . Style { Foreground : vaxis . IndexColor ( 3 ) } } ,
2025-02-06 19:50:25 +00:00
vaxis . Segment { Text : ` . it's an interface that takes in commands, like "open a text editor" or "show me my flower", in a language that doesn't take too long to get used to. the most popular shell is called ` } ,
2025-01-18 22:54:05 +00:00
vaxis . Segment { Text : "bash" , Style : vaxis . Style { Foreground : vaxis . IndexColor ( 3 ) } } ,
2025-02-06 19:50:25 +00:00
vaxis . Segment { Text : ` , and while there are more , there ' s no reason to switch unless you ' re looking for something different .
2025-01-18 22:54:05 +00:00
below is a terminal session . try typing ` } ,
vaxis . Segment { Text : "ls" , Style : vaxis . Style { Foreground : vaxis . IndexColor ( 3 ) } } ,
vaxis . Segment { Text : ` to list a folder, or ` } ,
vaxis . Segment { Text : "echo" , Style : vaxis . Style { Foreground : vaxis . IndexColor ( 3 ) } } ,
2025-02-06 19:50:25 +00:00
vaxis . Segment { Text : ` to make the shell say anything you want it to. run ` } ,
2025-01-18 22:54:05 +00:00
vaxis . Segment { Text : "exit" , Style : vaxis . Style { Foreground : vaxis . IndexColor ( 3 ) } } ,
2025-02-06 19:50:25 +00:00
vaxis . Segment { Text : ` to continue the tutorial, whenever you're ready. ` } ,
2025-01-18 22:54:05 +00:00
)
2025-02-06 19:50:25 +00:00
return
2025-01-18 22:54:05 +00:00
}
func ( view * Shell ) Event ( state * ui . State , event vaxis . Event ) ( processed bool ) {
2025-01-19 10:32:59 +00:00
if view . loaded {
return
}
view . loaded = true
2025-02-06 19:50:25 +00:00
row := view . Draw ( state )
win := state . Window ( ) . New ( 0 , row + 2 , 80 , 80 )
2025-01-18 22:54:05 +00:00
vt := term . New ( )
2025-01-19 10:32:59 +00:00
vt . TERM = os . Getenv ( "TERM" )
2025-01-18 22:54:05 +00:00
vt . Attach ( state . PostEvent ( ) )
vt . Focus ( )
2025-01-19 00:36:43 +00:00
err := vt . Start ( exec . Command ( "bash" , "-l" ) )
2025-01-18 22:54:05 +00:00
if err != nil {
panic ( err )
}
defer vt . Close ( )
for ev := range state . Events ( ) {
switch ev . ( type ) {
case term . EventClosed :
state . HideCursor ( )
2025-01-18 23:17:39 +00:00
vt . Detach ( )
vt . Close ( )
2025-01-18 22:54:05 +00:00
state . Window ( ) . Clear ( )
2025-01-18 23:47:22 +00:00
ui . ViewChange <- & Editor { }
2025-01-18 22:54:05 +00:00
return
case vaxis . Redraw :
view . Draw ( state )
2025-01-19 00:36:43 +00:00
vt . Draw ( win )
2025-01-18 22:54:05 +00:00
state . Render ( )
continue
}
2025-01-19 10:15:57 +00:00
vt . Update ( ev )
2025-01-18 22:54:05 +00:00
}
return
}