1package viewport
2
3import (
4 "github.com/charmbracelet/bubbles/v2/key"
5 "github.com/charmbracelet/bubbles/v2/viewport"
6 tea "github.com/charmbracelet/bubbletea/v2"
7 "github.com/charmbracelet/soft-serve/pkg/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()
19 vp.SetWidth(c.Width)
20 vp.SetHeight(c.Height)
21 vp.MouseWheelEnabled = true
22 return &Viewport{
23 common: c,
24 Model: &vp,
25 }
26}
27
28// 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}
34
35// Init implements tea.Model.
36func (v *Viewport) Init() tea.Cmd {
37 return nil
38}
39
40// Update implements tea.Model.
41func (v *Viewport) Update(msg tea.Msg) (tea.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 = &vp
53 return v, cmd
54}
55
56// View implements tea.Model.
57func (v *Viewport) View() string {
58 return v.Model.View()
59}
60
61// SetContent sets the viewport's content.
62func (v *Viewport) SetContent(content string) {
63 v.Model.SetContent(content)
64}
65
66// GotoTop moves the viewport to the top of the log.
67func (v *Viewport) GotoTop() {
68 v.Model.GotoTop()
69}
70
71// GotoBottom moves the viewport to the bottom of the log.
72func (v *Viewport) GotoBottom() {
73 v.Model.GotoBottom()
74}
75
76// HalfViewDown moves the viewport down by half the viewport height.
77func (v *Viewport) HalfViewDown() {
78 v.Model.HalfViewDown()
79}
80
81// HalfViewUp moves the viewport up by half the viewport height.
82func (v *Viewport) HalfViewUp() {
83 v.Model.HalfViewUp()
84}
85
86// ScrollPercent returns the viewport's scroll percentage.
87func (v *Viewport) ScrollPercent() float64 {
88 return v.Model.ScrollPercent()
89}