welcome/app/editor.go

97 lines
2.4 KiB
Go
Raw Normal View History

2025-01-18 23:47:22 +00:00
package app
import (
"fmt"
2025-01-19 10:32:59 +00:00
"os"
2025-01-18 23:47:22 +00:00
"os/exec"
"os/user"
"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 Editor struct {
loaded bool
}
2025-01-18 23:47:22 +00:00
func (view *Editor) Draw(state *ui.State) {
win := state.Window()
user, _ := user.Current()
win.Print(
vaxis.Segment{Text: ` editing your homepage
nice work! you'll get used to the shell in no time. one of the main things
you're going to do in town is editing text files. the program we use to do this
is aptly called an `},
vaxis.Segment{Text: "editor", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: `.
take your homepage, for example:
`},
vaxis.Segment{Text: fmt.Sprintf("https://tilde.town/~%s", user.Username),
Style: vaxis.Style{
Foreground: vaxis.IndexColor(4),
UnderlineStyle: vaxis.UnderlineSingle,
},
},
vaxis.Segment{Text: `
looks pretty barren, doesn't it? let's edit it to add some more words.
type `},
vaxis.Segment{Text: "nano public_html/index.html", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` to open nano, our text editor of choice. in it,
make some changes, and press `},
vaxis.Segment{Text: "Ctrl+O", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` to save and look at the changes in your
browser. press `},
vaxis.Segment{Text: "Ctrl+X", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` to save and exit nano.
like before, when you're done, type `},
vaxis.Segment{Text: "exit", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` in your shell to continue.`},
)
}
func (view *Editor) Event(state *ui.State, event vaxis.Event) (processed bool) {
2025-01-19 10:32:59 +00:00
if view.loaded {
return
}
view.loaded = true
win := state.Window().New(0, 16, 80, 80)
2025-01-18 23:47:22 +00:00
view.Draw(state)
vt := term.New()
2025-01-19 10:32:59 +00:00
vt.TERM = os.Getenv("TERM")
2025-01-18 23:47:22 +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 23:47:22 +00:00
if err != nil {
panic(err)
}
defer vt.Close()
for ev := range state.Events() {
switch ev.(type) {
case term.EventClosed:
state.HideCursor()
vt.Detach()
vt.Close()
state.Window().Clear()
ui.ViewChange <- &Help{}
return
case vaxis.Redraw:
view.Draw(state)
2025-01-19 10:32:59 +00:00
vt.Draw(win)
2025-01-18 23:47:22 +00:00
state.Render()
continue
}
2025-01-19 10:15:57 +00:00
vt.Update(ev)
2025-01-18 23:47:22 +00:00
}
return
}