-
Notifications
You must be signed in to change notification settings - Fork 542
/
view.go
66 lines (54 loc) · 1.38 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
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package provider
import (
"os"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/daytonaio/daytona/pkg/serverapiclient"
"github.com/daytonaio/daytona/pkg/views"
"golang.org/x/term"
)
type item struct {
provider serverapiclient.Provider
}
func (i item) Title() string { return *i.provider.Name }
func (i item) Description() string {
return *i.provider.Version
}
func (i item) FilterValue() string { return *i.provider.Name }
type model struct {
list list.Model
choice *serverapiclient.Provider
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl+c":
return m, tea.Quit
case "enter":
i, ok := m.list.SelectedItem().(item)
if ok {
m.choice = &i.provider
}
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := views.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) View() string {
terminalWidth, terminalHeight, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return ""
}
return views.DocStyle.Width(terminalWidth - 4).Height(terminalHeight - 4).Render(m.list.View())
}