Skip to content

Commit c64f58d

Browse files
committed
Add loading bars
1 parent 43e5192 commit c64f58d

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

Gopkg.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/posts/hot.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"time"
1212

13+
"github.com/briandowns/spinner"
1314
"github.com/rivo/tview"
1415
)
1516

@@ -24,11 +25,16 @@ var (
2425
)
2526

2627
func GetHotPosts() {
28+
29+
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
30+
s.Start()
2731
b, err := makeRequest(hotPostsAPI)
2832
if err != nil {
2933
log.Printf("Oops, some network error: %v\n", err)
3034
os.Exit(0)
3135
}
36+
s.Stop()
37+
3238
var hotposts HotPosts
3339
err = json.Unmarshal(b, &hotposts)
3440
if err != nil {
@@ -68,6 +74,7 @@ func GetHotPosts() {
6874
}
6975

7076
func makeRequest(url string) ([]byte, error) {
77+
7178
client := http.Client{
7279
Timeout: time.Duration(1 * time.Minute),
7380
}

pkg/posts/news.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"time"
1010

11+
"github.com/briandowns/spinner"
1112
"github.com/rivo/tview"
1213
)
1314

@@ -16,11 +17,16 @@ var (
1617
)
1718

1819
func GetNews() {
20+
21+
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
22+
s.Start()
1923
b, err := makeRequest(newsAPI)
2024
if err != nil {
2125
log.Printf("Oops, some network error: %v\n", err)
2226
os.Exit(0)
2327
}
28+
s.Stop()
29+
2430
var posts TopNews
2531
err = json.Unmarshal(b, &posts)
2632
if err != nil {

pkg/posts/post.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,44 @@ import (
1313

1414
// openPost opens a post in a new tview box
1515
func openPost(app *tview.Application, postcuid string, list *tview.List) {
16+
17+
textView := tview.NewTextView().
18+
SetDynamicColors(true).
19+
SetRegions(true).
20+
SetWrap(true).
21+
SetWordWrap(true).
22+
SetTextAlign(tview.AlignLeft).
23+
SetChangedFunc(func() {
24+
app.Draw()
25+
})
26+
27+
textView.Box = textView.Box.SetBorder(true).SetBorderPadding(1, 1, 2, 1)
28+
textView.SetBorder(true)
29+
30+
go func() {
31+
if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil {
32+
app.Stop()
33+
panic(err)
34+
}
35+
}()
36+
1637
var singlePost Post
38+
textView.SetText("[green::l]Loading...")
1739
b, err := makeRequest(fmt.Sprintf("%s/%s", postAPI, postcuid))
1840
if err != nil {
1941
app.Stop()
2042
log.Fatal(err)
2143
}
44+
textView.SetText("")
45+
textView.ScrollToBeginning()
2246

2347
err = json.Unmarshal(b, &singlePost)
2448
if err != nil {
2549
app.Stop()
2650
log.Fatal(err)
2751
}
2852

29-
textView := tview.NewTextView().
30-
SetDynamicColors(true).
31-
SetRegions(true).
32-
SetWrap(true).
33-
SetWordWrap(true).
34-
SetTextAlign(tview.AlignLeft).
35-
SetChangedFunc(func() {
36-
app.Draw()
37-
})
38-
39-
title := fmt.Sprintf("Title: %s", singlePost.Post.Title)
53+
title := fmt.Sprintf("\nTitle: %s", singlePost.Post.Title)
4054
var author string
4155
if singlePost.Post.Author.Name != "" {
4256
author = fmt.Sprintf("Author: %s", singlePost.Post.Author.Name)
@@ -89,9 +103,6 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
89103

90104
}
91105

92-
textView.Box = textView.Box.SetBorder(true).SetBorderPadding(1, 1, 2, 1)
93-
textView.SetBorder(true)
94-
95106
textView.SetDoneFunc(func(key tcell.Key) {
96107
if key == tcell.KeyEscape {
97108
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
@@ -101,10 +112,6 @@ func openPost(app *tview.Application, postcuid string, list *tview.List) {
101112
}
102113
})
103114

104-
if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil {
105-
app.Stop()
106-
panic(err)
107-
}
108115
}
109116

110117
func writeToTextView(t *tview.TextView, contents ...string) {

pkg/posts/trending.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"time"
1010

11+
"github.com/briandowns/spinner"
1112
"github.com/rivo/tview"
1213
)
1314

@@ -16,11 +17,16 @@ var (
1617
)
1718

1819
func GetTrendingPosts() {
20+
21+
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
22+
s.Start()
1923
b, err := makeRequest(trendingStoriesAPI)
2024
if err != nil {
2125
log.Printf("Oops, some network error: %v\n", err)
2226
os.Exit(0)
2327
}
28+
s.Stop()
29+
2430
var posts TrendingStories
2531
err = json.Unmarshal(b, &posts)
2632
if err != nil {

0 commit comments

Comments
 (0)