Respect bottomup for -Pw

This commit also refactors the news related sructs into their own types
instead of being anonymous and moves the printing to its own .Print()
function.
This commit is contained in:
morganamilo committed 2018-05-07 01:06:24 +01:00
1 parent feb3b8b6c7
commit 6af2b5c6e1
1 file changed
+45 -23
+45 -23
View File
@@ -402,21 +402,39 @@ outer:
return nil
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
Creator string `xml:"dc:creator"`
}
func (item Item) Print() error {
date, err := time.Parse(time.RFC1123Z, item.PubDate)
if err != nil {
return err
}
fd := formatTime(int(date.Unix()))
fmt.Println(magenta(fd), strings.TrimSpace(item.Title))
return nil
}
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Language string `xml:"language"`
Lastbuilddate string `xml:"lastbuilddate"`
Items []Item `xml:"item"`
}
type rss struct {
Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Language string `xml:"language"`
Lastbuilddate string `xml:"lastbuilddate"`
Item []struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
Creator string `xml:"dc:creator"`
} `xml:"item"`
} `xml:"channel"`
Channel Channel `xml:"channel"`
}
func printNewsFeed() error {
@@ -439,16 +457,20 @@ func printNewsFeed() error {
return err
}
for _, item := range rss.Channel.Item {
date, err := time.Parse(time.RFC1123Z, item.PubDate)
if err != nil {
return err
if config.SortMode == BottomUp {
for i := len(rss.Channel.Items) - 1; i >= 0; i-- {
err := rss.Channel.Items[i].Print()
if err != nil {
return err
}
}
} else {
for i := 0; i < len(rss.Channel.Items); i++ {
err := rss.Channel.Items[i].Print()
if err != nil {
return err
}
}
fd := formatTime(int(date.Unix()))
fmt.Println(magenta(fd), strings.TrimSpace(item.Title))
}
return nil