From a7e72ac2c46a9fd2ecb52bf40b49f3fc0d51e0a8 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 29 May 2024 18:33:16 +0100 Subject: [PATCH] chore: split utility functions Split utility functions into separate files. --- internal/utilities/browser.go | 24 ++++++ internal/utilities/format.go | 36 +++++++++ internal/utilities/html.go | 38 +++++++++ internal/utilities/utilities.go | 136 -------------------------------- internal/utilities/wrap.go | 55 +++++++++++++ 5 files changed, 153 insertions(+), 136 deletions(-) create mode 100644 internal/utilities/browser.go create mode 100644 internal/utilities/format.go create mode 100644 internal/utilities/html.go delete mode 100644 internal/utilities/utilities.go create mode 100644 internal/utilities/wrap.go diff --git a/internal/utilities/browser.go b/internal/utilities/browser.go new file mode 100644 index 0000000..53ea1eb --- /dev/null +++ b/internal/utilities/browser.go @@ -0,0 +1,24 @@ +package utilities + +import ( + "os/exec" + "runtime" +) + +func OpenLink(url string) { + var open string + //envBrower := os.Getenv("BROWSER") + + switch { + //case len(envBrower) > 0: + // open = envBrower + case runtime.GOOS == "linux": + open = "xdg-open" + default: + return + } + + command := exec.Command(open, url) + + _ = command.Start() +} diff --git a/internal/utilities/format.go b/internal/utilities/format.go new file mode 100644 index 0000000..51dc026 --- /dev/null +++ b/internal/utilities/format.go @@ -0,0 +1,36 @@ +package utilities + +import ( + "regexp" + "time" +) + +const ( + reset = "\033[0m" + boldblue = "\033[34;1m" + boldmagenta = "\033[35;1m" + green = "\033[32m" +) + +func HeaderFormat(text string) string { + return boldblue + text + reset +} + +func FieldFormat(text string) string { + return green + text + reset +} + +func DisplayNameFormat(text string) string { + // use this pattern to remove all emoji strings + pattern := regexp.MustCompile(`\s:[A-Za-z0-9]*:`) + + return boldmagenta + pattern.ReplaceAllString(text, "") + reset +} + +func FormatDate(date time.Time) string { + return date.Local().Format("02 Jan 2006") +} + +func FormatTime(date time.Time) string { + return date.Local().Format("02 Jan 2006, 15:04 (MST)") +} diff --git a/internal/utilities/html.go b/internal/utilities/html.go new file mode 100644 index 0000000..928df9d --- /dev/null +++ b/internal/utilities/html.go @@ -0,0 +1,38 @@ +package utilities + +import ( + "strings" + + "golang.org/x/net/html" +) + +func StripHTMLTags(text string) string { + token := html.NewTokenizer(strings.NewReader(text)) + + var builder strings.Builder + + for { + tt := token.Next() + switch tt { + case html.ErrorToken: + return builder.String() + case html.TextToken: + text := token.Token().Data + builder.WriteString(text) + case html.StartTagToken, html.EndTagToken: + tag := token.Token().String() + builder.WriteString(transformTag(tag)) + } + } +} + +func transformTag(tag string) string { + switch tag { + case "
": + return "\n" + case "

", "

": + return "\n" + } + + return "" +} diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go deleted file mode 100644 index 31d7df1..0000000 --- a/internal/utilities/utilities.go +++ /dev/null @@ -1,136 +0,0 @@ -package utilities - -import ( - "os/exec" - "regexp" - "runtime" - "strings" - "time" - "unicode" - - "golang.org/x/net/html" -) - -const ( - reset = "\033[0m" - boldblue = "\033[34;1m" - boldmagenta = "\033[35;1m" - green = "\033[32m" -) - -func OpenLink(url string) { - var open string - - if runtime.GOOS == "linux" { - open = "xdg-open" - } else { - return - } - - command := exec.Command(open, url) - - _ = command.Start() -} - -func StripHTMLTags(text string) string { - token := html.NewTokenizer(strings.NewReader(text)) - - var builder strings.Builder - - for { - tt := token.Next() - switch tt { - case html.ErrorToken: - return builder.String() - case html.TextToken: - text := token.Token().Data - builder.WriteString(text) - case html.StartTagToken, html.EndTagToken: - tag := token.Token().String() - builder.WriteString(transformTag(tag)) - } - } -} - -func transformTag(tag string) string { - switch tag { - case "
": - return "\n" - case "

", "

": - return "\n" - } - - return "" -} - -func WrapLines(text, separator string, charLimit int) string { - lines := strings.Split(text, "\n") - - if len(lines) == 1 { - return wrapLine(lines[0], separator, charLimit) - } - - var builder strings.Builder - - for i, line := range lines { - builder.WriteString(wrapLine(line, separator, charLimit)) - - if i < len(lines)-1 { - builder.WriteString(separator) - } - } - - return builder.String() -} - -func wrapLine(line, separator string, charLimit int) string { - if len(line) <= charLimit { - return line - } - - leftcursor, rightcursor := 0, 0 - - var builder strings.Builder - - for rightcursor < (len(line) - charLimit) { - rightcursor += charLimit - - for !unicode.IsSpace(rune(line[rightcursor-1])) && (rightcursor > leftcursor) { - rightcursor-- - } - - if rightcursor == leftcursor { - rightcursor = leftcursor + charLimit - } - - builder.WriteString(line[leftcursor:rightcursor] + separator) - leftcursor = rightcursor - } - - builder.WriteString(line[rightcursor:]) - - return builder.String() -} - -func HeaderFormat(text string) string { - return boldblue + text + reset -} - -func FieldFormat(text string) string { - return green + text + reset -} - -func DisplayNameFormat(text string) string { - // use this pattern to remove all emoji strings - pattern := regexp.MustCompile(`\s:[A-Za-z0-9]*:`) - - return boldmagenta + pattern.ReplaceAllString(text, "") + reset -} - -func FormatDate(date time.Time) string { - return date.Local().Format("02 Jan 2006") -} - -func FormatTime(date time.Time) string { - return date.Local().Format("02 Jan 2006, 15:04 (MST)") -} diff --git a/internal/utilities/wrap.go b/internal/utilities/wrap.go new file mode 100644 index 0000000..87349cf --- /dev/null +++ b/internal/utilities/wrap.go @@ -0,0 +1,55 @@ +package utilities + +import ( + "strings" + "unicode" +) + +func WrapLines(text, separator string, charLimit int) string { + lines := strings.Split(text, "\n") + + if len(lines) == 1 { + return wrapLine(lines[0], separator, charLimit) + } + + var builder strings.Builder + + for i, line := range lines { + builder.WriteString(wrapLine(line, separator, charLimit)) + + if i < len(lines)-1 { + builder.WriteString(separator) + } + } + + return builder.String() +} + +func wrapLine(line, separator string, charLimit int) string { + if len(line) <= charLimit { + return line + } + + leftcursor, rightcursor := 0, 0 + + var builder strings.Builder + + for rightcursor < (len(line) - charLimit) { + rightcursor += charLimit + + for !unicode.IsSpace(rune(line[rightcursor-1])) && (rightcursor > leftcursor) { + rightcursor-- + } + + if rightcursor == leftcursor { + rightcursor = leftcursor + charLimit + } + + builder.WriteString(line[leftcursor:rightcursor] + separator) + leftcursor = rightcursor + } + + builder.WriteString(line[rightcursor:]) + + return builder.String() +}