-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtui_details.go
111 lines (93 loc) · 2.54 KB
/
tui_details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
//nolint:gochecknoglobals
var detailsViewStyle = lipgloss.NewStyle().Padding(1, 4)
func newDetailsView() DetailsView {
viewportModel := viewport.New(0, 0)
helpModel := help.New()
return DetailsView{
viewportModel: viewportModel,
helpModel: helpModel,
}
}
var _ help.KeyMap = DetailsView{}
type DetailsView struct {
viewportModel viewport.Model
helpModel help.Model
}
func (d DetailsView) ShortHelp() []key.Binding {
return []key.Binding{
key.NewBinding(
key.WithHelp("↑/k", "up"),
),
key.NewBinding(
key.WithHelp("↓/j", "down"),
),
key.NewBinding(
key.WithHelp("v", "hide details"),
),
key.NewBinding(
key.WithHelp("q", "quit"),
),
}
}
func (d DetailsView) FullHelp() [][]key.Binding {
return [][]key.Binding{d.ShortHelp()}
}
func (d DetailsView) Init() tea.Cmd {
return nil
}
func (d DetailsView) Update(msg tea.Msg) (DetailsView, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
occupiedHorizontal, occupiedVertical := d.getOccupiedSize()
d.viewportModel.Width = msg.Width - occupiedHorizontal
d.viewportModel.Height = msg.Height - occupiedVertical
return d, nil
case viewPullRequestDetails:
d.viewportModel.SetContent(d.viewPullRequest(msg.pr))
case hidePullRequestDetails:
d.viewportModel.SetContent("")
case tea.KeyMsg:
if msg.String() == "v" {
cmd = hidePullRequestDetailsCmd()
}
}
nextViewport, viewportCmd := d.viewportModel.Update(msg)
nextHelp, helpCmd := d.helpModel.Update(msg)
d.viewportModel = nextViewport
d.helpModel = nextHelp
return d, tea.Batch(cmd, viewportCmd, helpCmd)
}
func (d DetailsView) View() string {
content := d.viewportModel.View()
h := d.helpModel.View(d)
return detailsViewStyle.Render(lipgloss.JoinVertical(
lipgloss.Top,
content,
h,
))
}
func (d DetailsView) viewPullRequest(pr pullRequest) string {
content := lipgloss.NewStyle().
Background(lipgloss.Color("62")).
Foreground(lipgloss.Color("230")).
Padding(0, 1).Render(pr.Title())
content += "\n\n"
content += pr.Description()
content += "\n\n---\n\n"
content += pr.bodyText
return content
}
func (d DetailsView) getOccupiedSize() (int, int) {
horizontal := detailsViewStyle.GetHorizontalFrameSize()
vertical := detailsViewStyle.GetVerticalFrameSize() + lipgloss.Height(d.helpModel.View(d))
return horizontal, vertical
}