From afbfa53ae6abdfdd5830b1384752fed531d84621 Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Fri, 3 Jul 2020 23:25:48 -0400 Subject: [PATCH] Add feed description view (#12) with colors! --- README.md | 7 ++++--- cui/colours.go | 7 +++++-- cui/main.go | 48 +++++++++++++++++++++++++++++++++++++++--------- cui/scroll.go | 12 ++++++++++-- file/file.go | 9 +++++---- go.mod | 1 + go.sum | 13 ++++++++++++- utils/html.go | 23 +++++++++++++++++++++++ 8 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 utils/html.go diff --git a/README.md b/README.md index b4402ff..deef1d1 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,15 @@ View RSS feeds from the terminal. -![image](https://user-images.githubusercontent.com/4519234/78465683-bc1f6e00-76c6-11ea-96e7-1cdd4a5c294f.png) +![image](https://user-images.githubusercontent.com/4519234/86504202-b861bd00-bd83-11ea-8a8e-4f28e38a71ce.png) + ## install ### via releases ```shell -VERSION='0.0.2'; \ +VERSION='0.1.0'; \ sudo curl --progress-bar \ -L "https://github.com/davegallant/srv/releases/download/v${VERSION}/srv_${VERSION}_$(uname -s)_x86_64.tar.gz" | \ sudo tar -C /usr/bin --overwrite -xvzf - srv @@ -39,7 +40,7 @@ An example config can be copied: cp ./config-example.yaml ~/.config/srv/config.yaml ``` -## control +## navigate Key mappings are statically defined for the time being. diff --git a/cui/colours.go b/cui/colours.go index d5bdab1..504b666 100644 --- a/cui/colours.go +++ b/cui/colours.go @@ -3,6 +3,9 @@ package cui import "github.com/jroimartin/gocui" const ( - selectionBgColor = gocui.ColorWhite - selectionFgColor = gocui.ColorBlack + feedItemSelectionBgColor = gocui.ColorCyan + feedItemSelectionFgColor = gocui.ColorBlack + feedNameSelectionBgColor = gocui.ColorWhite + feedNameSelectionFgColor = gocui.ColorBlack + descriptionFgColor = gocui.ColorCyan ) diff --git a/cui/main.go b/cui/main.go index 675b83a..eaaee9e 100644 --- a/cui/main.go +++ b/cui/main.go @@ -8,7 +8,9 @@ import ( "path" "github.com/davegallant/srv/controller" + "github.com/davegallant/srv/utils" "github.com/jroimartin/gocui" + "github.com/mmcdole/gofeed" ) // Controller can access internal state @@ -40,10 +42,28 @@ func openFeed(g *gocui.Gui, v *gocui.View) error { return nil } +func getCurrentFeedItem(v *gocui.View) *gofeed.Item { + _, oy := v.Origin() + return Controller.Rss.Feeds[Controller.CurrentFeed].Items[oy] +} + +// displayDescription displays feed descriptin if it exists +func displayDescription(g *gocui.Gui, v *gocui.View) error { + + ov, _ := g.View("Description") + ov.Clear() + + item := getCurrentFeedItem(v) + description := utils.StripHtmlTags(item.Description) + fmt.Fprintln(ov, description) + + return nil +} + // openItem opens the feed in an external browser func openItem(g *gocui.Gui, v *gocui.View) error { - _, oy := v.Origin() - item := Controller.Rss.Feeds[Controller.CurrentFeed].Items[oy] + + item := getCurrentFeedItem(v) err := exec.Command( Controller.Config.ExternalViewer, append(Controller.Config.ExternalViewerArgs, item.Link)...).Start() @@ -92,13 +112,13 @@ func nextView(g *gocui.Gui, v *gocui.View) error { func layout(g *gocui.Gui) error { maxX, maxY := g.Size() - if v, err := g.SetView("feeds", 0, 0, maxX-1, maxY/4-1); err != nil { + if v, err := g.SetView("feeds", 0, 0, maxX-1, maxY/3-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Highlight = true - v.SelBgColor = selectionBgColor - v.SelFgColor = selectionFgColor + v.SelBgColor = feedNameSelectionBgColor + v.SelFgColor = feedNameSelectionFgColor v.Title = "Feeds" if _, err = setCurrentViewOnTop(g, "feeds"); err != nil { @@ -108,15 +128,25 @@ func layout(g *gocui.Gui) error { fmt.Fprintln(v, "-", f.Title) } } - if v, err := g.SetView("Items", 0, maxY/4, maxX-1, maxY-1); err != nil { + if v, err := g.SetView("Items", 0, maxY/3, maxX-1, maxY-(maxY/5)-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Highlight = true - v.SelBgColor = selectionBgColor - v.SelFgColor = selectionFgColor + v.SelBgColor = feedItemSelectionBgColor + v.SelFgColor = feedItemSelectionFgColor v.Title = "Items" } + if v, err := g.SetView("Description", 0, maxY-maxY/5, maxX-1, maxY-1); err != nil { + if err != gocui.ErrUnknownView { + return err + } + v.SelBgColor = feedItemSelectionBgColor + v.SelFgColor = feedItemSelectionFgColor + v.Title = "Description" + v.FgColor = descriptionFgColor + v.Wrap = true + } return nil } @@ -135,7 +165,7 @@ func Start() { Controller = &controller.Controller{} Controller.Init(configPath) - g, err := gocui.NewGui(gocui.OutputNormal) + g, err := gocui.NewGui(gocui.Output256) if err != nil { log.Panicln(err) } diff --git a/cui/scroll.go b/cui/scroll.go index d44237a..4ff2f3d 100644 --- a/cui/scroll.go +++ b/cui/scroll.go @@ -30,9 +30,17 @@ func scroll(g *gocui.Gui, v *gocui.View, direction int) error { } func cursorDown(g *gocui.Gui, v *gocui.View) error { - return scroll(g, v, 1) + err := scroll(g, v, 1) + if g.CurrentView().Title == "Items" { + displayDescription(g, v) + } + return err } func cursorUp(g *gocui.Gui, v *gocui.View) error { - return scroll(g, v, -1) + err := scroll(g, v, -1) + if g.CurrentView().Title == "Items" { + displayDescription(g, v) + } + return err } diff --git a/file/file.go b/file/file.go index c25183d..b2a265e 100644 --- a/file/file.go +++ b/file/file.go @@ -2,14 +2,15 @@ package file import ( - "os" + "github.com/spf13/afero" ) // Exists returns true if a file exists func Exists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { + var AppFs = afero.NewOsFs() + _, err := AppFs.Stat(filename) + if err != nil { return false } - return !info.IsDir() + return true } diff --git a/go.mod b/go.mod index 1b03d84..f082f52 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/mattn/go-runewidth v0.0.8 // indirect github.com/mmcdole/gofeed v1.0.0 github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be // indirect + github.com/spf13/afero v1.3.1 github.com/stretchr/testify v1.6.1 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect golang.org/x/text v0.3.2 // indirect diff --git a/go.sum b/go.sum index 74bbc76..21f2684 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jroimartin/gocui v0.4.0 h1:52jnalstgmc25FmtGcWqa0tcbMEWS6RpFLsOIO+I+E8= github.com/jroimartin/gocui v0.4.0/go.mod h1:7i7bbj99OgFHzo7kB2zPb8pXLqMBSQegY7azfqXMkyY= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -28,23 +29,31 @@ github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/ github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8= github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be h1:yzmWtPyxEUIKdZg4RcPq64MfS8NA6A5fNOJgYhpR9EQ= github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/afero v1.3.1 h1:GPTpEAuNr98px18yNQ66JllNil98wfRZ/5Ukny8FeQA= +github.com/spf13/afero v1.3.1/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= @@ -58,5 +67,7 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= \ No newline at end of file +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/utils/html.go b/utils/html.go new file mode 100644 index 0000000..9c31bd9 --- /dev/null +++ b/utils/html.go @@ -0,0 +1,23 @@ +package utils + +import ( + "regexp" + "sort" + "strings" +) + +// StripHtmlTags uses regex to strip all html elements +func StripHtmlTags(s string) string { + const pattern = `(<\/?[a-zA-A]+?[^>]*\/?>)*` + r := regexp.MustCompile(pattern) + groups := r.FindAllString(s, -1) + sort.Slice(groups, func(i, j int) bool { + return len(groups[i]) > len(groups[j]) + }) + for _, group := range groups { + if strings.TrimSpace(group) != "" { + s = strings.ReplaceAll(s, group, "") + } + } + return s +}