Comments and cleanup
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 bbcdb3a · Browse files · 3 files·+44 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+44 added−9 removed
- other:+44 added−9 removed
Provenance
- Commit
6737526- Tree
4a10770- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
bbcdb3a
3 files changed+44 added−9 removed
modified:examples/pager/main.go+21 added-7 removed
32 unmodified lines33 fmt.Println("could not load file:", err)34 os.Exit(1)35 }36 37- // Set PAGER_LOG to a path to log to a file. For example,37+ // Set PAGER_LOG to a path to log to a file. For example:38 //39 // export PAGER_LOG=debug.log40 //41- if os.Getenv("PAGER_LOG") != "" {42- p := os.Getenv("PAGER_LOG")41+ // This becomes handy when debugging stuff since you can't debug to stdout42+ // because the UI is occupying it!43+ p := os.Getenv("PAGER_LOG")44+ if p != "" {45 f, err := tea.LogToFile(p, "pager")46 if err != nil {47 fmt.Printf("Could not open file %s: %v", p, err)48 os.Exit(1)49 }50 defer f.Close()51 }52 51- // Use the full size of the terminal in its "Alternate Screen Buffer"53+ // Use the full size of the terminal in its "alternate screen buffer"54 tea.AltScreen()55 defer tea.ExitAltScreen()56 57 if err := tea.NewProgram(14 unmodified lines72 73 func initialize(content string) func() (tea.Model, tea.Cmd) {74 return func() (tea.Model, tea.Cmd) {75 return model{74- content: content, // keep content in the model76+ // Store content in the model so we can hand it off to the viewport77+ // later.78+ content: content,79 }, nil80 }81 }82 6 unmodified lines89 )90 91 switch msg := msg.(type) {92 case tea.KeyMsg:93+ // Ctrl+c exits94 if msg.Type == tea.KeyCtrlC {95 return m, tea.Quit96 }97 98 case tea.WindowSizeMsg:99 verticalMargins := headerHeight + footerHeight100 101 if !m.ready {102+ // Since this program is using the full size of the viewport we need103+ // to wait until we've received the window dimensions before we104+ // can initialize the viewport. The initial dimensions come in105+ // quickly, though asynchronously, which is why we wait for them106+ // here.107 m.viewport = viewport.NewModel(msg.Width, msg.Height-verticalMargins)108 m.viewport.YPosition = headerHeight109 m.viewport.HighPerformanceRendering = useHighPerformanceRenderer110 m.viewport.SetContent(m.content)2 unmodified lines113 m.viewport.Width = msg.Width114 m.viewport.Height = msg.Height - verticalMargins115 }116 107- // Render (or re-render) the whole viewport117 if useHighPerformanceRenderer {118+ // Render (or re-render) the whole viewport. Necessary both to119+ // initialize the viewport and when the window is resized.120+ //121+ // This is needed for high-performance rendering only.122 cmds = append(cmds, viewport.Sync(m.viewport))123 }124 }125 1 unmodified line127 // style navigation) it's important that the viewport's update function:128 //129 // * Recieves messages from the Bubble Tea runtime130 // * Returns commands to the Bubble Tea runtime131+ //132 m.viewport, cmd = viewport.Update(msg, m.viewport)133 if useHighPerformanceRenderer {134 cmds = append(cmds, cmd)135 }20 unmodified lines156 gapSize := m.viewport.Width - runewidth.StringWidth(footerMid)157 footerTop = strings.Repeat(" ", gapSize) + footerTop158 footerMid = strings.Repeat("─", gapSize) + footerMid159 footerBot = strings.Repeat(" ", gapSize) + footerBot146- footer := footerTop + "\n" + footerMid + "\n" + footerBot160+ footer := fmt.Sprintf("%s\n%s\n%s", footerTop, footerMid, footerBot)161 162 return fmt.Sprintf("%s\n%s\n%s", header, viewport.View(m.viewport), footer)163 }modified:renderer.go+19 added
59 unmodified lines60 r.flush()61 r.done <- struct{}{}62 }63 64+// listen waits for ticks on the ticker, or a signal to stop the renderer.65 func (r *renderer) listen() {66 for {67 select {68 case <-r.ticker.C:210 unmodified lines279 topBoundary int280 bottomBoundary int281 }282 283+// SyncScrollArea performs a paint of the entire scroll area. This is required284+// to initialize the scrollable region and should also be called on resize285+// (WindowSizeMsg).286+//287+// For high-performance, scroll-based rendering only.288 func SyncScrollArea(lines []string, topBoundary int, bottomBoundary int) Cmd {289 return func() Msg {290 return syncScrollAreaMsg{291 lines: lines,4 unmodified lines296 }297 298 type clearScrollAreaMsg struct{}299 300+// ClearScrollArea deallocates the scrollable region and returns the control of301+// those lines to the main rendering routine.302+//303+// For high-performance, scroll-based rendering only.304 func ClearScrollArea() Msg {305 return clearScrollAreaMsg{}306 }307 2 unmodified lines310 topBoundary int311 bottomBoundary int312 }313 314+// ScrollUp adds lines to the top of the scrollable region, pushing lines below315+// down. Lines that are pushed out the scrollable region disappear from view.316+//317+// For high-performance, scroll-based rendering only.318 func ScrollUp(newLines []string, topBoundary, bottomBoundary int) Cmd {319 return func() Msg {320 return scrollUpMsg{321 lines: newLines,8 unmodified lines330 topBoundary int331 bottomBoundary int332 }333 334+// ScrollDown adds lines to the bottom of the scrollable region, pushing lines335+// above up. Lines that are pushed out of the scrollable region disappear from336+// view.337+//338+// For high-performance, scroll-based rendering only.339 func ScrollDown(newLines []string, topBoundary, bottomBoundary int) Cmd {340 return func() Msg {341 return scrollDownMsg{342 lines: newLines,5 unmodified linesmodified:tea.go+4 added-2 removed
6 unmodified lines7 "golang.org/x/crypto/ssh/terminal"8 )9 10 // Msg represents an action and is usually the result of an IO operation. It's11-// triggers the Update function, and henceforth, the UI.11+// triggers the Update function, and henceforth, the UI.12 type Msg interface{}13 14 // Model contains the program's state.15 type Model interface{}16 17-// Cmd is an IO operation. If it's nil it's considered a no-op.17+// Cmd is an IO operation. If it's nil it's considered a no-op. Keep in mind18+// that there's almost never a need to use a command to send a message to19+// another part of your program.20 type Cmd func() Msg21 22 // Batch peforms a bunch of commands concurrently with no ordering guarantees23 // about the results.171 unmodified lines