Skip to content

Instantly share code, notes, and snippets.

@pongo
Created March 19, 2026 10:38
Show Gist options
  • Select an option

  • Save pongo/e2e969f18f0801771f76265bd5b87fa0 to your computer and use it in GitHub Desktop.

Select an option

Save pongo/e2e969f18f0801771f76265bd5b87fa0 to your computer and use it in GitHub Desktop.
Go bubbletea file listing
package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"os"
"path/filepath"
)
type model struct {
files []string
cursor int
}
func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Обработка клавиш: up/down/q
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up":
if m.cursor > 0 {
m.cursor--
}
case "down":
if m.cursor < len(m.files)-1 {
m.cursor++
}
}
}
return m, nil
}
func (m model) View() string {
s := "Файлы (стрелки — прокрутка, q — выход):\n\n"
for i, file := range m.files {
cursor := " "
if i == m.cursor {
cursor = ">"
}
s += fmt.Sprintf("%s %s\n", cursor, file)
}
return s
}
func main() {
var files []string
filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
files = append(files, path)
return nil
})
p := tea.NewProgram(model{files: files})
p.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment