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