From f12b800a71e0c2e7a0b0fc834a4c0e923650b642 Mon Sep 17 00:00:00 2001 From: Seednode Date: Wed, 9 Nov 2022 23:33:13 -0600 Subject: [PATCH] Replaced const string template with strings.Builder, to be hopefully somewhat more readable while still being moderately efficient --- cmd/version.go | 2 +- cmd/web.go | 41 +++++++++++++++++++---------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/cmd/version.go b/cmd/version.go index 35394a0..c7f2146 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.24.0" +var Version = "0.24.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 7b71f11..783b89e 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -25,10 +25,6 @@ const ( LogDate string = `2006-01-02T15:04:05.000-07:00` Prefix string = `/src` RedirectStatusCode int = http.StatusSeeOther - - // You may not like it, but this is what peak string generation looks like. - RefreshScriptTemplate string = `` - HtmlTemplate string = `%v (%vx%v)Roulette selected: %v%v` ) type Filters struct { @@ -204,33 +200,34 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio queryParams, err := generateQueryParams(filters, r.URL.Query().Get("sort"), refreshInterval) - refreshScript := "" + var htmlBody strings.Builder + htmlBody.WriteString(``) + htmlBody.WriteString(``) + htmlBody.WriteString(fmt.Sprintf(`%v (%vx%v)`, + fileName, + dimensions.Width, + dimensions.Height)) + htmlBody.WriteString(``) + htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %v`, + queryParams, + generateFilePath(filePath), + dimensions.Width, + dimensions.Height, + fileName)) if refreshInterval != "0" { r, err := strconv.Atoi(refreshInterval) if err != nil { return err } refreshTimer := strconv.Itoa(r * 1000) - refreshScript = fmt.Sprintf(RefreshScriptTemplate, + htmlBody.WriteString(fmt.Sprintf(``, queryParams, - refreshTimer) + refreshTimer)) } + htmlBody.WriteString(``) - htmlBody := fmt.Sprintf(HtmlTemplate, - fileName, - dimensions.Width, - dimensions.Height, - queryParams, - generateFilePath(filePath), - dimensions.Width, - dimensions.Height, - fileName, - refreshScript, - ) - - formattedBody := gohtml.Format(htmlBody) - - _, err = io.WriteString(w, formattedBody) + _, err = io.WriteString(w, gohtml.Format(htmlBody.String())) if err != nil { return err }