1package viewport23import (4 "charm.land/bubbles/v2/key"5 "charm.land/bubbles/v2/viewport"6 tea "charm.land/bubbletea/v2"7 "github.com/charmbracelet/soft-serve/pkg/ui/common"8)910// Viewport represents a viewport component.11type Viewport struct {12 common common.Common13 *viewport.Model14}1516// New returns a new Viewport.17func New(c common.Common) *Viewport {18 vp := viewport.New()19 vp.SetWidth(c.Width)20 vp.SetHeight(c.Height)21 vp.MouseWheelEnabled = true22 return &Viewport{23 common: c,24 Model: &vp,25 }26}2728// SetSize implements common.Component.29func (v *Viewport) SetSize(width, height int) {30 v.common.SetSize(width, height)31 v.Model.SetWidth(width)32 v.Model.SetHeight(height)33}3435// Init implements tea.Model.36func (v *Viewport) Init() tea.Cmd {37 return nil38}3940// Update implements tea.Model.41func (v *Viewport) Update(msg tea.Msg) (common.Model, tea.Cmd) {42 switch msg := msg.(type) {43 case tea.KeyPressMsg:44 switch {45 case key.Matches(msg, v.common.KeyMap.GotoTop):46 v.GotoTop()47 case key.Matches(msg, v.common.KeyMap.GotoBottom):48 v.GotoBottom()49 }50 }51 vp, cmd := v.Model.Update(msg)52 v.Model = &vp53 return v, cmd54}5556// View implements tea.Model.57func (v *Viewport) View() string {58 return v.Model.View()59}6061// SetContent sets the viewport's content.62func (v *Viewport) SetContent(content string) {63 v.Model.SetContent(content)64}6566// GotoTop moves the viewport to the top of the log.67func (v *Viewport) GotoTop() {68 v.Model.GotoTop()69}7071// GotoBottom moves the viewport to the bottom of the log.72func (v *Viewport) GotoBottom() {73 v.Model.GotoBottom()74}7576// HalfViewDown moves the viewport down by half the viewport height.77func (v *Viewport) HalfViewDown() {78 v.Model.HalfPageDown()79}8081// HalfViewUp moves the viewport up by half the viewport height.82func (v *Viewport) HalfViewUp() {83 v.Model.HalfPageUp()84}8586// ScrollPercent returns the viewport's scroll percentage.87func (v *Viewport) ScrollPercent() float64 {88 return v.Model.ScrollPercent()89}