Skip to content

Commit

Permalink
Refactoring and bugfixing RSS.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickali committed Aug 5, 2020
1 parent a48c036 commit 707ca6e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
14 changes: 10 additions & 4 deletions addons/newsreader/newsreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mum4k/termdash/widgets/text"
)

var maxItems = 20
var maxItems = 100
var maxLength = 300
var fmtDate string

Expand Down Expand Up @@ -52,8 +52,9 @@ func Print(stURL string) ([]string, []text.WriteOption, []text.WriteOption) {
strippedNewlines7Desc := strings.Replace(strippedNewlines6Desc, "<p>", "", -1)
strippedNewlines8Desc := strings.Replace(strippedNewlines7Desc, "</p>", "", -1)
strippedNewlines9Desc := strings.Replace(strippedNewlines8Desc, "&lt;", "", -1)
strippedNewlines10Desc := strings.ReplaceAll(strippedNewlines9Desc, "\u200b", "")

descLength := len(strippedNewlines9Desc)
descLength := len(strippedNewlines10Desc)
if descLength < 300 {
if descLength == 0 {
maxLength = 0
Expand All @@ -64,13 +65,18 @@ func Print(stURL string) ([]string, []text.WriteOption, []text.WriteOption) {
maxLength = 300
}

strippedDesc := p.Sanitize(strippedNewlines9Desc[:maxLength])
strippedDesc := p.Sanitize(strippedNewlines10Desc[:maxLength])

wrappedText = append(wrappedText, items[i].Title+"\n")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorGreen)))
wrappedState = append(wrappedState, nil)

wrappedText = append(wrappedText, strippedDesc+"\n")
if len(strippedDesc) > 5 {
wrappedText = append(wrappedText, strippedDesc+"\n")
} else {
wrappedText = append(wrappedText, "")
}

wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorRed)))
wrappedState = append(wrappedState, nil)

Expand Down
2 changes: 2 additions & 0 deletions addons/stocks/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module addons/stocks

go 1.14

require github.com/mum4k/termdash v0.12.1
13 changes: 13 additions & 0 deletions addons/stocks/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mum4k/termdash v0.12.1 h1:g3WAT602WIYII6Szhn2KsGoT4PffJZQoA4aAFxEllKc=
github.com/mum4k/termdash v0.12.1/go.mod h1:haerPCSO0U8pehROAecmuOHDF+2UXw2KaCTxdWooDFE=
github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
28 changes: 18 additions & 10 deletions addons/stocks/stocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package stocks

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"

"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/text"
)

const url = "https://www.alphavantage.co/query"
Expand All @@ -31,12 +32,17 @@ type QuoteDetail struct {

// Print just outputs a string.
// See https://www.alphavantage.co/documentation/#latestprice
func Print(stSymbol string, stAPI string) {
func Print(stSymbol string, stAPI string) ([]string, []text.WriteOption, []text.WriteOption) {
stURL := url + "?function=GLOBAL_QUOTE&symbol=" + stSymbol + "&apikey=" + stAPI
wrappedText := make([]string, 0)
wrappedOpt := make([]text.WriteOption, 0)
wrappedState := make([]text.WriteOption, 0)

response, err := http.Get(stURL)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
wrappedText = append(wrappedText, "The HTTP request failed with error with stock")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorRed)))
wrappedState = append(wrappedState, text.WriteReplace())
} else {
data, _ := ioutil.ReadAll(response.Body)
jsonData := &StockQuote{
Expand All @@ -45,13 +51,15 @@ func Print(stSymbol string, stAPI string) {
err := json.Unmarshal([]byte(data), jsonData)

if err != nil {
fmt.Printf("Something failed with failed with error %s\n", err)
wrappedText = append(wrappedText, "Problems unmarshalling stock")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorRed)))
wrappedState = append(wrappedState, text.WriteReplace())

} else {
fQuote, _ := strconv.ParseFloat(jsonData.QuoteDetail.QuotePrice, 64)
fmt.Println(jsonData.QuoteDetail.QuoteSymbol + ":")
fmt.Print("\t")
fmt.Println(fQuote)
wrappedText = append(wrappedText, jsonData.QuoteDetail.QuoteSymbol+": "+jsonData.QuoteDetail.QuotePrice)
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorGreen)))
wrappedState = append(wrappedState, text.WriteReplace())
}
}
return
return wrappedText, wrappedOpt, wrappedState
}
49 changes: 33 additions & 16 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func readConfig() bool {
return true
}

func writeUpdate(ctx context.Context, TopLeft *text.Text, BottomLeft *text.Text, TopRight *text.Text, delay time.Duration) {
func writeUpdate(ctx context.Context, TopLeftLeft *text.Text, BottomLeft *text.Text, TopRight *text.Text, delay time.Duration) {

ticker := time.NewTicker(delay)
defer ticker.Stop()
Expand All @@ -65,9 +65,9 @@ func writeUpdate(ctx context.Context, TopLeft *text.Text, BottomLeft *text.Text,
wrappedText, wrappedOpt, wrappedState := weather.Print(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))
for i, s := range wrappedText {
if wrappedState[i] != nil {
TopLeft.Write(s, wrappedState[i], wrappedOpt[i])
TopLeftLeft.Write(s, wrappedState[i], wrappedOpt[i])
} else {
TopLeft.Write(s, wrappedOpt[i])
TopLeftLeft.Write(s, wrappedOpt[i])
}
}

Expand Down Expand Up @@ -100,26 +100,29 @@ func main() {

readConfig()

var wrappedText []string
var wrappedOpt []text.WriteOption
var wrappedState []text.WriteOption
var weathwrappedText []string
var weathwrappedOpt []text.WriteOption
var weathwrappedState []text.WriteOption
var newswrappedText []string
var newswrappedOpt []text.WriteOption
var newswrappedState []text.WriteOption
var rsswrappedText []string
var rsswrappedOpt []text.WriteOption
var rsswrappedState []text.WriteOption
var stockwrappedText []string
var stockwrappedOpt []text.WriteOption
var stockwrappedState []text.WriteOption

var wg sync.WaitGroup
wg.Add(4)

go func() {
wrappedText, wrappedOpt, wrappedState = weather.Print(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))
weathwrappedText, weathwrappedOpt, weathwrappedState = weather.Print(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))
wg.Done()
}()

go func() {
stocks.Print(viper.GetString("stocks.symbol"), viper.GetString("stocks.api_key"))
stockwrappedText, stockwrappedOpt, stockwrappedState = stocks.Print(viper.GetString("stocks.symbol"), viper.GetString("stocks.api_key"))
wg.Done()
}()

Expand All @@ -142,15 +145,24 @@ func main() {
}
defer t.Close()

borderlessTopLeft, err := text.New(text.WrapAtWords())
borderlessTopLeftLeft, err := text.New(text.WrapAtWords())
borderlessTopLeftRight, err := text.New(text.WrapAtWords())
borderlessBottomLeft, err := text.New(text.WrapAtWords())
borderlessTopRight, err := text.New(text.WrapAtWords())

for i, s := range wrappedText {
if wrappedState[i] != nil {
borderlessTopLeft.Write(s, wrappedState[i], wrappedOpt[i])
for i, s := range weathwrappedText {
if weathwrappedState[i] != nil {
borderlessTopLeftLeft.Write(s, weathwrappedState[i], weathwrappedOpt[i])
} else {
borderlessTopLeft.Write(s, wrappedOpt[i])
borderlessTopLeftLeft.Write(s, weathwrappedOpt[i])
}
}

for i, s := range stockwrappedText {
if stockwrappedState[i] != nil {
borderlessTopLeftRight.Write(s, stockwrappedState[i], stockwrappedOpt[i])
} else {
borderlessTopLeftRight.Write(s, stockwrappedOpt[i])
}
}

Expand All @@ -170,7 +182,7 @@ func main() {
}
}

go writeUpdate(ctx, borderlessTopLeft, borderlessBottomLeft, borderlessTopRight, 10*time.Second)
go writeUpdate(ctx, borderlessTopLeftLeft, borderlessBottomLeft, borderlessTopRight, 10*time.Second)

c, err := container.New(
t, container.ID(rootID),
Expand All @@ -179,9 +191,14 @@ func main() {
container.SplitVertical(
container.Left(
container.SplitHorizontal(
container.Top(container.PlaceWidget(borderlessTopLeft)),
container.Top(
container.SplitVertical(
container.Left(
container.PaddingLeftPercent(15), container.Border(linestyle.Round), container.PlaceWidget(borderlessTopLeftLeft)),
container.Right(
container.PaddingLeftPercent(37), container.Border(linestyle.Round), container.PlaceWidget(borderlessTopLeftRight)))),
container.Bottom(
container.Border(linestyle.Light), container.BorderTitle("RSS"), container.PaddingRightPercent(3), container.PaddingLeftPercent(3), container.PlaceWidget(borderlessBottomLeft)), container.SplitPercent(5))),
container.Border(linestyle.Light), container.BorderTitle("RSS"), container.PaddingRightPercent(3), container.PaddingLeftPercent(3), container.PlaceWidget(borderlessBottomLeft)), container.SplitPercent(7))),
container.Right(container.Border(linestyle.Light), container.BorderTitle("News"), container.PaddingRightPercent(3), container.PaddingLeftPercent(3), container.PlaceWidget(borderlessTopRight))))

if err != nil {
Expand Down

0 comments on commit 707ca6e

Please sign in to comment.