1package repo
2
3import (
4 "fmt"
5
6 "github.com/charmbracelet/bubbles/key"
7 tea "github.com/charmbracelet/bubbletea"
8 "github.com/charmbracelet/soft-serve/ui/common"
9 "github.com/charmbracelet/soft-serve/ui/components/code"
10 "github.com/charmbracelet/soft-serve/ui/git"
11)
12
13// ReadmeMsg is a message sent when the readme is loaded.
14type ReadmeMsg struct {
15 Msg tea.Msg
16}
17
18// Readme is the readme component page.
19type Readme struct {
20 common common.Common
21 code *code.Code
22 ref RefMsg
23 repo *git.Repository
24}
25
26// NewReadme creates a new readme model.
27func NewReadme(common common.Common) *Readme {
28 readme := code.New(common, "", "")
29 readme.NoContentStyle = readme.NoContentStyle.SetString("No readme found.")
30 return &Readme{
31 code: readme,
32 common: common,
33 }
34}
35
36// SetSize implements common.Component.
37func (r *Readme) SetSize(width, height int) {
38 r.common.SetSize(width, height)
39 r.code.SetSize(width, height)
40}
41
42// ShortHelp implements help.KeyMap.
43func (r *Readme) ShortHelp() []key.Binding {
44 b := []key.Binding{
45 r.common.KeyMap.UpDown,
46 }
47 return b
48}
49
50// FullHelp implements help.KeyMap.
51func (r *Readme) FullHelp() [][]key.Binding {
52 k := r.code.KeyMap
53 b := [][]key.Binding{
54 {
55 k.PageDown,
56 k.PageUp,
57 k.HalfPageDown,
58 k.HalfPageUp,
59 },
60 {
61 k.Down,
62 k.Up,
63 },
64 }
65 return b
66}
67
68// Init implements tea.Model.
69func (r *Readme) Init() tea.Cmd {
70 return r.updateReadmeCmd
71}
72
73// Update implements tea.Model.
74func (r *Readme) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
75 cmds := make([]tea.Cmd, 0)
76 switch msg := msg.(type) {
77 case RepoMsg:
78 r.repo = msg
79 case RefMsg:
80 r.ref = msg
81 cmds = append(cmds, r.Init())
82 }
83 c, cmd := r.code.Update(msg)
84 r.code = c.(*code.Code)
85 if cmd != nil {
86 cmds = append(cmds, cmd)
87 }
88 return r, tea.Batch(cmds...)
89}
90
91// View implements tea.Model.
92func (r *Readme) View() string {
93 return r.code.View()
94}
95
96// StatusBarValue implements statusbar.StatusBar.
97func (r *Readme) StatusBarValue() string {
98 return ""
99}
100
101// StatusBarInfo implements statusbar.StatusBar.
102func (r *Readme) StatusBarInfo() string {
103 return fmt.Sprintf("☰ %.f%%", r.code.ScrollPercent()*100)
104}
105
106func (r *Readme) updateReadmeCmd() tea.Msg {
107 m := ReadmeMsg{}
108 if r.repo == nil {
109 return common.ErrorCmd(git.ErrMissingRepo)
110 }
111 rm, rp := r.repo.Readme()
112 r.code.GotoTop()
113 cmd := r.code.SetContent(rm, rp)
114 if cmd != nil {
115 m.Msg = cmd()
116 }
117 return m
118}