all repos — aflux @ main

main.go (view raw)

 1package main
 2
 3import (
 4	"flag"
 5	"fmt"
 6	"os"
 7	"sort"
 8	"time"
 9
10	tea "github.com/charmbracelet/bubbletea"
11	goFeed "github.com/mmcdole/gofeed"
12)
13
14type model struct {
15	url      string
16	sortAsc  bool
17	showDesc bool
18	feed     goFeed.Feed
19}
20
21func (model model) Init() tea.Cmd {
22	// Just return `nil`, which means "no I/O right now, please."
23	return nil
24}
25
26func (model model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
27	switch msg := msg.(type) {
28	case tea.KeyMsg:
29		switch msg.String() {
30		case "ctrl+c", "q":
31			return model, tea.Quit
32		case "r":
33			model.feed = getFeed(model.url)
34		case "d":
35			model.showDesc = !model.showDesc
36		case "s":
37			model.sortAsc = !model.sortAsc
38
39		}
40	}
41	return model, nil
42}
43
44func (model model) View() string {
45	output := fmt.Sprintf("%t %t\n", model.showDesc, model.sortAsc)
46	output = fmt.Sprintf("%s%s\n\n", output, time.Now().String())
47
48	if model.sortAsc {
49		sort.Sort(model.feed)
50	} else {
51		sort.Sort(sort.Reverse(model.feed))
52	}
53
54	for _, item := range model.feed.Items {
55		output = fmt.Sprintf("%s%s", output, renderFeedItem(item, model.showDesc))
56	}
57	return output
58}
59
60func main() {
61	url := os.Getenv("ATOM_URL")
62	showTui := flag.Bool("t", false, "TUI mode")
63	sortAsc := flag.Bool("s", false, "Sort from oldest to newest")
64	showDesc := flag.Bool("d", false, "Show description for each event")
65	flag.Parse()
66
67	if url == "" {
68		url = "https://5ika.ch/posts/index.xml"
69	}
70
71	m := model{
72		url:      url,
73		sortAsc:  *sortAsc,
74		showDesc: *showDesc,
75		feed:     getFeed(url),
76	}
77
78	if *showTui {
79		p := tea.NewProgram(m, tea.WithAltScreen())
80
81		if _, err := p.Run(); err != nil {
82			fmt.Printf("Alas, there's been an error: %v", err)
83			os.Exit(1)
84		}
85	} else {
86		output := m.View()
87		fmt.Print(output)
88	}
89}