From 09e780385ef4b970c09008d8c6d22fa0f01ce48a Mon Sep 17 00:00:00 2001 From: hedy Date: Thu, 19 Jan 2023 09:21:35 +0800 Subject: [PATCH] Display: Use Style colors --- client.go | 11 ++++++++++- display.go | 10 ++++++---- gemini.go | 14 +------------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/client.go b/client.go index 0aea231..a56f4a1 100644 --- a/client.go +++ b/client.go @@ -130,6 +130,15 @@ func (c *Client) DisplayPage(page *Page) { // ParseGeminiPage parses bytes in page in returns a rendered string for the // page func (c *Client) ParseGeminiPage(page *Page) string { + var ( + h1Style = c.style.gmiH1.Sprint + h2Style = c.style.gmiH2.Sprint + h3Style = c.style.gmiH3.Sprint + preStyle = c.style.gmiPre.Sprint + linkStyle = c.style.gmiLink.Sprint + quoteStyle = c.style.gmiQuote.Sprint + ) + termWidth, _, err := term.GetSize(0) if err != nil { // TODO do something @@ -162,7 +171,7 @@ func (c *Client) ParseGeminiPage(page *Page) string { // NOT doing this anymore! // (because it looked bad if quotes are continuous) // TODO: remove extra new lines in the end - rendered += ansiwrap.GreedyIndent(line, width, 1+sides, 3+sides) + "\n" + rendered += ansiwrap.GreedyIndent(quoteStyle(line), width, 1+sides, 3+sides) + "\n" } else if strings.HasPrefix(line, "* ") { // whitespace after * is mandatory // Using width - 3 because of 3 spaces " " indent at the start diff --git a/display.go b/display.go index 3974cf6..e02bf8c 100644 --- a/display.go +++ b/display.go @@ -27,10 +27,12 @@ var DefaultStyle = Style{ Prompt: color.FgCyan, StatusError: color.New(color.FgYellow), - gmiH1: color.New(color.Bold, color.Underline, color.FgYellow), - gmiH2: color.New(color.Bold, color.FgGreen), - gmiH3: color.New(color.FgMagenta), - gmiLink: color.New(color.FgBlue), + gmiH1: color.New(color.Bold, color.Underline, color.FgYellow), + gmiH2: color.New(color.Bold, color.FgMagenta), + gmiH3: color.New(color.FgHiGreen), + gmiPre: color.New(color.FgYellow), + gmiLink: color.New(color.FgBlue), + gmiQuote: color.New(color.Italic, color.FgGreen), } var ( diff --git a/gemini.go b/gemini.go index 58893f3..1d5702f 100644 --- a/gemini.go +++ b/gemini.go @@ -10,8 +10,6 @@ import ( //"errors" "net/url" "strings" - - "github.com/fatih/color" ) type GeminiResponse struct { @@ -30,16 +28,6 @@ type GeminiResponse struct { //ErrDecodeMetaFail = errors.New("failed to decode meta header") //) -var ( - h1Style = color.New(color.Bold).Add(color.Underline).Add(color.FgHiMagenta).SprintFunc() - h2Style = color.New(color.Bold).Add(color.FgGreen).SprintFunc() - h3Style = color.New(color.Bold).Add(color.FgMagenta).SprintFunc() - preStyle = color.New(color.FgYellow).SprintFunc() - linkStyle = color.New(color.FgBlue).SprintFunc() - // style only applied to first line for some reason, so removing it all together :P - // quoteStyle = color.New(color.Italic).SprintFunc() -) - // GeminiParsedURL fetches u and returns *GeminiResponse func GeminiParsedURL(u url.URL) (res *GeminiResponse, err error) { host := u.Host @@ -67,7 +55,7 @@ func GeminiParsedURL(u url.URL) (res *GeminiResponse, err error) { status, err := strconv.Atoi(parts[0]) if err != nil { conn.Close() - return res, errors.New("Invalid status code") + return res, errors.New("invalid status code") } meta := strings.Join(parts[1:], " ") res = &GeminiResponse{status, meta, reader, false, conn, false}