95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package steps
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"miasma-installer/tui/styles"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type Disk struct {
|
|
name, description string
|
|
}
|
|
|
|
func (i Disk) FilterValue() string { return i.name }
|
|
func (i Disk) Title() string { return i.name }
|
|
func (i Disk) Description() string { return i.description }
|
|
|
|
type ItemDelegate struct{}
|
|
|
|
func (i ItemDelegate) Height() int { return 1 }
|
|
func (i ItemDelegate) Width() int { return 0 }
|
|
func (i ItemDelegate) Update(msg tea.Msg, m list.Model) tea.Cmd { return nil }
|
|
func (i ItemDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
|
|
i, ok := item.(Disk)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
s := fmt.Sprintf("%d. %s - %s", index+1, i.Title(), i.Description())
|
|
|
|
// focus styles
|
|
if index == m.Index() {
|
|
s = styles.ActiveItemStyle.Render("> " + s)
|
|
} else {
|
|
s = styles.InactiveItemStyle.Render(" " + s)
|
|
}
|
|
fmt.Fprint(w, s)
|
|
}
|
|
|
|
type DiskSelModel struct {
|
|
list list.Model
|
|
Finished bool
|
|
SelectedDisk string
|
|
}
|
|
|
|
func NewDiskSelModel(width, height int) DiskSelModel {
|
|
// This is a placeholder - I need to update this to a tea.Cmd to scan the system for drives
|
|
disks := []list.Item{
|
|
Disk{name: "/dev/sda", description: "500GB Samsung NVME"},
|
|
Disk{name: "/dev/sdb", description: "1TB Baracuda HDD"},
|
|
Disk{name: "/dev/loop0", description: "2GB Fanxiang SSD"},
|
|
}
|
|
|
|
l := list.New(disks, itemDelegate{}, width, height-5)
|
|
l.Title = styles.TitleStyle.Render("Select Installation Disk")
|
|
l.SetShowStatusBar(false)
|
|
l.SetFilterEnabled(false)
|
|
l.Styles.Title = styles.TitleStyle
|
|
l.KeyMap.Unbind()
|
|
|
|
return DiskSelModel{list: l}
|
|
}
|
|
|
|
func (m DiskSelModel) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m DiskSelModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
if m.Finished {
|
|
return m, nil
|
|
}
|
|
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
if msg.String() == "enter" {
|
|
if item, ok := m.list.SelectedItem().(Disk); ok {
|
|
m.SelectedDisk = item.name
|
|
m.Finished = true
|
|
return m, nil
|
|
}
|
|
}
|
|
}
|
|
var cmd tea.Cmd
|
|
m.list, cmd = m.list.Update(msg)
|
|
return m, cmd
|
|
}
|
|
|
|
func (m DiskSelModel) View() string {
|
|
s := m.list.View()
|
|
s += "\n" + styles.HelpStyle.Render("Use ↑/↓ to navigate, [Enter] to select disk.")
|
|
return s
|
|
}
|