Skip to content
Myr

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

4 files+78 added−17 removed

added:examples/fullscreen/main.go+59 added
1package main23// A simple program that counts down from 5 and then exits.45import (6 "fmt"7 "log"8 "tea"9 "time"10)1112type model int1314type tickMsg struct{}1516func 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}2425func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {26 m, _ := mdl.(model)2728 switch message := msg.(type) {2930 case tea.KeyPressMsg:31 switch message {32 case "ctrl+c":33 fallthrough34 case "esc":35 fallthrough36 case "q":37 return m, tea.Quit38 }3940 case tickMsg:41 m -= 142 if m <= 0 {43 return m, tea.Quit44 }4546 }4748 return m, nil49}5051func 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}5556func tick(_ tea.Model) tea.Msg {57 time.Sleep(time.Second)58 return tickMsg{}59}
modified:go.mod+1 added-1 removed
3 unmodified lines
45require (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
1github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=4github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=3github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=4github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=7github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=8github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=9github.com/tj/go-terminput v1.0.0 h1:X928u7ohxr7Dfzlxppvc5Zql5Lwd1bOH5VyIL2RPrFw=10github.com/tj/go-terminput v1.0.0/go.mod h1:8zzAs+cqdjZlTxE9DbGyjKDfGNxaJjC5bvg4vkWX2V0=11golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=12golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=13golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=14golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=15golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=5golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=6golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
modified:tea.go+16 added-5 removed
85 unmodified lines
86 // Render initial view87 hideCursor()88 p.render(model)8990 // 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 lines
193 clearLine()194 }195}196197// Fullscreen switches to the altscreen and clears the terminal. The former198// view can be restored with ExitFullscreen().199func Fullscreen() {200 fmt.Print(esc + "?1049h" + esc + "H")201}202203// ExitFullscreen exits the altscreen and returns the former terminal view204func ExitFullscreen() {205 fmt.Print(esc + "?1049l")206}207198// ClearScreen clears the visible portion of the terminal208// ClearScreen clears the visible portion of the terminal. Effectively, it209// fills the terminal with blank spaces.210func ClearScreen() {211 fmt.Printf(esc + "2J" + esc + "3J" + esc + "1;1H")212}