1package viewport
  2
  3import (
  4	"github.com/charmbracelet/bubbles/key"
  5	"github.com/charmbracelet/bubbles/viewport"
  6	tea "github.com/charmbracelet/bubbletea"
  7	"github.com/charmbracelet/soft-serve/server/ui/common"
  8)
  9
 10// Viewport represents a viewport component.
 11type Viewport struct {
 12	common common.Common
 13	*viewport.Model
 14}
 15
 16// New returns a new Viewport.
 17func New(c common.Common) *Viewport {
 18	vp := viewport.New(c.Width, c.Height)
 19	vp.MouseWheelEnabled = true
 20	return &Viewport{
 21		common: c,
 22		Model:  &vp,
 23	}
 24}
 25
 26// SetSize implements common.Component.
 27func (v *Viewport) SetSize(width, height int) {
 28	v.common.SetSize(width, height)
 29	v.Model.Width = width
 30	v.Model.Height = height
 31}
 32
 33// Init implements tea.Model.
 34func (v *Viewport) Init() tea.Cmd {
 35	return nil
 36}
 37
 38// Update implements tea.Model.
 39func (v *Viewport) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 40	switch msg := msg.(type) {
 41	case tea.KeyMsg:
 42		switch {
 43		case key.Matches(msg, v.common.KeyMap.GotoTop):
 44			v.GotoTop()
 45		case key.Matches(msg, v.common.KeyMap.GotoBottom):
 46			v.GotoBottom()
 47		}
 48	}
 49	vp, cmd := v.Model.Update(msg)
 50	v.Model = &vp
 51	return v, cmd
 52}
 53
 54// View implements tea.Model.
 55func (v *Viewport) View() string {
 56	return v.Model.View()
 57}
 58
 59// SetContent sets the viewport's content.
 60func (v *Viewport) SetContent(content string) {
 61	v.Model.SetContent(content)
 62}
 63
 64// GotoTop moves the viewport to the top of the log.
 65func (v *Viewport) GotoTop() {
 66	v.Model.GotoTop()
 67}
 68
 69// GotoBottom moves the viewport to the bottom of the log.
 70func (v *Viewport) GotoBottom() {
 71	v.Model.GotoBottom()
 72}
 73
 74// HalfViewDown moves the viewport down by half the viewport height.
 75func (v *Viewport) HalfViewDown() {
 76	v.Model.HalfViewDown()
 77}
 78
 79// HalfViewUp moves the viewport up by half the viewport height.
 80func (v *Viewport) HalfViewUp() {
 81	v.Model.HalfViewUp()
 82}
 83
 84// ViewUp moves the viewport up by a page.
 85func (v *Viewport) ViewUp() []string {
 86	return v.Model.ViewUp()
 87}
 88
 89// ViewDown moves the viewport down by a page.
 90func (v *Viewport) ViewDown() []string {
 91	return v.Model.ViewDown()
 92}
 93
 94// LineUp moves the viewport up by the given number of lines.
 95func (v *Viewport) LineUp(n int) []string {
 96	return v.Model.LineUp(n)
 97}
 98
 99// LineDown moves the viewport down by the given number of lines.
100func (v *Viewport) LineDown(n int) []string {
101	return v.Model.LineDown(n)
102}
103
104// ScrollPercent returns the viewport's scroll percentage.
105func (v *Viewport) ScrollPercent() float64 {
106	return v.Model.ScrollPercent()
107}