Skip to content

Commit d18cbde

Browse files
committed
get hot posts done
1 parent 0a07a76 commit d18cbde

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

cmd/posts.go

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"log"
88
"net/http"
9+
"os"
910
"strconv"
1011
"time"
1112

@@ -69,49 +70,29 @@ func getHotPosts() {
6970
app := tview.NewApplication()
7071

7172
for ind, post := range hotposts.Posts {
72-
openPost := func() {
73-
var singlePost Post
74-
b, err := makeRequest(fmt.Sprintf("%s/%s", postAPI, post.Cuid))
75-
if err != nil {
76-
app.Stop()
77-
log.Fatal(err)
78-
}
7973

80-
err = json.Unmarshal(b, &singlePost)
74+
list = list.AddItem(post.Title, post.Brief, rune(strconv.Itoa(ind)[0]), nil)
75+
}
76+
77+
list.AddItem("Quit", "Press to exit", 'q', func() {
78+
app.Stop()
79+
os.Exit(0)
80+
})
81+
82+
list.SetSelectedFunc(func(runeindex int, title string, desc string, r rune) {
83+
if r != 'q' {
84+
n, err := strconv.Atoi(string(r))
8185
if err != nil {
8286
app.Stop()
83-
log.Fatal(err)
87+
panic(err)
8488
}
8589

86-
writePost := func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
87-
// md := markdown.New(markdown.XHTMLOutput(true))
88-
89-
// outputmd := md.RenderToString([]byte(singlePost.Post.ContentMarkdown))
90+
openPost(app, hotposts.Posts[n].Cuid, list)
9091

91-
for ind, line := range tview.WordWrap(singlePost.Post.ContentMarkdown, width) {
92-
tview.PrintSimple(screen, line, x+3, y+ind+1)
93-
}
94-
95-
// for ind, line := range tview.WordWrap(string(outputmd), width) {
96-
// tview.PrintSimple(screen, line, x+3, y+ind+1)
97-
// }
98-
return x, y, width, height
99-
}
100-
101-
box := tview.NewBox().
102-
SetBorder(true).
103-
SetTitle(post.Title).SetDrawFunc(writePost)
104-
app.SetRoot(box, true).Run()
10592
}
106-
107-
list = list.AddItem(post.Title, post.Brief, rune(strconv.Itoa(ind)[0]), openPost)
108-
}
109-
110-
list.AddItem("Quit", "Press to exit", 'q', func() {
111-
app.Stop()
11293
})
113-
11494
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
95+
app.Stop()
11596
panic(err)
11697
}
11798
// box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
@@ -121,6 +102,43 @@ func getHotPosts() {
121102

122103
}
123104

105+
func openPost(app *tview.Application, postcuid string, list *tview.List) {
106+
var singlePost Post
107+
b, err := makeRequest(fmt.Sprintf("%s/%s", postAPI, postcuid))
108+
if err != nil {
109+
app.Stop()
110+
log.Fatal(err)
111+
}
112+
113+
err = json.Unmarshal(b, &singlePost)
114+
if err != nil {
115+
app.Stop()
116+
log.Fatal(err)
117+
}
118+
119+
textView := tview.NewTextView().
120+
SetDynamicColors(true).
121+
SetRegions(true).
122+
SetChangedFunc(func() {
123+
app.Draw()
124+
})
125+
textView.SetText(singlePost.Post.ContentMarkdown)
126+
127+
textView.SetDoneFunc(func(key tcell.Key) {
128+
if key == tcell.KeyEscape {
129+
if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
130+
app.Stop()
131+
panic(err)
132+
}
133+
}
134+
})
135+
textView.SetBorder(true)
136+
if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil {
137+
app.Stop()
138+
panic(err)
139+
}
140+
}
141+
124142
func getNews() {
125143
b, err := makeRequest(newsAPI)
126144
if err != nil {

0 commit comments

Comments
 (0)