2025-12-26 17:45:37 +05:00
|
|
|
|
package setup
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bubble/setup/ui"
|
|
|
|
|
|
"bubble/types"
|
2025-12-29 17:46:48 +05:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
2025-12-26 17:45:37 +05:00
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
2026-01-14 18:08:43 +05:00
|
|
|
|
"github.com/sirupsen/logrus"
|
2025-12-26 17:45:37 +05:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-29 17:46:48 +05:00
|
|
|
|
type ListApplicationItem struct {
|
|
|
|
|
|
Application types.PackageApplication
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 17:46:48 +05:00
|
|
|
|
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))
|
2025-12-26 17:45:37 +05:00
|
|
|
|
for i, app := range applications {
|
2025-12-29 17:46:48 +05:00
|
|
|
|
items[i] = ListApplicationItem{app}
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
2025-12-29 17:46:48 +05:00
|
|
|
|
|
|
|
|
|
|
delegate := list.NewDefaultDelegate()
|
|
|
|
|
|
delegate.Styles.SelectedTitle = ui.ListSelectedStyle
|
|
|
|
|
|
delegate.Styles.SelectedDesc = ui.ListSelectedStyle
|
|
|
|
|
|
delegate.ShowDescription = false
|
|
|
|
|
|
l := list.New(items, delegate, width, height)
|
2026-01-14 18:08:43 +05:00
|
|
|
|
l.Title = "Приложения"
|
2025-12-29 17:46:48 +05:00
|
|
|
|
l.Styles.Title = ui.ListTitleStyle
|
2026-01-14 18:08:43 +05:00
|
|
|
|
l.SetShowHelp(false)
|
2025-12-29 17:46:48 +05:00
|
|
|
|
return &ListApplicationsModel{
|
|
|
|
|
|
List: l,
|
|
|
|
|
|
width: width,
|
|
|
|
|
|
height: height,
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 17:46:48 +05:00
|
|
|
|
func (m *ListApplicationsModel) Init() tea.Cmd {
|
2025-12-26 17:45:37 +05:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 17:46:48 +05:00
|
|
|
|
func (m *ListApplicationsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2025-12-26 17:45:37 +05:00
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
switch msg := msg.(type) {
|
2025-12-29 17:46:48 +05:00
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
|
|
m.width = msg.Width
|
|
|
|
|
|
m.height = msg.Height
|
|
|
|
|
|
return m, nil
|
2025-12-26 17:45:37 +05:00
|
|
|
|
case tea.KeyMsg:
|
|
|
|
|
|
switch msg.String() {
|
|
|
|
|
|
case "enter":
|
2025-12-29 17:46:48 +05:00
|
|
|
|
selected := m.List.SelectedItem().(ListApplicationItem)
|
2025-12-26 17:45:37 +05:00
|
|
|
|
return m, func() tea.Msg {
|
2025-12-29 17:46:48 +05:00
|
|
|
|
return types.ShowModalMsg{
|
|
|
|
|
|
Type: types.ModalConfirm,
|
|
|
|
|
|
Title: "Начать установку",
|
2026-01-14 18:08:43 +05:00
|
|
|
|
Message: fmt.Sprintf("Вы хотите настроить приложение:\n\n%s\n\nПродолжить?", selected.Title()),
|
2025-12-29 17:46:48 +05:00
|
|
|
|
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
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-29 17:46:48 +05:00
|
|
|
|
m.List, cmd = m.List.Update(msg)
|
2025-12-26 17:45:37 +05:00
|
|
|
|
return m, cmd
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 17:46:48 +05:00
|
|
|
|
func (m *ListApplicationsModel) View() string {
|
2026-01-14 18:08:43 +05:00
|
|
|
|
gapStroke := " "
|
|
|
|
|
|
containerWidth, containerHeight, contentWidth, contentHeight := ui.ContentSizes(m.width, m.height)
|
|
|
|
|
|
|
|
|
|
|
|
listWidth := int(float64(contentWidth)*ui.ListWithRatio) - 2*1
|
|
|
|
|
|
//ширина листа = пропорция от ширины контента - границы для контейнера листа
|
|
|
|
|
|
listHeight := contentHeight - 2*1
|
|
|
|
|
|
//высота листа = высота контента - границы для контейнера листа
|
|
|
|
|
|
descWidth := int(float64(contentWidth)*(1-ui.ListWithRatio)) - 2*1 - len(gapStroke)
|
|
|
|
|
|
//ширина описания = обратная пропорция листа от ширины контента - границы для контейнера описания - отступ от контейнера листа
|
|
|
|
|
|
descHeight := contentHeight - 2*1
|
|
|
|
|
|
//высота листа = высота контента - границы для контейнера описания
|
|
|
|
|
|
|
|
|
|
|
|
m.List.SetSize(listWidth, listHeight)
|
|
|
|
|
|
|
|
|
|
|
|
//logs.go logrus.DebugLevel set
|
|
|
|
|
|
logrus.Debugf(
|
|
|
|
|
|
"window: width %d, height %d\ncontainer: width %d, height %d\ncontent: width %d, height %d\nlist: width %d, height %d\ndesc: width %d, height %d\n\n",
|
|
|
|
|
|
m.width, m.height,
|
|
|
|
|
|
containerWidth, containerHeight,
|
|
|
|
|
|
contentWidth, contentHeight,
|
|
|
|
|
|
listWidth, listHeight,
|
|
|
|
|
|
descWidth, descHeight,
|
|
|
|
|
|
)
|
2025-12-29 17:46:48 +05:00
|
|
|
|
|
2026-01-14 18:08:43 +05:00
|
|
|
|
listView := lipgloss.NewStyle().
|
|
|
|
|
|
Width(listWidth).
|
|
|
|
|
|
Height(listHeight).
|
|
|
|
|
|
Border(lipgloss.RoundedBorder()).
|
|
|
|
|
|
BorderForeground(ui.ColorPurple).
|
|
|
|
|
|
AlignVertical(lipgloss.Top).
|
|
|
|
|
|
Render(m.List.View())
|
2025-12-29 17:46:48 +05:00
|
|
|
|
|
|
|
|
|
|
var desc string
|
|
|
|
|
|
if sel := m.List.SelectedItem(); sel != nil {
|
|
|
|
|
|
desc = sel.(ListApplicationItem).Description()
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
2025-12-29 17:46:48 +05:00
|
|
|
|
|
2026-01-14 18:08:43 +05:00
|
|
|
|
descView := ui.ListDescriptionStyle.
|
|
|
|
|
|
Width(descWidth).
|
|
|
|
|
|
Height(descHeight).
|
|
|
|
|
|
Border(lipgloss.RoundedBorder()).
|
|
|
|
|
|
BorderForeground(ui.ColorError).
|
|
|
|
|
|
AlignVertical(lipgloss.Top).
|
|
|
|
|
|
Render(
|
|
|
|
|
|
desc,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
body := lipgloss.JoinHorizontal(lipgloss.Center, listView, gapStroke, descView)
|
|
|
|
|
|
container := ui.AppContainer.
|
|
|
|
|
|
Width(containerWidth).
|
|
|
|
|
|
Height(containerHeight).
|
|
|
|
|
|
Render(body)
|
|
|
|
|
|
return lipgloss.Place(
|
|
|
|
|
|
m.width, m.height,
|
2025-12-29 17:46:48 +05:00
|
|
|
|
lipgloss.Center, lipgloss.Center,
|
|
|
|
|
|
container,
|
|
|
|
|
|
lipgloss.WithWhitespaceChars("░"),
|
2026-01-14 18:08:43 +05:00
|
|
|
|
lipgloss.WithWhitespaceForeground(ui.ColorWarning),
|
2025-12-29 17:46:48 +05:00
|
|
|
|
)
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|