Fullscreen mode via altscreen + fullscreen example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent c1b4d6a · Browse files · 4 files·+78 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+78 added−17 removed
- other:+78 added−17 removed
Provenance
- Commit
4f42c50- Tree
96ca153- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
c1b4d6a
4 files changed+78 added−17 removed
added:examples/fullscreen/main.go+59 added
1+package main2+3+// A simple program that counts down from 5 and then exits.4+5+import (6+ "fmt"7+ "log"8+ "tea"9+ "time"10+)11+12+type model int13+14+type tickMsg struct{}15+16+func main() {17+ tea.Fullscreen()18+ defer tea.ExitFullscreen()19+ err := tea.NewProgram(model(5), update, view, []tea.Sub{tick}).Start()20+ if err != nil {21+ log.Fatal(err)22+ }23+}24+25+func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {26+ m, _ := mdl.(model)27+28+ switch message := msg.(type) {29+30+ case tea.KeyPressMsg:31+ switch message {32+ case "ctrl+c":33+ fallthrough34+ case "esc":35+ fallthrough36+ case "q":37+ return m, tea.Quit38+ }39+40+ case tickMsg:41+ m -= 142+ if m <= 0 {43+ return m, tea.Quit44+ }45+46+ }47+48+ return m, nil49+}50+51+func view(mdl tea.Model) string {52+ m, _ := mdl.(model)53+ return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)54+}55+56+func tick(_ tea.Model) tea.Msg {57+ time.Sleep(time.Second)58+ return tickMsg{}59+}modified:go.mod+1 added-1 removed
3 unmodified lines4 5 require (6 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf17767 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e9428- github.com/tj/go-terminput v1.0.08+ golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 // indirect9 )modified:go.sum+2 added-11 removed
1 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3-github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=4-github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=3 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=4 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=7-github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=8-github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=9-github.com/tj/go-terminput v1.0.0 h1:X928u7ohxr7Dfzlxppvc5Zql5Lwd1bOH5VyIL2RPrFw=10-github.com/tj/go-terminput v1.0.0/go.mod h1:8zzAs+cqdjZlTxE9DbGyjKDfGNxaJjC5bvg4vkWX2V0=11-golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=12-golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=13-golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=14-golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=15-golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=5+golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=6+golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=modified:tea.go+16 added-5 removed
85 unmodified lines86 // Render initial view87 hideCursor()88 p.render(model)89 90- // Subscribe to user input91- // TODO: should we move this to the end-user program level or just keep this92- // here, since it blocks nicely and user input will probably be something93- // users typically need?90+ // Subscribe to user input. We could move this out of here and offer it91+ // as a subscription, but it blocks nicely and seems to be a common enough92+ // need that we're enabling it by default.93 go func() {94 for {95 msg, _ := ReadKey(p.rw)96 msgs <- KeyPressMsg(msg)96 unmodified lines193 clearLine()194 }195 }196 197+// Fullscreen switches to the altscreen and clears the terminal. The former198+// view can be restored with ExitFullscreen().199+func Fullscreen() {200+ fmt.Print(esc + "?1049h" + esc + "H")201+}202+203+// ExitFullscreen exits the altscreen and returns the former terminal view204+func ExitFullscreen() {205+ fmt.Print(esc + "?1049l")206+}207+198-// ClearScreen clears the visible portion of the terminal208+// ClearScreen clears the visible portion of the terminal. Effectively, it209+// fills the terminal with blank spaces.210 func ClearScreen() {211 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")212 }