Skip to content

Commit

Permalink
docs: add documentation for all public data
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Apr 19, 2024
1 parent 74b2345 commit d902dd9
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 19 deletions.
3 changes: 2 additions & 1 deletion cmd/keys.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package cmd contains all the command functions
package cmd

import (
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions cmd/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

Expand All @@ -40,7 +43,7 @@ func NewModel() model {
Bold(true).
Foreground(lipgloss.Color("229"))

return model{
return Model{
context: "",
feeds: lib.Feeds{},
feed: lib.Feed{},
Expand Down
7 changes: 4 additions & 3 deletions cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package lib common libary functtions
package lib

import (
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions lib/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand All @@ -12,13 +13,15 @@ type Post struct {
Read bool `json:"read"`
}

// Feed represents a single feed
type Feed struct {
Title string `json:"-"`
URL string `json:"URL"`
Posts []Post `json:"posts"`
ID int `json:"-"`
}

// Feeds represents a collection of feeds
type Feeds []Feed

func (f Feeds) sort(urls []string) Feeds {
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion lib/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 5 additions & 0 deletions lib/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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://", "")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion lib/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ 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
}
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 {
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main is the entry point for the application
package main

import (
Expand Down

0 comments on commit d902dd9

Please sign in to comment.