chore(launcher): improve commenting
This commit is contained in:
parent
f1ab5eb07b
commit
4391530fad
1 changed files with 31 additions and 1 deletions
|
@ -1,5 +1,4 @@
|
|||
// warning this code is messy and terrible
|
||||
// TODO read list data from file
|
||||
package launcher
|
||||
|
||||
import (
|
||||
|
@ -15,6 +14,8 @@ import (
|
|||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
// ===============
|
||||
// lipgloss styles
|
||||
var appStyle = lipgloss.NewStyle().Margin(1, 2)
|
||||
|
||||
var titleStyle = lipgloss.NewStyle().
|
||||
|
@ -27,6 +28,8 @@ var categoryTitleStyle = lipgloss.NewStyle().
|
|||
Background(lipgloss.Color("4")).
|
||||
Padding(0, 1)
|
||||
|
||||
// ============
|
||||
// data structs
|
||||
type Category struct {
|
||||
title string
|
||||
desc string
|
||||
|
@ -47,6 +50,8 @@ func (p Program) Title() string { return p.title }
|
|||
func (p Program) Description() string { return p.desc }
|
||||
func (p Program) FilterValue() string { return p.title }
|
||||
|
||||
// ======================
|
||||
// bubbletea model struct
|
||||
type model struct {
|
||||
categoriesList list.Model
|
||||
categoriesKeys *categoriesKeyMap
|
||||
|
@ -56,6 +61,8 @@ type model struct {
|
|||
shouldExec string
|
||||
}
|
||||
|
||||
// =============
|
||||
// list keybinds
|
||||
type categoriesKeyMap struct {
|
||||
selectKey key.Binding
|
||||
}
|
||||
|
@ -87,6 +94,8 @@ func newProgramsKeyMap() *programsKeyMap {
|
|||
}
|
||||
}
|
||||
|
||||
// =======================
|
||||
// bubbletea model methods
|
||||
func (m model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
@ -141,7 +150,13 @@ func (m model) View() string {
|
|||
}
|
||||
}
|
||||
|
||||
// ============
|
||||
// run launcher
|
||||
func Run(cmd *cobra.Command, args []string) error {
|
||||
// =========
|
||||
// init data
|
||||
|
||||
// TODO read this from a file instead
|
||||
categories := []list.Item{
|
||||
Category{
|
||||
title: "editors",
|
||||
|
@ -213,11 +228,15 @@ func Run(cmd *cobra.Command, args []string) error {
|
|||
},
|
||||
}
|
||||
|
||||
// =============================
|
||||
// create delegates and keybinds
|
||||
categoryDelegate := list.NewDefaultDelegate()
|
||||
categoryKeys := newCategoriesKeyMap()
|
||||
programDelegate := list.NewDefaultDelegate()
|
||||
programKeys := newProgramsKeyMap()
|
||||
|
||||
// ===================
|
||||
// set delegate styles
|
||||
categoryDelegate.Styles.SelectedTitle = categoryDelegate.Styles.SelectedTitle.
|
||||
Foreground(lipgloss.Color("4")).BorderLeftForeground(lipgloss.Color("4"))
|
||||
categoryDelegate.Styles.SelectedDesc = categoryDelegate.Styles.SelectedTitle
|
||||
|
@ -226,6 +245,8 @@ func Run(cmd *cobra.Command, args []string) error {
|
|||
Foreground(lipgloss.Color("1")).BorderLeftForeground(lipgloss.Color("1"))
|
||||
programDelegate.Styles.SelectedDesc = programDelegate.Styles.SelectedTitle
|
||||
|
||||
// ====================
|
||||
// define initial model
|
||||
initialModel := model{
|
||||
categoriesList: list.New(categories, categoryDelegate, 0, 0),
|
||||
categoriesKeys: categoryKeys,
|
||||
|
@ -234,6 +255,8 @@ func Run(cmd *cobra.Command, args []string) error {
|
|||
selectedCategory: false,
|
||||
}
|
||||
|
||||
// =================================
|
||||
// set initial model styles and keys
|
||||
initialModel.categoriesList.Title = "the cherry.town program directory"
|
||||
initialModel.categoriesList.Styles.Title = titleStyle
|
||||
initialModel.categoriesList.AdditionalShortHelpKeys = func() []key.Binding {
|
||||
|
@ -261,6 +284,8 @@ func Run(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
// =====================
|
||||
// run bubbletea program
|
||||
p := tea.NewProgram(initialModel)
|
||||
finalModel, err := p.Run()
|
||||
if err != nil {
|
||||
|
@ -268,11 +293,16 @@ func Run(cmd *cobra.Command, args []string) error {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
// ============
|
||||
// clear screen
|
||||
|
||||
// TODO this feels hacky and should probably be replaced
|
||||
execCmd := exec.Command("clear")
|
||||
execCmd.Stdout = os.Stdout
|
||||
execCmd.Run()
|
||||
|
||||
// ====================================
|
||||
// execute chosen command if one is set
|
||||
if finalModel, ok := finalModel.(model); ok && finalModel.shouldExec != "" {
|
||||
execCmd := exec.Command(finalModel.shouldExec)
|
||||
execCmd.Stdin = os.Stdin
|
||||
|
|
Loading…
Add table
Reference in a new issue