package main import ( "flag" "fmt" "os" "sort" "time" tea "github.com/charmbracelet/bubbletea" goFeed "github.com/mmcdole/gofeed" ) type model struct { url string sortAsc bool showDesc bool feed goFeed.Feed } func (model model) Init() tea.Cmd { // Just return `nil`, which means "no I/O right now, please." return nil } func (model model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "ctrl+c", "q": return model, tea.Quit case "r": model.feed = getFeed(model.url) case "d": model.showDesc = !model.showDesc case "s": model.sortAsc = !model.sortAsc } } return model, nil } func (model model) View() string { output := fmt.Sprintf("%t %t\n", model.showDesc, model.sortAsc) output = fmt.Sprintf("%s%s\n\n", output, time.Now().String()) if model.sortAsc { sort.Sort(model.feed) } else { sort.Sort(sort.Reverse(model.feed)) } for _, item := range model.feed.Items { output = fmt.Sprintf("%s%s", output, renderFeedItem(item, model.showDesc)) } return output } func main() { url := os.Getenv("ATOM_URL") showTui := flag.Bool("t", false, "TUI mode") sortAsc := flag.Bool("s", false, "Sort from oldest to newest") showDesc := flag.Bool("d", false, "Show description for each event") flag.Parse() if url == "" { url = "https://5ika.ch/posts/index.xml" } m := model{ url: url, sortAsc: *sortAsc, showDesc: *showDesc, feed: getFeed(url), } if *showTui { p := tea.NewProgram(m, tea.WithAltScreen()) if _, err := p.Run(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) os.Exit(1) } } else { output := m.View() fmt.Print(output) } }