-
Notifications
You must be signed in to change notification settings - Fork 744
/
view.go
150 lines (123 loc) · 3.98 KB
/
view.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package selection
import (
"fmt"
"io"
"log"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/daytonaio/daytona/cmd/daytona/config"
"github.com/daytonaio/daytona/pkg/views"
view_util "github.com/daytonaio/daytona/pkg/views/util"
)
var CustomRepoIdentifier = "<CUSTOM_REPO>"
type item[T any] struct {
id, title, desc, createdTime, uptime, target string
choiceProperty T
}
func (i item[T]) Title() string { return i.title }
func (i item[T]) Id() string { return i.id }
func (i item[T]) Description() string { return i.desc }
func (i item[T]) FilterValue() string { return i.title }
func (i item[T]) CreatedTime() string { return i.createdTime }
func (i item[T]) Uptime() string { return i.uptime }
func (i item[T]) Target() string { return i.target }
type model[T any] struct {
list list.Model
choice *T
footer string
}
func (m model[T]) Init() tea.Cmd {
return nil
}
func (m model[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "q", "ctrl+c":
return m, tea.Quit
case "enter":
i, ok := m.list.SelectedItem().(item[T])
if ok {
m.choice = &i.choiceProperty
}
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := view_util.DocStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model[T]) View() string {
if m.footer == "" {
c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}
activeProfile, err := c.GetActiveProfile()
if err != nil {
log.Fatal(err)
}
m.footer = view_util.GetListFooter(activeProfile.Name)
}
return view_util.DocStyle.Render(m.list.View() + m.footer)
}
type ItemDelegate[T any] struct {
}
func (d ItemDelegate[T]) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, _ := listItem.(item[T]) // Cast the listItem to your custom item type
s := strings.Builder{}
var isSelected = index == m.Index()
baseStyles := lipgloss.NewStyle().Padding(0, 0, 0, 2)
title := baseStyles.Copy().Render(i.Title())
idWithTargetString := fmt.Sprintf("%s (%s)", i.Id(), i.Target())
idWithTarget := baseStyles.Copy().Foreground(views.Gray).Render(idWithTargetString)
description := baseStyles.Copy().Render(i.Description())
// Add the created/updated time if it's available
timeWidth := m.Width() - baseStyles.GetHorizontalFrameSize() - lipgloss.Width(title)
timeStyles := lipgloss.NewStyle().
Align(lipgloss.Right).
Width(timeWidth)
timeString := timeStyles.Render("")
if i.Uptime() != "" {
timeString = timeStyles.Render(i.Uptime())
} else if i.CreatedTime() != "" {
timeString = timeStyles.Render(fmt.Sprintf("created %s", i.CreatedTime()))
}
// Adjust styles as the user moves through the menu
if isSelected {
selectedStyles := lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, false, false, true).
BorderForeground(views.Blue).
Bold(true).
Padding(0, 0, 0, 1)
title = selectedStyles.Copy().Foreground(views.Blue).Render(i.Title())
idWithTarget = selectedStyles.Copy().Foreground(views.Gray).Render(idWithTargetString)
description = selectedStyles.Copy().Foreground(views.DimmedBlue).Render(i.Description())
timeString = timeStyles.Copy().Foreground(views.DimmedBlue).Render(timeString)
}
// Render to the terminal
s.WriteString(lipgloss.JoinHorizontal(lipgloss.Bottom, title, timeString))
s.WriteRune('\n')
s.WriteString(idWithTarget)
s.WriteRune('\n')
s.WriteString(description)
s.WriteRune('\n')
fmt.Fprint(w, s.String())
}
func (d ItemDelegate[T]) Height() int {
height := lipgloss.NewStyle().GetVerticalFrameSize() + 4
return height
}
func (d ItemDelegate[T]) Spacing() int {
return 0
}
func (d ItemDelegate[T]) Update(msg tea.Msg, m *list.Model) tea.Cmd {
return nil
}