Rename project to Boba
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 9dd9461 · Browse files · 17 files·+391 added−379 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
17 files+391 added−379 removed
- other:+391 added−379 removed
Provenance
- Commit
4bf40fb- Tree
2b3c604- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
9dd9461
17 files changed+391 added−379 removed
modified:examples/fullscreen/main.go+14 added-14 removed
5 unmodified lines6 "fmt"7 "log"8 "time"9 10- "github.com/charmbracelet/tea"10+ "github.com/charmbracelet/boba"11 )12 13 type model int14 15 type tickMsg time.Time16 17-func newTickMsg(t time.Time) tea.Msg {17+func newTickMsg(t time.Time) boba.Msg {18 return tickMsg(t)19 }20 21 func main() {22- tea.AltScreen()23- defer tea.ExitAltScreen()24- err := tea.NewProgram(initialize, update, view, subscriptions).Start()22+ boba.AltScreen()23+ defer boba.ExitAltScreen()24+ err := boba.NewProgram(initialize, update, view, subscriptions).Start()25 if err != nil {26 log.Fatal(err)27 }28 }29 30-func initialize() (tea.Model, tea.Cmd) {30+func initialize() (boba.Model, boba.Cmd) {31 return model(5), nil32 }33 34-func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {34+func update(message boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {35 m, _ := mdl.(model)36 37 switch msg := message.(type) {38 39- case tea.KeyMsg:39+ case boba.KeyMsg:40 switch msg.String() {41 case "ctrl+c":42 fallthrough43 case "esc":44 fallthrough45 case "q":46- return m, tea.Quit46+ return m, boba.Quit47 }48 49 case tickMsg:50 m -= 151 if m <= 0 {52- return m, tea.Quit52+ return m, boba.Quit53 }54 55 }56 57 return m, nil58 }59 60-func subscriptions(_ tea.Model) tea.Subs {61- return tea.Subs{62- "tick": tea.Every(time.Second, newTickMsg),60+func subscriptions(_ boba.Model) boba.Subs {61+ return boba.Subs{62+ "tick": boba.Every(time.Second, newTickMsg),63 }64 }65 66-func view(mdl tea.Model) string {66+func view(mdl boba.Model) string {67 m, _ := mdl.(model)68 return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)69 }modified:examples/http/main.go+9 added-9 removed
7 unmodified lines8 "log"9 "net/http"10 "time"11 12- "github.com/charmbracelet/tea"12+ "github.com/charmbracelet/boba"13 )14 15 const url = "https://charm.sh/"16 5 unmodified lines22 type statusMsg int23 type errMsg error24 25 func main() {26- p := tea.NewProgram(initialize, update, view, nil)26+ p := boba.NewProgram(initialize, update, view, nil)27 if err := p.Start(); err != nil {28 log.Fatal(err)29 }30 }31 32-func initialize() (tea.Model, tea.Cmd) {32+func initialize() (boba.Model, boba.Cmd) {33 return Model{0, nil}, checkServer34 }35 36-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {36+func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {37 m, ok := model.(Model)38 if !ok {39 return Model{err: errors.New("could not perform assertion on model during update")}, nil40 }41 42 switch msg := msg.(type) {43 44- case tea.KeyMsg:44+ case boba.KeyMsg:45 switch msg.String() {46 case "esc":47 fallthrough48 case "ctrl+c":49 fallthrough50 case "q":51- return m, tea.Quit51+ return m, boba.Quit52 default:53 return m, nil54 }55 56 case statusMsg:57 m.status = int(msg)58- return m, tea.Quit58+ return m, boba.Quit59 60 case errMsg:61 m.err = msg62 return m, nil2 unmodified lines65 return m, nil66 }67 }68 69-func view(model tea.Model) string {69+func view(model boba.Model) string {70 m, _ := model.(Model)71 s := fmt.Sprintf("Checking %s...", url)72 if m.err != nil {73 s += fmt.Sprintf("something went wrong: %s", m.err)2 unmodified lines76 }77 return s78 }79 80-func checkServer() tea.Msg {80+func checkServer() boba.Msg {81 c := &http.Client{82 Timeout: 10 * time.Second,83 }84 res, err := c.Get(url)5 unmodified linesmodified:examples/input/main.go+13 added-13 removed
5 unmodified lines6 "errors"7 "fmt"8 "log"9 10- "github.com/charmbracelet/tea"10+ "github.com/charmbracelet/boba"11 "github.com/charmbracelet/teaparty/input"12 )13 14 type Model struct {4 unmodified lines19 type tickMsg struct{}20 type errMsg error21 22 func main() {23- p := tea.NewProgram(23+ p := boba.NewProgram(24 initialize,25 update,26 view,27 subscriptions,3 unmodified lines31 log.Fatal(err)32 }33 }34 35-func initialize() (tea.Model, tea.Cmd) {35+func initialize() (boba.Model, boba.Cmd) {36 inputModel := input.DefaultModel()37 inputModel.Placeholder = "Pikachu"38 39 return Model{1 unmodified line41 err: nil,42 }, nil43 }44 45-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {46- var cmd tea.Cmd45+func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {46+ var cmd boba.Cmd47 m, ok := model.(Model)48 if !ok {49 // When we encounter errors in Update we simply add the error to the50 // model so we can handle it in the view. We could also return a command3 unmodified lines54 }, nil55 }56 57 switch msg := msg.(type) {58- case tea.KeyMsg:58+ case boba.KeyMsg:59 switch msg.Type {60- case tea.KeyCtrlC:60+ case boba.KeyCtrlC:61 fallthrough62- case tea.KeyEsc:63- return m, tea.Quit62+ case boba.KeyEsc:63+ return m, boba.Quit64 }65 66 // We handle errors just like any other message67 case errMsg:4 unmodified lines72 m.textInput, cmd = input.Update(msg, m.textInput)73 return m, cmd74 }75 76-func subscriptions(model tea.Model) tea.Subs {77- return tea.Subs{76+func subscriptions(model boba.Model) boba.Subs {77+ return boba.Subs{78 // We just hand off the subscription to the input component, giving79 // it the model it expects.80- "input": func(model tea.Model) tea.Msg {80+ "input": func(model boba.Model) boba.Msg {81 m, _ := model.(Model)82 return input.Blink(m.textInput)83 },84 }85 }86 87-func view(model tea.Model) string {87+func view(model boba.Model) string {88 m, ok := model.(Model)89 if !ok {90 return "Oh no: could not perform assertion on model."91 } else if m.err != nil {8 unmodified linesmodified:examples/pager/main.go+3 added-3 removed
3 unmodified lines4 "fmt"5 "io/ioutil"6 "os"7 8- "github.com/charmbracelet/tea"8+ "github.com/charmbracelet/boba"9 "github.com/charmbracelet/teaparty/pager"10 )11 12 func main() {2 unmodified lines15 fmt.Println("could not load file:", err)16 os.Exit(1)17 }18 19- tea.AltScreen()20- defer tea.ExitAltScreen()19+ boba.AltScreen()20+ defer boba.ExitAltScreen()21 if err := pager.NewProgram(string(content)).Start(); err != nil {22 fmt.Println("could not run program:", err)23 os.Exit(1)24 }1 unmodified linemodified:examples/simple/main.go+11 added-11 removed
5 unmodified lines6 "fmt"7 "log"8 "time"9 10- "github.com/charmbracelet/tea"10+ "github.com/charmbracelet/boba"11 )12 13 // A model can be more or less any type of data. It holds all the data for a14 // program, so often it's a struct. For this simple example, however, all5 unmodified lines20 type tickMsg time.Time21 22 func main() {23 // Initialize our program24- p := tea.NewProgram(initialize, update, view, subscriptions)24+ p := boba.NewProgram(initialize, update, view, subscriptions)25 if err := p.Start(); err != nil {26 log.Fatal(err)27 }28 }29 30-func initialize() (tea.Model, tea.Cmd) {30+func initialize() (boba.Model, boba.Cmd) {31 return Model(5), nil32 }33 34 // Update is called when messages are recived. The idea is that you inspect35 // the message and update the model (or send back a new one) accordingly. You36 // can also return a commmand, which is a function that peforms I/O and37 // returns a message.38-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {38+func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {39 m, _ := model.(Model)40 41 switch msg.(type) {42- case tea.KeyMsg:43- return m, tea.Quit42+ case boba.KeyMsg:43+ return m, boba.Quit44 case tickMsg:45 m -= 146 if m <= 0 {47- return m, tea.Quit47+ return m, boba.Quit48 }49 }50 return m, nil51 }52 53 // Views take data from the model and return a string which will be rendered54 // to the terminal.55-func view(model tea.Model) string {55+func view(model boba.Model) string {56 m, _ := model.(Model)57 return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.", m)58 }59 60 // This is a subscription which we setup in NewProgram(). It waits for one61 // second, sends a tick, and then restarts.62-func subscriptions(_ tea.Model) tea.Subs {63- return tea.Subs{64- "tick": tea.Every(time.Second, func(t time.Time) tea.Msg {62+func subscriptions(_ boba.Model) boba.Subs {63+ return boba.Subs{64+ "tick": boba.Every(time.Second, func(t time.Time) boba.Msg {65 return tickMsg(t)66 }),67 }68 }modified:examples/spinner/main.go+9 added-9 removed
2 unmodified lines3 import (4 "fmt"5 "log"6 7- "github.com/charmbracelet/tea"7+ "github.com/charmbracelet/boba"8 "github.com/charmbracelet/teaparty/spinner"9 "github.com/muesli/termenv"10 )11 8 unmodified lines20 21 type errMsg error22 23 func main() {24- p := tea.NewProgram(initialize, update, view, subscriptions)24+ p := boba.NewProgram(initialize, update, view, subscriptions)25 if err := p.Start(); err != nil {26 log.Fatal(err)27 }28 }29 30-func initialize() (tea.Model, tea.Cmd) {30+func initialize() (boba.Model, boba.Cmd) {31 m := spinner.NewModel()32 m.Type = spinner.Dot33 34 return Model{35 spinner: m,36 }, nil37 }38 39-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {39+func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {40 m, ok := model.(Model)41 if !ok {42 return model, nil43 }44 45 switch msg := msg.(type) {46 47- case tea.KeyMsg:47+ case boba.KeyMsg:48 switch msg.String() {49 case "q":50 fallthrough51 case "esc":52 fallthrough53 case "ctrl+c":54- return m, tea.Quit54+ return m, boba.Quit55 default:56 return m, nil57 }58 7 unmodified lines66 }67 68 }69 70-func view(model tea.Model) string {70+func view(model boba.Model) string {71 m, ok := model.(Model)72 if !ok {73 return "could not perform assertion on model in view"74 }6 unmodified lines81 String()82 return fmt.Sprintf("\n\n %s Loading forever...press q to quit\n\n", s)83 }84 85-func subscriptions(model tea.Model) tea.Subs {85+func subscriptions(model boba.Model) boba.Subs {86 m, ok := model.(Model)87 if !ok {88 return nil89 }1 unmodified line91 sub, err := spinner.MakeSub(m.spinner)92 if err != nil {93 return nil94 }95- return tea.Subs{95+ return boba.Subs{96 "tick": sub,97 }98 }modified:examples/go.mod+4 added-3 removed
1 module examples2 3 go 1.134 5-replace github.com/charmbracelet/tea => ../5+replace github.com/charmbracelet/boba => ../6 7 require (8- github.com/charmbracelet/tea v0.0.0-20200130023737-bb06373836b49- github.com/charmbracelet/teaparty v0.0.0-20200212224515-b4d35fd529068+ github.com/charmbracelet/boba v0.0.0-00010101000000-0000000000009+ github.com/charmbracelet/tea v0.3.010+ github.com/charmbracelet/teaparty v0.0.0-20200511213328-a72bf9128d8311 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177612 github.com/muesli/termenv v0.5.213 )modified:examples/go.sum+9 added
1+github.com/charmbracelet/tea v0.0.0-20200130023737-bb06373836b4 h1:O8IGyYrKQuCwZZ98JP3DvzQCoPiXM5Y2zjwlDY7mOFM=2+github.com/charmbracelet/tea v0.0.0-20200130023737-bb06373836b4/go.mod h1:UsFFdg04MNbcYi1r2FBtdDEFY07bObaYDKHhE1xZUaQ=3+github.com/charmbracelet/tea v0.3.0 h1:W5F1x/IYeSCKpZl3/hM3Mn5v2KAagckabDFhhzh5sIE=4+github.com/charmbracelet/tea v0.3.0/go.mod h1:uA/DUzCuyIZ1NFyAdCz6k+gF8lspujo6ZvoavcSsLCM=5 github.com/charmbracelet/teaparty v0.0.0-20200212224515-b4d35fd52906 h1:kcvv+hjb0dJiqhtMXkql5tczxalWMPvKIqwWo7cyhiQ=6 github.com/charmbracelet/teaparty v0.0.0-20200212224515-b4d35fd52906/go.mod h1:BG6oiwNZL9hB739ZOifRi3ePRGv0nT+kRTfxYLcZj/Y=7+github.com/charmbracelet/teaparty v0.0.0-20200511213328-a72bf9128d83 h1:ivIS4ze0LLG9yl9L8cnYerh6dWnqhmBqh1ohmrA3I/I=8+github.com/charmbracelet/teaparty v0.0.0-20200511213328-a72bf9128d83/go.mod h1:rlnGPwUokLHs2rQBiIYg6fpLM5m4DvtE2LwzdvS1wsk=9 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=10 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=11 github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=12 github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=6 unmodified lines19 github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=20 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=21 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=22 golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=23+golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=24 golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=25 golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=26 golang.org/x/sys v0.0.0-20200430202703-d923437fa56d h1:xmcims+WSpFuY56YEzkKF6IMDxYAVDRipkQRJfXUBZk=27 golang.org/x/sys v0.0.0-20200430202703-d923437fa56d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=28+golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f h1:mOhmO9WsBaJCNmaZHPtHs9wOcdqdKCjF6OPJlmDM3KI=29+golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=added:boba.go+296 added
1+package boba2+3+import (4+ "io"5+ "os"6+ "strings"7+8+ "github.com/muesli/termenv"9+)10+11+// Msg represents an action and is usually the result of an IO operation. It's12+// triggers the Update function, and henceforth, the UI.13+type Msg interface{}14+15+// Model contains the program's state.16+type Model interface{}17+18+// Cmd is an IO operation that runs once. If it's nil it's considered a no-op.19+type Cmd func() Msg20+21+// Batch peforms a bunch of commands concurrently with no ordering guarantees22+// about the results.23+func Batch(cmds ...Cmd) Cmd {24+ if len(cmds) == 0 {25+ return nil26+ }27+ return func() Msg {28+ return batchMsg(cmds)29+ }30+}31+32+// Sub is an event subscription; generally a recurring IO operation. If it33+// returns nil it's considered a no-op, but there's really no reason to have34+// a nil subscription.35+type Sub func() Msg36+37+// Subs is a keyed set of subscriptions. The key should be a unique38+// identifier: two different subscriptions should not have the same key or39+// weird behavior will occur.40+type Subs map[string]Sub41+42+// Subscriptions returns a map of subscriptions (Subs) our application will43+// subscribe to. If Subscriptions is nil it's considered a no-op.44+type Subscriptions func(Model) Subs45+46+// subscription is an internal reference to a subscription used in subscription47+// management.48+type subscription struct {49+ done chan struct{}50+ sub Sub51+}52+53+// subManager is used to manage active subscriptions, hence the pointers.54+type subManager map[string]*subscription55+56+// endAll stops all subscriptions and remove subscription references from57+// subManager.58+func (m *subManager) endAll() {59+ if m != nil {60+ for key, sub := range *m {61+ close(sub.done)62+ delete(*m, key)63+ }64+ }65+}66+67+// Init is the first function that will be called. It returns your initial68+// model and runs an optional command69+type Init func() (Model, Cmd)70+71+// Update is called when a message is received. It may update the model and/or72+// send a command.73+type Update func(Msg, Model) (Model, Cmd)74+75+// View produces a string which will be rendered to the terminal76+type View func(Model) string77+78+// Program is a terminal user interface79+type Program struct {80+ init Init81+ update Update82+ view View83+ subscriptions Subscriptions84+}85+86+// Quit is a command that tells the program to exit87+func Quit() Msg {88+ return quitMsg{}89+}90+91+// Signals that the program should quit92+type quitMsg struct{}93+94+// batchMsg is used to perform a bunch of commands95+type batchMsg []Cmd96+97+// NewProgram creates a new Program98+func NewProgram(init Init, update Update, view View, subs Subscriptions) *Program {99+ return &Program{100+ init: init,101+ update: update,102+ view: view,103+ subscriptions: subs,104+ }105+}106+107+// Start initializes the program108+func (p *Program) Start() error {109+ var (110+ model Model111+ cmd Cmd112+ subs = make(subManager)113+ cmds = make(chan Cmd)114+ msgs = make(chan Msg)115+ errs = make(chan error)116+ done = make(chan struct{})117+ linesRendered int118+ )119+120+ err := initTerminal()121+ if err != nil {122+ return err123+ }124+ defer restoreTerminal()125+126+ // Initialize program127+ model, cmd = p.init()128+ if cmd != nil {129+ go func() {130+ cmds <- cmd131+ }()132+ }133+134+ // Render initial view135+ linesRendered = p.render(model, linesRendered)136+137+ // Subscribe to user input. We could move this out of here and offer it138+ // as a subscription, but it blocks nicely and seems to be a common enough139+ // need that we're enabling it by default.140+ go func() {141+ for {142+ msg, err := ReadKey(os.Stdin)143+ if err != nil {144+ errs <- err145+ }146+ msgs <- KeyMsg(msg)147+ }148+ }()149+150+ // Initialize subscriptions151+ subs = p.processSubs(msgs, model, subs)152+153+ // Process commands154+ go func() {155+ for {156+ select {157+ case <-done:158+ return159+ case cmd := <-cmds:160+ if cmd != nil {161+ go func() {162+ msgs <- cmd()163+ }()164+ }165+ }166+ }167+ }()168+169+ // Handle updates and draw170+ for {171+ select {172+ case err := <-errs:173+ close(done)174+ return err175+ case msg := <-msgs:176+177+ // Handle quit message178+ if _, ok := msg.(quitMsg); ok {179+ close(done)180+ return nil181+ }182+183+ // Process batch commands184+ if batchedCmds, ok := msg.(batchMsg); ok {185+ for _, cmd := range batchedCmds {186+ cmds <- cmd187+ }188+ continue189+ }190+191+ model, cmd = p.update(msg, model) // run update192+ cmds <- cmd // process command (if any)193+ subs = p.processSubs(msgs, model, subs) // check for new and outdated subscriptions194+ linesRendered = p.render(model, linesRendered) // render to terminal195+ }196+ }197+}198+199+// Render a view to the terminal. Returns the number of lines rendered.200+func (p *Program) render(model Model, linesRendered int) int {201+ view := p.view(model)202+203+ // We need to add carriage returns to ensure that the cursor travels to the204+ // start of a column after a newline205+ view = strings.Replace(view, "\n", "\r\n", -1)206+207+ if linesRendered > 0 {208+ termenv.ClearLines(linesRendered)209+ }210+ _, _ = io.WriteString(os.Stdout, view)211+ return strings.Count(view, "\r\n")212+}213+214+// Manage subscriptions. Here we run the program's Subscription function and215+// inspect the functions it returns (a Subs map). If we notice existing216+// subscriptions have disappeared from the map we stop those subscriptions217+// by ending the Goroutines they run on. If we notice new subscriptions which218+// aren't currently running, we run them as loops in a new Goroutine.219+//220+// This function should be called on initialization and after every update.221+func (p *Program) processSubs(msgs chan Msg, model Model, activeSubs subManager) subManager {222+223+ // Nothing to do.224+ if p.subscriptions == nil && activeSubs == nil {225+ return activeSubs226+ }227+228+ // There are no subscriptions. Cancel active ones and return.229+ if p.subscriptions == nil && activeSubs != nil {230+ activeSubs.endAll()231+ return activeSubs232+ }233+234+ newSubs := p.subscriptions(model)235+236+ // newSubs is an empty map. Cancel any active subscriptions and return.237+ if newSubs == nil {238+ activeSubs.endAll()239+ return activeSubs240+ }241+242+ // Stop subscriptions that don't exist in the new subscription map and243+ // stop subscriptions where the new subscription is mapped to a nil.244+ if len(activeSubs) > 0 {245+ for key, sub := range activeSubs {246+ _, exists := newSubs[key]247+ if !exists || exists && newSubs[key] == nil {248+ close(sub.done)249+ delete(activeSubs, key)250+ }251+ }252+ }253+254+ // Start new subscriptions if they don't exist in the active subscription map255+ if len(newSubs) > 0 {256+ for key, sub := range newSubs {257+ if _, exists := activeSubs[key]; !exists {258+259+ if sub == nil {260+ continue261+ }262+263+ activeSubs[key] = &subscription{264+ done: make(chan struct{}),265+ sub: sub,266+ }267+268+ go func(done chan struct{}, s Sub) {269+ for {270+ select {271+ case <-done:272+ return273+ case msgs <- s():274+ continue275+ }276+ }277+ }(activeSubs[key].done, activeSubs[key].sub)278+279+ }280+ }281+ }282+283+ return activeSubs284+}285+286+// AltScreen exits the altscreen. This is just a wrapper around the termenv287+// function288+func AltScreen() {289+ termenv.AltScreen()290+}291+292+// ExitAltScreen exits the altscreen. This is just a wrapper around the termenv293+// function294+func ExitAltScreen() {295+ termenv.ExitAltScreen()296+}modified:go.mod+1 added-1 removed
1-module github.com/charmbracelet/tea1+module github.com/charmbracelet/boba2 3 go 1.134 5 require (4 unmodified linesmodified:key.go+1 added-1 removed
1-package tea1+package boba2 3 import (4 "errors"5 "fmt"279 unmodified linesmodified:logging.go+1 added-1 removed
1 // +build darwin dragonfly freebsd linux netbsd openbsd solaris2 3-package tea3+package boba4 5 import (6 "log"7 "log/syslog"15 unmodified linesmodified:README.md+17 added-15 removed
1-# Tea1+# Boba2 3 The fun, functional way to build terminal apps. A Go framework based on4-[The Elm Architecture][elm]. 茶!4+[The Elm Architecture][elm].5 6-⚠️ This project is a pre-release! The API is subject to change a little.6+⚠️ This project is a pre-release so the API is subject to change7+a little. That said, we're using it in production.8 9+10 ## Simple example11 12 ```go13 package main3 unmodified lines17 import (18 "fmt"19 "log"20 "time"19- "github.com/charmbracelet/tea"21+ "github.com/charmbracelet/boba"22 )23 24 type model int25 26 type tickMsg time.Time27 28 func main() {27- p := tea.NewProgram(init, update, view, subscriptions)29+ p := boba.NewProgram(init, update, view, subscriptions)30 if err := p.Start(); err != nil {31 log.Fatal(err)32 }33 }34 35 // Listen for messages and update the model accordingly34-func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {36+func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {37 m, _ := mdl.(model)38 39 switch msg.(type) {40 case tickMsg:41 m--42 if m == 0 {41- return m, tea.Quit43+ return m, boba.Quit44 }45 }46 return m, nil47 }48 49 // Render to the terminal48-func view(mdl tea.Model) string {50+func view(mdl boba.Model) string {51 m, _ := mdl.(model)52 return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)53 }54 55 // Subscribe to events54-func subscriptions(_ tea.Model) tea.Subs {55- return tea.Subs{56- "tick": time.Every(time.Second, func(t time.Time) tea.Msg {56+func subscriptions(_ boba.Model) boba.Subs {57+ return boba.Subs{58+ "tick": time.Every(time.Second, func(t time.Time) boba.Msg {59 return tickMsg(t)60 },61 }62 }63 ```64 65 Hungry for more? See the [other examples][examples].66 65-[examples]: https://github.com/charmbracelet/tea/tree/master/examples67+[examples]: https://github.com/charmbracelet/boba/tree/master/examples68 69+70 ## Other Resources71 69-* [Tea Party](https://github.com/charmbracelet/teaparty): a collection of Tea70- components.72 * [Termenv](https://github.com/muesli/termenv): advanced ANSI style and color73 support for your terminal applications. Very useful when rendering your74 views.75 * [Reflow](https://github.com/muesli/reflow): a collection of ANSI-aware text7 unmodified lines83 84 [elm]: https://guide.elm-lang.org/architecture/85 [gotea]: https://github.com/tj/go-tea86 87+88 ## License89 88-[MIT](https://github.com/charmbracelet/tea/raw/master/LICENSE)90+[MIT](https://github.com/charmbracelet/boba/raw/master/LICENSE)91 92 ***93 94 Part of [Charm](https://charm.sh).4 unmodified linesmodified:subscriptions.go+1 added-1 removed
1-package tea1+package boba2 3 import (4 "time"5 )27 unmodified linesdeleted:tea.go-296 removed
1-package tea2-3-import (4- "io"5- "os"6- "strings"7-8- "github.com/muesli/termenv"9-)10-11-// Msg represents an action and is usually the result of an IO operation. It's12-// triggers the Update function, and henceforth, the UI.13-type Msg interface{}14-15-// Model contains the program's state.16-type Model interface{}17-18-// Cmd is an IO operation that runs once. If it's nil it's considered a no-op.19-type Cmd func() Msg20-21-// Batch peforms a bunch of commands concurrently with no ordering guarantees22-// about the results.23-func Batch(cmds ...Cmd) Cmd {24- if len(cmds) == 0 {25- return nil26- }27- return func() Msg {28- return batchMsg(cmds)29- }30-}31-32-// Sub is an event subscription; generally a recurring IO operation. If it33-// returns nil it's considered a no-op, but there's really no reason to have34-// a nil subscription.35-type Sub func() Msg36-37-// Subs is a keyed set of subscriptions. The key should be a unique38-// identifier: two different subscriptions should not have the same key or39-// weird behavior will occur.40-type Subs map[string]Sub41-42-// Subscriptions returns a map of subscriptions (Subs) our application will43-// subscribe to. If Subscriptions is nil it's considered a no-op.44-type Subscriptions func(Model) Subs45-46-// subscription is an internal reference to a subscription used in subscription47-// management.48-type subscription struct {49- done chan struct{}50- sub Sub51-}52-53-// subManager is used to manage active subscriptions, hence the pointers.54-type subManager map[string]*subscription55-56-// endAll stops all subscriptions and remove subscription references from57-// subManager.58-func (m *subManager) endAll() {59- if m != nil {60- for key, sub := range *m {61- close(sub.done)62- delete(*m, key)63- }64- }65-}66-67-// Init is the first function that will be called. It returns your initial68-// model and runs an optional command69-type Init func() (Model, Cmd)70-71-// Update is called when a message is received. It may update the model and/or72-// send a command.73-type Update func(Msg, Model) (Model, Cmd)74-75-// View produces a string which will be rendered to the terminal76-type View func(Model) string77-78-// Program is a terminal user interface79-type Program struct {80- init Init81- update Update82- view View83- subscriptions Subscriptions84-}85-86-// Quit is a command that tells the program to exit87-func Quit() Msg {88- return quitMsg{}89-}90-91-// Signals that the program should quit92-type quitMsg struct{}93-94-// batchMsg is used to perform a bunch of commands95-type batchMsg []Cmd96-97-// NewProgram creates a new Program98-func NewProgram(init Init, update Update, view View, subs Subscriptions) *Program {99- return &Program{100- init: init,101- update: update,102- view: view,103- subscriptions: subs,104- }105-}106-107-// Start initializes the program108-func (p *Program) Start() error {109- var (110- model Model111- cmd Cmd112- subs = make(subManager)113- cmds = make(chan Cmd)114- msgs = make(chan Msg)115- errs = make(chan error)116- done = make(chan struct{})117- linesRendered int118- )119-120- err := initTerminal()121- if err != nil {122- return err123- }124- defer restoreTerminal()125-126- // Initialize program127- model, cmd = p.init()128- if cmd != nil {129- go func() {130- cmds <- cmd131- }()132- }133-134- // Render initial view135- linesRendered = p.render(model, linesRendered)136-137- // Subscribe to user input. We could move this out of here and offer it138- // as a subscription, but it blocks nicely and seems to be a common enough139- // need that we're enabling it by default.140- go func() {141- for {142- msg, err := ReadKey(os.Stdin)143- if err != nil {144- errs <- err145- }146- msgs <- KeyMsg(msg)147- }148- }()149-150- // Initialize subscriptions151- subs = p.processSubs(msgs, model, subs)152-153- // Process commands154- go func() {155- for {156- select {157- case <-done:158- return159- case cmd := <-cmds:160- if cmd != nil {161- go func() {162- msgs <- cmd()163- }()164- }165- }166- }167- }()168-169- // Handle updates and draw170- for {171- select {172- case err := <-errs:173- close(done)174- return err175- case msg := <-msgs:176-177- // Handle quit message178- if _, ok := msg.(quitMsg); ok {179- close(done)180- return nil181- }182-183- // Process batch commands184- if batchedCmds, ok := msg.(batchMsg); ok {185- for _, cmd := range batchedCmds {186- cmds <- cmd187- }188- continue189- }190-191- model, cmd = p.update(msg, model) // run update192- cmds <- cmd // process command (if any)193- subs = p.processSubs(msgs, model, subs) // check for new and outdated subscriptions194- linesRendered = p.render(model, linesRendered) // render to terminal195- }196- }197-}198-199-// Render a view to the terminal. Returns the number of lines rendered.200-func (p *Program) render(model Model, linesRendered int) int {201- view := p.view(model)202-203- // We need to add carriage returns to ensure that the cursor travels to the204- // start of a column after a newline205- view = strings.Replace(view, "\n", "\r\n", -1)206-207- if linesRendered > 0 {208- termenv.ClearLines(linesRendered)209- }210- _, _ = io.WriteString(os.Stdout, view)211- return strings.Count(view, "\r\n")212-}213-214-// Manage subscriptions. Here we run the program's Subscription function and215-// inspect the functions it returns (a Subs map). If we notice existing216-// subscriptions have disappeared from the map we stop those subscriptions217-// by ending the Goroutines they run on. If we notice new subscriptions which218-// aren't currently running, we run them as loops in a new Goroutine.219-//220-// This function should be called on initialization and after every update.221-func (p *Program) processSubs(msgs chan Msg, model Model, activeSubs subManager) subManager {222-223- // Nothing to do.224- if p.subscriptions == nil && activeSubs == nil {225- return activeSubs226- }227-228- // There are no subscriptions. Cancel active ones and return.229- if p.subscriptions == nil && activeSubs != nil {230- activeSubs.endAll()231- return activeSubs232- }233-234- newSubs := p.subscriptions(model)235-236- // newSubs is an empty map. Cancel any active subscriptions and return.237- if newSubs == nil {238- activeSubs.endAll()239- return activeSubs240- }241-242- // Stop subscriptions that don't exist in the new subscription map and243- // stop subscriptions where the new subscription is mapped to a nil.244- if len(activeSubs) > 0 {245- for key, sub := range activeSubs {246- _, exists := newSubs[key]247- if !exists || exists && newSubs[key] == nil {248- close(sub.done)249- delete(activeSubs, key)250- }251- }252- }253-254- // Start new subscriptions if they don't exist in the active subscription map255- if len(newSubs) > 0 {256- for key, sub := range newSubs {257- if _, exists := activeSubs[key]; !exists {258-259- if sub == nil {260- continue261- }262-263- activeSubs[key] = &subscription{264- done: make(chan struct{}),265- sub: sub,266- }267-268- go func(done chan struct{}, s Sub) {269- for {270- select {271- case <-done:272- return273- case msgs <- s():274- continue275- }276- }277- }(activeSubs[key].done, activeSubs[key].sub)278-279- }280- }281- }282-283- return activeSubs284-}285-286-// AltScreen exits the altscreen. This is just a wrapper around the termenv287-// function288-func AltScreen() {289- termenv.AltScreen()290-}291-292-// ExitAltScreen exits the altscreen. This is just a wrapper around the termenv293-// function294-func ExitAltScreen() {295- termenv.ExitAltScreen()296-}modified:tty_unix.go+1 added-1 removed
1 // +build darwin dragonfly freebsd linux netbsd openbsd solaris2 3-package tea3+package boba4 5 import (6 "github.com/muesli/termenv"7 "github.com/pkg/term"26 unmodified linesmodified:tty_windows.go+1 added-1 removed
1 // +build windows2 3-package tea3+package boba4 5 import "github.com/muesli/termenv"6 7 func initTerminal() error {7 unmodified lines