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: `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.`}, ) 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 }