Skip to content

Commit c418d50

Browse files
Horizontal lines length now depends on terminal width (now looks better in Termux)
1 parent a12340b commit c418d50

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ module github.com/shadowy-pycoder/goso
22

33
go 1.23.0
44

5-
require github.com/alecthomas/chroma/v2 v2.14.0
5+
require (
6+
github.com/alecthomas/chroma/v2 v2.14.0
7+
golang.org/x/term v0.26.0
8+
)
69

7-
require github.com/dlclark/regexp2 v1.11.0 // indirect
10+
require (
11+
github.com/dlclark/regexp2 v1.11.0 // indirect
12+
golang.org/x/sys v0.27.0 // indirect
13+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxK
88
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
99
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
1010
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
11+
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
12+
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13+
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
14+
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=

goso.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/alecthomas/chroma/v2/formatters"
1818
"github.com/alecthomas/chroma/v2/lexers"
1919
"github.com/alecthomas/chroma/v2/styles"
20+
"golang.org/x/term"
2021
)
2122

2223
const (
@@ -38,8 +39,9 @@ const (
3839
)
3940

4041
var (
41-
codeStartIdx int
42-
codeEndIdx int
42+
codeStartIdx int
43+
codeEndIdx int
44+
terminalWidth int
4345
// https://meta.stackexchange.com/questions/1777/what-html-tags-are-allowed-on-stack-exchange-sites
4446
codePattern = regexp.MustCompile(`<pre\s.*?>`)
4547
aHrefPattern = regexp.MustCompile(`(?s)<a\s+(?:[^>]*?\s+)?href=(["'])?([^\'" >]+)(.*?)?</a>`)
@@ -62,7 +64,7 @@ var (
6264
"</ol>", "",
6365
"<li>", " - ",
6466
"</li>", "",
65-
"<hr>", "────────────────────────────────────────────────────────────────────────────────",
67+
6668
"<b>", bold,
6769
"</b>", reset,
6870
"<h1>", bold,
@@ -251,7 +253,7 @@ type Answer struct {
251253
}
252254

253255
func (a *Answer) String() string {
254-
line := strings.Repeat("─", 80)
256+
line := strings.Repeat("─", terminalWidth)
255257
color := yellow
256258
if a.IsAccepted {
257259
color = green
@@ -264,6 +266,7 @@ func (a *Answer) String() string {
264266
%sDate: %s
265267
Link: %s%s
266268
%s
269+
267270
`,
268271
line,
269272
color, a.Score, reset, answerColor, bold, a.Author, reset,
@@ -282,7 +285,7 @@ type Result struct {
282285
}
283286

284287
func (r *Result) String() string {
285-
line := strings.Repeat("─", 80)
288+
line := strings.Repeat("─", terminalWidth)
286289
color := yellow
287290
if r.UpvoteCount < 0 {
288291
color = downvoted
@@ -307,6 +310,7 @@ func prepareText(text string) string {
307310

308311
func fmtText(text string) string {
309312
t := r.Replace(html.UnescapeString(text))
313+
t = strings.ReplaceAll(t, "<hr>", strings.Repeat("─", terminalWidth))
310314
t = divPattern.ReplaceAllString(t, "")
311315
t = aHrefPattern.ReplaceAllString(t, "\n - $2")
312316
t = bqPattern.ReplaceAllString(t, italic)
@@ -406,6 +410,12 @@ func GetAnswers(conf *Config,
406410
fetchResults func(*Config) (*GoogleSearchResult, error),
407411
fetchAnswers func(*Config, *GoogleSearchResult) (map[int]*Result, error),
408412
) (string, error) {
413+
var err error
414+
terminalWidth, _, err = term.GetSize(0)
415+
if err != nil {
416+
return "", err
417+
}
418+
terminalWidth = min(terminalWidth, 80)
409419
var answers strings.Builder
410420
style := styles.Get(conf.Style)
411421
if style == nil {

goso_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010
"strings"
1111
"testing"
12+
"time"
1213
)
1314

1415
func openFile(path string) (*os.File, func(), error) {
@@ -37,12 +38,17 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
3738
results := make(map[int]*Result)
3839
questions := make([]string, 0, len(gr.Items))
3940
for _, item := range gr.Items {
40-
question := item.Pagemap.Question[0]
41-
answerCount, _ := strconv.Atoi(question.Answercount)
42-
if answerCount == 0 {
43-
continue
41+
var upvoteCount int
42+
var dateCreated time.Time
43+
if len(item.Pagemap.Question) > 0 {
44+
question := item.Pagemap.Question[0]
45+
answerCount, _ := strconv.Atoi(question.Answercount)
46+
if answerCount == 0 {
47+
continue
48+
}
49+
upvoteCount, _ = strconv.Atoi(question.Upvotecount)
50+
dateCreated, _ = time.Parse("2006-01-02T15:04:05", question.Datecreated)
4451
}
45-
upvoteCount, _ := strconv.Atoi(question.Upvotecount)
4652
u, _ := netUrl.Parse(item.Link)
4753
questionStr := strings.Split(u.Path, "/")[2]
4854
questions = append(questions, questionStr)
@@ -52,6 +58,7 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
5258
Link: item.Link,
5359
QuestionId: questionId,
5460
UpvoteCount: upvoteCount,
61+
Date: dateCreated,
5562
}
5663
}
5764
_ = strings.Join(questions, ";")
@@ -73,9 +80,12 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
7380
}
7481
result.Answers = append(result.Answers,
7582
&Answer{
76-
Score: item.Score,
77-
Body: item.Body,
78-
Link: fmt.Sprintf("https://stackoverflow.com/a/%d", item.AnswerID),
83+
Author: item.Owner.DisplayName,
84+
Score: item.Score,
85+
Body: item.Body,
86+
Link: fmt.Sprintf("https://stackoverflow.com/a/%d", item.AnswerID),
87+
IsAccepted: item.IsAccepted,
88+
Date: time.Unix(int64(item.CreationDate), 0).UTC(),
7989
})
8090
}
8191
return results, nil

0 commit comments

Comments
 (0)