1
0
forked from nbsp/welcome
welcome/app/editor.go
2025-04-03 05:04:46 +00:00

94 lines
2.5 KiB
Go

package app
import (
"fmt"
"os"
"os/exec"
"os/user"
"git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/term"
"git.tilde.town/nbsp/welcome/ui"
)
type Editor struct {
loaded bool
}
func (view *Editor) Draw(state *ui.State) (row int) {
win := state.Window().New(0, 0, 80, state.Window().Height)
user, _ := user.Current()
_, row = win.Wrap(
vaxis.Segment{Text: `──── editing your homepage ────
`, Style: vaxis.Style{Foreground: vaxis.IndexColor(5)}}, vaxis.Segment{Text: `yeah buddy! Welcome to shell. 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 barren, doesn't it? Let's edit it to add some more words.
Type `},
vaxis.Segment{Text: "micro public_html/index.html", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` and hit Enter to open micro, our text editor of choice. In it, make some changes, and press `},
vaxis.Segment{Text: "Ctrl+S", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` to save. Refresh your homepage in your browser to see your changes. Press `},
vaxis.Segment{Text: "Ctrl+Q", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` to save and exit micro.
Like before, when you're done, type `},
vaxis.Segment{Text: "exit", Style: vaxis.Style{Foreground: vaxis.IndexColor(3)}},
vaxis.Segment{Text: ` in your shell and hit Enter to continue.`},
)
return
}
func (view *Editor) Event(state *ui.State, event vaxis.Event) (processed bool) {
if view.loaded {
return
}
view.loaded = true
row := view.Draw(state)
win := state.Window().New(0, row+2, 80, 80)
vt := term.New()
vt.TERM = os.Getenv("TERM")
vt.Attach(state.PostEvent())
vt.Focus()
err := vt.Start(exec.Command("bash", "-l"))
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)
vt.Draw(win)
state.Render()
continue
}
vt.Update(ev)
}
return
}