Skip to content
Myr

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

3 files+44 added−9 removed

modified:examples/pager/main.go+21 added-7 removed
32 unmodified lines
33 fmt.Println("could not load file:", err)34 os.Exit(1)35 }3637 // 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 }5251 // 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()5657 if err := tea.NewProgram(
14 unmodified lines
7273func 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 lines
89 )9091 switch msg := msg.(type) {92 case tea.KeyMsg:93 // Ctrl+c exits94 if msg.Type == tea.KeyCtrlC {95 return m, tea.Quit96 }9798 case tea.WindowSizeMsg:99 verticalMargins := headerHeight + footerHeight100101 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 lines
113 m.viewport.Width = msg.Width114 m.viewport.Height = msg.Height - verticalMargins115 }116107 // 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 line
127 // 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 lines
156 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)161162 return fmt.Sprintf("%s\n%s\n%s", header, viewport.View(m.viewport), footer)163}
modified:renderer.go+19 added
59 unmodified lines
60 r.flush()61 r.done <- struct{}{}62}6364// listen waits for ticks on the ticker, or a signal to stop the renderer.65func (r *renderer) listen() {66 for {67 select {68 case <-r.ticker.C:
210 unmodified lines
279 topBoundary int280 bottomBoundary int281}282283// 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.288func SyncScrollArea(lines []string, topBoundary int, bottomBoundary int) Cmd {289 return func() Msg {290 return syncScrollAreaMsg{291 lines: lines,
4 unmodified lines
296}297298type clearScrollAreaMsg struct{}299300// 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.304func ClearScrollArea() Msg {305 return clearScrollAreaMsg{}306}307
2 unmodified lines
310 topBoundary int311 bottomBoundary int312}313314// 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.318func ScrollUp(newLines []string, topBoundary, bottomBoundary int) Cmd {319 return func() Msg {320 return scrollUpMsg{321 lines: newLines,
8 unmodified lines
330 topBoundary int331 bottomBoundary int332}333334// 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.339func ScrollDown(newLines []string, topBoundary, bottomBoundary int) Cmd {340 return func() Msg {341 return scrollDownMsg{342 lines: newLines,
5 unmodified lines
modified:tea.go+4 added-2 removed
6 unmodified lines
7 "golang.org/x/crypto/ssh/terminal"8)910// 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.12type Msg interface{}1314// Model contains the program's state.15type Model interface{}1617// 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.20type Cmd func() Msg2122// Batch peforms a bunch of commands concurrently with no ordering guarantees23// about the results.
171 unmodified lines