GoSetup-bubble/setup/list.go

140 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package setup
import (
"bubble/setup/ui"
"bubble/types"
"fmt"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ListApplicationItem struct {
Application types.PackageApplication
}
func (i ListApplicationItem) Title() string {
return fmt.Sprintf("%s, v%s", i.Application.Name, i.Application.Version)
}
func (i ListApplicationItem) Description() string {
return fmt.Sprintf("%s", i.Application.Description)
}
func (i ListApplicationItem) FilterValue() string {
return i.Application.Name
}
type ListApplicationsModel struct {
List list.Model
width int
height int
}
func NewListApplicationsModel(applications []types.PackageApplication, width, height int) *ListApplicationsModel {
items := make([]list.Item, len(applications))
for i, app := range applications {
items[i] = ListApplicationItem{app}
}
delegate := list.NewDefaultDelegate()
delegate.Styles.SelectedTitle = ui.ListSelectedStyle
delegate.Styles.SelectedDesc = ui.ListSelectedStyle
delegate.ShowDescription = false
l := list.New(items, delegate, width, height)
l.Title = "Выберите приложение для настройки"
l.Styles.Title = ui.ListTitleStyle
return &ListApplicationsModel{
List: l,
width: width,
height: height,
}
}
func (m *ListApplicationsModel) Init() tea.Cmd {
return nil
}
func (m *ListApplicationsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "enter":
selected := m.List.SelectedItem().(ListApplicationItem)
return m, func() tea.Msg {
return types.ShowModalMsg{
Type: types.ModalConfirm,
Title: "Начать установку",
Message: fmt.Sprintf("Вы хотит настроить приложение:\n\n%s\n\nПродолжить?", selected.Title()),
ModalButtons: []types.ModalButton{
{Text: "Да", Value: true},
{Text: "Нет", Value: false},
},
OnResult: func(result any) tea.Cmd {
if result == true {
return func() tea.Msg {
return types.SelectedAppMsg{App: selected.Application}
}
}
return nil
},
}
}
}
}
m.List, cmd = m.List.Update(msg)
return m, cmd
}
func (m *ListApplicationsModel) View() string {
contentWidth := ui.ContentWidth(m.width)
listWidth := int(float64(contentWidth) * ui.ListWithRatio)
m.List.SetSize(listWidth-6, m.height-10)
listView := m.List.View()
var desc string
if sel := m.List.SelectedItem(); sel != nil {
desc = sel.(ListApplicationItem).Description()
}
descView := ui.ListDescriptionStyle.Width(contentWidth - listWidth - 10).Render(desc)
body := lipgloss.JoinHorizontal(lipgloss.Top, listView, " ", descView)
container := ui.ListContainer.Width(contentWidth).Render(body)
return lipgloss.Place(m.width, m.height,
lipgloss.Center, lipgloss.Center,
container,
lipgloss.WithWhitespaceChars("░"),
lipgloss.WithWhitespaceForeground(ui.ColorGrey),
)
//var desc string
//if selected := m.List.SelectedItem(); selected != nil {
// desc = selected.(ListApplicationItem).Description()
//}
//descView := ui.ListDescriptionStyle.
// Width(contentWidth/2 - 4).
// Render(desc)
//
//body := lipgloss.JoinHorizontal(lipgloss.Top, listView, " ", descView)
//
//container := ui.ListStyle.
// Width(contentWidth).
// Render(body)
//
//return lipgloss.Place(m.width, m.height,
// lipgloss.Center, lipgloss.Center,
// container,
// lipgloss.WithWhitespaceChars("░"),
// lipgloss.WithWhitespaceForeground(ui.ColorGrey),
//)
}