Skip to content

Commit ecad74c

Browse files
committed
add news
1 parent c7165a0 commit ecad74c

File tree

3 files changed

+70
-103
lines changed

3 files changed

+70
-103
lines changed

Gopkg.lock

Lines changed: 1 addition & 65 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: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strconv"
1111
"time"
1212

13-
"github.com/gdamore/tcell"
1413
"github.com/rivo/tview"
1514
)
1615

@@ -68,43 +67,6 @@ func GetHotPosts() {
6867

6968
}
7069

71-
func openPost(app *tview.Application, postcuid string, list *tview.List) {
72-
var singlePost Post
73-
b, err := makeRequest(fmt.Sprintf("%s/%s", postAPI, postcuid))
74-
if err != nil {
75-
app.Stop()
76-
log.Fatal(err)
77-
}
78-
79-
err = json.Unmarshal(b, &singlePost)
80-
if err != nil {
81-
app.Stop()
82-
log.Fatal(err)
83-
}
84-
85-
textView := tview.NewTextView().
86-
SetDynamicColors(true).
87-
SetRegions(true).
88-
SetChangedFunc(func() {
89-
app.Draw()
90-
})
91-
textView.SetText(singlePost.Post.ContentMarkdown)
92-
93-
textView.SetDoneFunc(func(key tcell.Key) {
94-
if key == tcell.KeyEscape {
95-
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
96-
app.Stop()
97-
panic(err)
98-
}
99-
}
100-
})
101-
textView.SetBorder(true)
102-
if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil {
103-
app.Stop()
104-
panic(err)
105-
}
106-
}
107-
10870
func makeRequest(url string) ([]byte, error) {
10971
client := http.Client{
11072
Timeout: time.Duration(1 * time.Minute),

pkg/posts/post.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package posts
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
8+
"github.com/gdamore/tcell"
9+
"github.com/rivo/tview"
10+
)
11+
12+
// openPost opens a post in a new tview box
13+
func openPost(app *tview.Application, postcuid string, list *tview.List) {
14+
var singlePost Post
15+
b, err := makeRequest(fmt.Sprintf("%s/%s", postAPI, postcuid))
16+
if err != nil {
17+
app.Stop()
18+
log.Fatal(err)
19+
}
20+
21+
err = json.Unmarshal(b, &singlePost)
22+
if err != nil {
23+
app.Stop()
24+
log.Fatal(err)
25+
}
26+
27+
textView := tview.NewTextView().
28+
SetDynamicColors(true).
29+
SetRegions(true).
30+
SetWrap(true).
31+
SetWordWrap(true).
32+
SetTextAlign(tview.AlignLeft).
33+
SetChangedFunc(func() {
34+
app.Draw()
35+
})
36+
37+
title := fmt.Sprintf("Title: %s", singlePost.Post.Title)
38+
author := fmt.Sprintf("Author: %s", singlePost.Post.Author.Name)
39+
upvotes := fmt.Sprintf("Upvotes: %d", singlePost.Post.Upvotes)
40+
ptype := fmt.Sprintf("Type: %s", singlePost.Post.Type)
41+
writeToTextView(textView, title,
42+
author,
43+
upvotes,
44+
ptype,
45+
"\n",
46+
singlePost.Post.ContentMarkdown,
47+
)
48+
49+
textView.SetDoneFunc(func(key tcell.Key) {
50+
if key == tcell.KeyEscape {
51+
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
52+
app.Stop()
53+
panic(err)
54+
}
55+
}
56+
})
57+
textView.SetBorder(true)
58+
if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil {
59+
app.Stop()
60+
panic(err)
61+
}
62+
}
63+
64+
func writeToTextView(t *tview.TextView, contents ...string) {
65+
for _, content := range contents {
66+
t.Write([]byte(content))
67+
t.Write([]byte("\n"))
68+
}
69+
}

0 commit comments

Comments
 (0)