diff --git a/cmd/keys.go b/cmd/keys.go index a8cb8c8..8e6d9b5 100644 --- a/cmd/keys.go +++ b/cmd/keys.go @@ -1,3 +1,4 @@ +// Package cmd contains all the command functions package cmd import ( @@ -80,7 +81,7 @@ var keys = keyMap{ ), } -func (m model) handleKeys(msg tea.KeyMsg) (model, tea.Cmd) { +func (m Model) handleKeys(msg tea.KeyMsg) (Model, tea.Cmd) { switch { case key.Matches(msg, m.keys.Help): m.help.ShowAll = !m.help.ShowAll diff --git a/cmd/load.go b/cmd/load.go index c43a406..be93c97 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -10,7 +10,7 @@ import ( ) // load the home view, this conists of the list of feeds -func (m model) loadHome() model { +func (m Model) loadHome() Model { columns := []table.Column{ {Title: "Unread", Width: 7}, {Title: "Title", Width: m.table.Width() - 7}, @@ -28,7 +28,7 @@ func (m model) loadHome() model { return m } -func (m model) loadContent(id int) model { +func (m Model) loadContent(id int) Model { feed := m.feeds[id] feed.ID = id @@ -54,7 +54,7 @@ func (m model) loadContent(id int) model { return m } -func (m model) loadSearch() model { +func (m Model) loadSearch() Model { m.context = "search" m.table.Blur() @@ -65,7 +65,7 @@ func (m model) loadSearch() model { return m } -func (m model) loadSearchValues() model { +func (m Model) loadSearchValues() Model { search := m.filter.Value() var filteredPosts []lib.Post @@ -95,7 +95,7 @@ func (m model) loadSearchValues() model { return m } -func (m model) loadNewTable(columns []table.Column, rows []table.Row) model { +func (m Model) loadNewTable(columns []table.Column, rows []table.Row) Model { t := &m.table // NOTE: clear the rows first to prevent panic @@ -110,7 +110,7 @@ func (m model) loadNewTable(columns []table.Column, rows []table.Row) model { return m } -func (m model) loadReader() model { +func (m Model) loadReader() Model { id := m.table.Cursor() post := m.feed.Posts[id] post.ID = id diff --git a/cmd/main.go b/cmd/main.go index 217e6eb..ab10e52 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,7 +10,8 @@ import ( "github.com/isabelroses/izrss/lib" ) -func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +// Update will regnerate the model on each run +func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var ( cmd tea.Cmd cmds []tea.Cmd @@ -30,7 +31,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Batch(cmds...) } -func (m model) handleWindowSize(msg tea.WindowSizeMsg) model { +func (m Model) handleWindowSize(msg tea.WindowSizeMsg) Model { framew, frameh := lib.MainStyle.GetFrameSize() height := msg.Height - frameh @@ -57,7 +58,7 @@ func (m model) handleWindowSize(msg tea.WindowSizeMsg) model { return m } -func (m model) updateViewport(msg tea.Msg) (model, tea.Cmd) { +func (m Model) updateViewport(msg tea.Msg) (Model, tea.Cmd) { var ( cmd tea.Cmd cmds []tea.Cmd diff --git a/cmd/modal.go b/cmd/modal.go index 61494d6..f70285b 100644 --- a/cmd/modal.go +++ b/cmd/modal.go @@ -11,7 +11,8 @@ import ( "github.com/isabelroses/izrss/lib" ) -type model struct { +// Model is the main model for the application +type Model struct { help help.Model filter textinput.Model post lib.Post @@ -24,13 +25,15 @@ type model struct { ready bool } -func (m model) Init() tea.Cmd { +// Init sets the initial state of the model +func (m Model) Init() tea.Cmd { return tea.Batch( tea.SetWindowTitle("izrss"), ) } -func NewModel() model { +// NewModel creates a new model with sensible defaults +func NewModel() Model { t := table.New(table.WithFocused(true)) t.SetStyles(lib.TableStyle()) @@ -40,7 +43,7 @@ func NewModel() model { Bold(true). Foreground(lipgloss.Color("229")) - return model{ + return Model{ context: "", feeds: lib.Feeds{}, feed: lib.Feed{}, diff --git a/cmd/view.go b/cmd/view.go index cfd479e..17c2619 100644 --- a/cmd/view.go +++ b/cmd/view.go @@ -8,19 +8,20 @@ import ( "github.com/isabelroses/izrss/lib" ) -func (m model) headerView() string { +func (m Model) headerView() string { title := lib.ReaderStyle.Render(m.post.Title) line := strings.Repeat("─", lib.Max(0, m.viewport.Width-lipgloss.Width(title))) return lipgloss.JoinHorizontal(lipgloss.Center, title, line) } -func (m model) footerView() string { +func (m Model) footerView() string { info := lib.ReaderStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100)) line := strings.Repeat("─", lib.Max(0, m.viewport.Width-lipgloss.Width(info))) return lipgloss.JoinHorizontal(lipgloss.Center, line, info) } -func (m model) View() string { +// View renders the model as a string +func (m Model) View() string { out := "" if !m.ready { diff --git a/lib/config.go b/lib/config.go index dbc70a5..e155fbc 100644 --- a/lib/config.go +++ b/lib/config.go @@ -1,3 +1,4 @@ +// Package lib common libary functtions package lib import ( @@ -8,6 +9,7 @@ import ( "github.com/adrg/xdg" ) +// ParseUrls reads the URLs from the config file and returns them as a slice func ParseUrls() []string { urlsFile, err := xdg.ConfigFile("izrss/urls") if err != nil { diff --git a/lib/feeds.go b/lib/feeds.go index f67eae2..0815202 100644 --- a/lib/feeds.go +++ b/lib/feeds.go @@ -2,6 +2,7 @@ package lib import "sort" +// Post represents a single post in a feed type Post struct { UUID string `json:"uuid"` Title string `json:"-"` @@ -12,6 +13,7 @@ type Post struct { Read bool `json:"read"` } +// Feed represents a single feed type Feed struct { Title string `json:"-"` URL string `json:"URL"` @@ -19,6 +21,7 @@ type Feed struct { ID int `json:"-"` } +// Feeds represents a collection of feeds type Feeds []Feed func (f Feeds) sort(urls []string) Feeds { @@ -36,6 +39,7 @@ func (f Feeds) sort(urls []string) Feeds { return f } +// GetTotalUnreads returns the total number of unread posts in a feed func (f Feed) GetTotalUnreads() int { total := 0 for _, post := range f.Posts { diff --git a/lib/fetch.go b/lib/fetch.go index 6754ccc..8e1cc3e 100644 --- a/lib/fetch.go +++ b/lib/fetch.go @@ -12,6 +12,7 @@ import ( "github.com/mmcdole/gofeed" ) +// FetchURL fetches the content of a URL and returns it as a byte slice func FetchURL(url string, preferCache bool) []byte { fileStr := "izrss/" + URLToDir(url) file, err := xdg.CacheFile(fileStr) @@ -42,6 +43,7 @@ func FetchURL(url string, preferCache bool) []byte { return body } +// GetContentForURL fetches the content of a URL and returns it as a Feed func GetContentForURL(url string, preferCache bool) Feed { feed := setupReader(url, preferCache) @@ -69,6 +71,7 @@ func GetContentForURL(url string, preferCache bool) Feed { return feedRet } +// GetPosts fetches the content of a URL and returns it as a slice of Posts func GetPosts(url string) []Post { feed := setupReader(url, false) posts := []Post{} @@ -123,7 +126,7 @@ func setupReader(url string, preferCache bool) *gofeed.Feed { return feed } -// go reotines am irite +// GetAllContent fetches the content of all URLs and returns it as a slice of Feeds func GetAllContent(preferCache bool) Feeds { urls := ParseUrls() diff --git a/lib/helpers.go b/lib/helpers.go index d04c216..c6e3e9b 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -8,6 +8,7 @@ import ( "time" ) +// OpenURL opens the specified URL in the default browser of the user. // https://stackoverflow.com/questions/39320371/how-start-web-server-to-open-page-in-browser-in-golang // openURL opens the specified URL in the default browser of the user. func OpenURL(url string) error { @@ -46,6 +47,7 @@ func isWSL() bool { return strings.Contains(strings.ToLower(string(releaseData)), "microsoft") } +// ConvertDate converts a date string to the format "dd/mm/yyyy" func ConvertDate(dateString string) string { layoutList := []string{ "Mon, 02 Jan 2006 15:04:05 -0700", @@ -84,6 +86,8 @@ func ConvertDate(dateString string) string { return date } +// URLToDir converts a URL to a directory name +// https://isabelroses.com/feed.xml -> isabelroses_com_feed.xml func URLToDir(url string) string { url = strings.ReplaceAll(url, "https://", "") url = strings.ReplaceAll(url, "http://", "") @@ -94,6 +98,7 @@ func URLToDir(url string) string { return url } +// Max returns the maximum of two integers func Max(a, b int) int { if a > b { return a diff --git a/lib/render.go b/lib/render.go index 999b275..cd8748a 100644 --- a/lib/render.go +++ b/lib/render.go @@ -19,6 +19,7 @@ var ( htom = tomd.NewConverter("", true, nil) ) +// RenderMarkdown renders markdown content to a string func RenderMarkdown(content string) string { fromMd, err := htom.ConvertString(content) if err != nil { diff --git a/lib/state.go b/lib/state.go index 4ae9ef7..c82916b 100644 --- a/lib/state.go +++ b/lib/state.go @@ -8,12 +8,14 @@ import ( "github.com/adrg/xdg" ) +// ToggleRead toggles the read status of a post func ToggleRead(feeds Feeds, feedID int, postID int) Feeds { postr := &feeds[feedID].Posts[postID] postr.Read = !postr.Read return feeds } +// ReadAll marks all posts in a feed as read func ReadAll(feeds Feeds, feedID int) Feeds { for i := range feeds[feedID].Posts { feeds[feedID].Posts[i].Read = true @@ -21,12 +23,14 @@ func ReadAll(feeds Feeds, feedID int) Feeds { return feeds } +// MarkRead marks a post as read func MarkRead(feeds Feeds, feedID int, postID int) Feeds { postr := &feeds[feedID].Posts[postID] postr.Read = true return feeds } +// WriteTracking saves the tracking state to a JSON file func (feeds Feeds) WriteTracking() error { json, err := json.Marshal(feeds) if err != nil { @@ -35,7 +39,7 @@ func (feeds Feeds) WriteTracking() error { return os.WriteFile(getSateFile(), json, 0644) } -// Read from JSON file +// ReadTracking reads the tracking state from a JSON file func (feeds Feeds) ReadTracking() (Feeds, error) { fileStr := getSateFile() if _, err := os.Stat(fileStr); os.IsNotExist(err) { diff --git a/lib/style.go b/lib/style.go index ac130d3..600f4f6 100644 --- a/lib/style.go +++ b/lib/style.go @@ -6,15 +6,18 @@ import ( ) var ( + // MainStyle is the main style for the application MainStyle = lipgloss.NewStyle(). Border(lipgloss.RoundedBorder(), true). BorderForeground(lipgloss.Color("240")). Padding(0, 1). Margin(0) + // ReaderStyle is the style for the reader ReaderStyle = lipgloss.NewStyle() ) +// TableStyle returns the style for the table func TableStyle() table.Styles { s := table.DefaultStyles() s.Header = s.Header. diff --git a/main.go b/main.go index 46d2d2d..c05fd7f 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// Package main is the entry point for the application package main import (