Skip to content

Commit

Permalink
Horizontal lines length now depends on terminal width (now looks bett…
Browse files Browse the repository at this point in the history
…er in Termux)
  • Loading branch information
shadowy-pycoder committed Nov 14, 2024
1 parent a12340b commit c418d50
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module github.com/shadowy-pycoder/goso

go 1.23.0

require github.com/alecthomas/chroma/v2 v2.14.0
require (
github.com/alecthomas/chroma/v2 v2.14.0
golang.org/x/term v0.26.0
)

require github.com/dlclark/regexp2 v1.11.0 // indirect
require (
github.com/dlclark/regexp2 v1.11.0 // indirect
golang.org/x/sys v0.27.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxK
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
20 changes: 15 additions & 5 deletions goso.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"golang.org/x/term"
)

const (
Expand All @@ -38,8 +39,9 @@ const (
)

var (
codeStartIdx int
codeEndIdx int
codeStartIdx int
codeEndIdx int
terminalWidth int
// https://meta.stackexchange.com/questions/1777/what-html-tags-are-allowed-on-stack-exchange-sites
codePattern = regexp.MustCompile(`<pre\s.*?>`)
aHrefPattern = regexp.MustCompile(`(?s)<a\s+(?:[^>]*?\s+)?href=(["'])?([^\'" >]+)(.*?)?</a>`)
Expand All @@ -62,7 +64,7 @@ var (
"</ol>", "",
"<li>", " - ",
"</li>", "",
"<hr>", "────────────────────────────────────────────────────────────────────────────────",

"<b>", bold,
"</b>", reset,
"<h1>", bold,
Expand Down Expand Up @@ -251,7 +253,7 @@ type Answer struct {
}

func (a *Answer) String() string {
line := strings.Repeat("─", 80)
line := strings.Repeat("─", terminalWidth)
color := yellow
if a.IsAccepted {
color = green
Expand All @@ -264,6 +266,7 @@ func (a *Answer) String() string {
%sDate: %s
Link: %s%s
%s
`,
line,
color, a.Score, reset, answerColor, bold, a.Author, reset,
Expand All @@ -282,7 +285,7 @@ type Result struct {
}

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

func fmtText(text string) string {
t := r.Replace(html.UnescapeString(text))
t = strings.ReplaceAll(t, "<hr>", strings.Repeat("─", terminalWidth))
t = divPattern.ReplaceAllString(t, "")
t = aHrefPattern.ReplaceAllString(t, "\n - $2")
t = bqPattern.ReplaceAllString(t, italic)
Expand Down Expand Up @@ -406,6 +410,12 @@ func GetAnswers(conf *Config,
fetchResults func(*Config) (*GoogleSearchResult, error),
fetchAnswers func(*Config, *GoogleSearchResult) (map[int]*Result, error),
) (string, error) {
var err error
terminalWidth, _, err = term.GetSize(0)
if err != nil {
return "", err
}
terminalWidth = min(terminalWidth, 80)
var answers strings.Builder
style := styles.Get(conf.Style)
if style == nil {
Expand Down
26 changes: 18 additions & 8 deletions goso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"testing"
"time"
)

func openFile(path string) (*os.File, func(), error) {
Expand Down Expand Up @@ -37,12 +38,17 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
results := make(map[int]*Result)
questions := make([]string, 0, len(gr.Items))
for _, item := range gr.Items {
question := item.Pagemap.Question[0]
answerCount, _ := strconv.Atoi(question.Answercount)
if answerCount == 0 {
continue
var upvoteCount int
var dateCreated time.Time
if len(item.Pagemap.Question) > 0 {
question := item.Pagemap.Question[0]
answerCount, _ := strconv.Atoi(question.Answercount)
if answerCount == 0 {
continue
}
upvoteCount, _ = strconv.Atoi(question.Upvotecount)
dateCreated, _ = time.Parse("2006-01-02T15:04:05", question.Datecreated)
}
upvoteCount, _ := strconv.Atoi(question.Upvotecount)
u, _ := netUrl.Parse(item.Link)
questionStr := strings.Split(u.Path, "/")[2]
questions = append(questions, questionStr)
Expand All @@ -52,6 +58,7 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
Link: item.Link,
QuestionId: questionId,
UpvoteCount: upvoteCount,
Date: dateCreated,
}
}
_ = strings.Join(questions, ";")
Expand All @@ -73,9 +80,12 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
}
result.Answers = append(result.Answers,
&Answer{
Score: item.Score,
Body: item.Body,
Link: fmt.Sprintf("https://stackoverflow.com/a/%d", item.AnswerID),
Author: item.Owner.DisplayName,
Score: item.Score,
Body: item.Body,
Link: fmt.Sprintf("https://stackoverflow.com/a/%d", item.AnswerID),
IsAccepted: item.IsAccepted,
Date: time.Unix(int64(item.CreationDate), 0).UTC(),
})
}
return results, nil
Expand Down

0 comments on commit c418d50

Please sign in to comment.