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