package main import ( "flag" "fmt" "io/ioutil" "log" "os" tea "github.com/charmbracelet/bubbletea" ) const help = `go-sh-manymanuals: ??? ??? Options: -h output help ` var portFlag int var helpFlag bool func handleCliFlags() { flag.BoolVar(&helpFlag, "h", false, "output help") flag.Parse() } type model struct { manuals []string cursor int } func initialModel() model { manuals, err := gatherManuals() if err != nil { log.Fatalf("unable to read manuals: %s", err) os.Exit(1) } return model{ manuals: manuals, } } func gatherManuals() ([]string, error) { fileInfos, err := ioutil.ReadDir(".") if err != nil { return nil, err } var filenames []string for _, fileInfo := range fileInfos { filenames = append(filenames, fileInfo.Name()) } return filenames, nil } func (m model) Init() tea.Cmd { return nil } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "ctrl+c", "q": return m, tea.Quit case "up", "k": if m.cursor > 0 { m.cursor-- } case "down", "j": if m.cursor < len(m.manuals)-1 { m.cursor++ } } } return m, nil } func (m model) View() string { ui := "File listing for current working directory\n\n" for i, choice := range m.manuals { cursor := " " if m.cursor == i { cursor = ">" } ui += fmt.Sprintf("%s %s\n", cursor, choice) } ui += "\nPress q to quit.\n" return ui } func main() { handleCliFlags() if helpFlag { fmt.Printf(help) os.Exit(0) } p := tea.NewProgram(initialModel(), tea.WithAltScreen()) if err := p.Start(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) os.Exit(1) } }