@@ -12,6 +12,7 @@ import (
1212 "slices"
1313 "strconv"
1414 "strings"
15+ "time"
1516
1617 "github.com/alecthomas/chroma/v2/formatters"
1718 "github.com/alecthomas/chroma/v2/lexers"
@@ -30,6 +31,10 @@ const (
3031 green string = "\033 [32m"
3132 yellow string = "\033 [33m"
3233 magenta string = "\033 [35m"
34+ questionColor string = "\033 [38;5;204m"
35+ answerColor string = "\033 [38;5;255m"
36+ downvoted string = "\033 [38;5;160m"
37+ lightgray string = "\033 [38;5;248m"
3338)
3439
3540var (
8388 "<code>" , green ,
8489 "</code>" , reset ,
8590 "<br />" , "" ,
91+ "<br/>" , "" ,
8692 "<hr />" , "" ,
93+ "<hr/>" , "" ,
8794 "<sup>" , "" ,
8895 "</sup>" , "" ,
8996 "<sub>" , "" ,
@@ -235,35 +242,63 @@ type Config struct {
235242 Client * http.Client
236243}
237244type Answer struct {
238- Score int
239- Body string
240- Link string
245+ Author string
246+ Score int
247+ Body string
248+ Link string
249+ IsAccepted bool
250+ Date time.Time
241251}
242252
243253func (a * Answer ) String () string {
244254 line := strings .Repeat ("─" , 80 )
255+ color := yellow
256+ if a .IsAccepted {
257+ color = green
258+ } else if a .Score < 0 {
259+ color = downvoted
260+ }
245261 return fmt .Sprintf (`
246262%s
247- %s[%d]%s %s
263+ %s[%d]%s %sAnswer from %s%s%s
264+ %sDate: %s
265+ Link: %s%s
248266%s
249- ` , line , yellow , a .Score , reset , a .Link , line )
267+ ` ,
268+ line ,
269+ color , a .Score , reset , answerColor , bold , a .Author , reset ,
270+ lightgray , a .Date .Format (time .RFC822 ),
271+ a .Link , reset ,
272+ line )
250273}
251274
252275type Result struct {
253276 Title string
254277 Link string
255278 QuestionId int
256279 UpvoteCount int
280+ Date time.Time
257281 Answers []* Answer
258282}
259283
260284func (r * Result ) String () string {
261285 line := strings .Repeat ("─" , 80 )
286+ color := yellow
287+ if r .UpvoteCount < 0 {
288+ color = downvoted
289+ }
290+
262291 return fmt .Sprintf (`
263292%s
264- %s[%d]%s %s
265- %s
266- %s` , line , yellow , r .UpvoteCount , reset , r .Title , r .Link , line )
293+ %s[%d]%s %s%s%s%s
294+ %sDate: %s
295+ Link: %s%s
296+ %s` ,
297+ line ,
298+ color , r .UpvoteCount , reset , bold , questionColor , r .Title , reset ,
299+ lightgray , r .Date .Format (time .RFC822 ),
300+ r .Link , reset ,
301+ line )
267302}
268303
269304func prepareText (text string ) string {
@@ -307,13 +342,15 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
307342 questions := make ([]string , 0 , len (gr .Items ))
308343 for _ , item := range gr .Items {
309344 var upvoteCount int
345+ var dateCreated time.Time
310346 if len (item .Pagemap .Question ) > 0 {
311347 question := item .Pagemap .Question [0 ]
312348 answerCount , _ := strconv .Atoi (question .Answercount )
313349 if answerCount == 0 {
314350 continue
315351 }
316352 upvoteCount , _ = strconv .Atoi (question .Upvotecount )
353+ dateCreated , _ = time .Parse ("2006-01-02T15:04:05" , question .Datecreated )
317354 }
318355 u , _ := netUrl .Parse (item .Link )
319356 questionStr := strings .Split (u .Path , "/" )[2 ]
@@ -324,6 +361,7 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
324361 Link : item .Link ,
325362 QuestionId : questionId ,
326363 UpvoteCount : upvoteCount ,
364+ Date : dateCreated ,
327365 }
328366 }
329367 question_ids := strings .Join (questions , ";" )
@@ -353,9 +391,12 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
353391 }
354392 result .Answers = append (result .Answers ,
355393 & Answer {
356- Score : item .Score ,
357- Body : item .Body ,
358- Link : fmt .Sprintf ("https://stackoverflow.com/a/%d" , item .AnswerID ),
394+ Author : item .Owner .DisplayName ,
395+ Score : item .Score ,
396+ Body : item .Body ,
397+ Link : fmt .Sprintf ("https://stackoverflow.com/a/%d" , item .AnswerID ),
398+ IsAccepted : item .IsAccepted ,
399+ Date : time .Unix (int64 (item .CreationDate ), 0 ).UTC (),
359400 })
360401 }
361402 return results , nil
0 commit comments