diff --git a/ansi.go b/ansi.go index 6c0d2b3..5e625e5 100644 --- a/ansi.go +++ b/ansi.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/beevik/etree" - "github.com/charmbracelet/x/exp/term/ansi" + "github.com/charmbracelet/x/ansi" "github.com/mattn/go-runewidth" ) diff --git a/config.go b/config.go index cd572e6..68a3554 100644 --- a/config.go +++ b/config.go @@ -84,7 +84,6 @@ func expandPadding(p []float64, scale float64) []float64 { return []float64{p[top] * scale, p[right] * scale, p[bottom] * scale, p[left] * scale} default: return []float64{0, 0, 0, 0} - } } @@ -102,7 +101,7 @@ const ( var userConfigPath = filepath.Join(xdg.ConfigHome, "freeze", "user.json") func loadUserConfig() (fs.File, error) { - return os.Open(userConfigPath) + return os.Open(userConfigPath) //nolint: wrapcheck } func saveUserConfig(config Config) error { @@ -112,19 +111,19 @@ func saveUserConfig(config Config) error { err := os.MkdirAll(filepath.Dir(userConfigPath), os.ModePerm) if err != nil { - return err + return err //nolint: wrapcheck } f, err := os.Create(userConfigPath) if err != nil { - return err + return err //nolint: wrapcheck } b, err := json.Marshal(config) if err != nil { - return err + return err //nolint: wrapcheck } _, err = f.Write(b) printFilenameOutput(userConfigPath) - return err + return err //nolint: wrapcheck } diff --git a/cut_test.go b/cut_test.go index a493d08..497b0c2 100644 --- a/cut_test.go +++ b/cut_test.go @@ -46,5 +46,4 @@ func TestCut(t *testing.T) { t.Errorf("cut(%s, %v)", test.input, test.lines) } } - } diff --git a/error.go b/error.go index 4bf7fd0..82b96d9 100644 --- a/error.go +++ b/error.go @@ -7,8 +7,10 @@ import ( "github.com/charmbracelet/lipgloss" ) -var errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") -var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2) +var ( + errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") + errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2) +) func printError(title string, err error) { fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) diff --git a/font/font.go b/font/font.go index aaa4035..5f2b465 100644 --- a/font/font.go +++ b/font/font.go @@ -105,5 +105,10 @@ var JetBrainsMonoTTF []byte //go:embed JetBrainsMonoNL-Regular.ttf var JetBrainsMonoNLTTF []byte -var JetBrainsMono string = base64.StdEncoding.EncodeToString(JetBrainsMonoTTF) -var JetBrainsMonoNL string = base64.StdEncoding.EncodeToString(JetBrainsMonoNLTTF) +var ( + // JetBrainsMono font. + JetBrainsMono = base64.StdEncoding.EncodeToString(JetBrainsMonoTTF) + + // JetBrainsMonoNL font. + JetBrainsMonoNL = base64.StdEncoding.EncodeToString(JetBrainsMonoNLTTF) +) diff --git a/freeze_test.go b/freeze_test.go index 06b4e43..0443255 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -14,8 +14,10 @@ import ( const binary = "./test/freeze-test" -var update = flag.Bool("update", false, "update golden files") -var png = flag.Bool("png", false, "update pngs") +var ( + update = flag.Bool("update", false, "update golden files") + png = flag.Bool("png", false, "update pngs") +) func TestMain(m *testing.M) { flag.Parse() @@ -264,15 +266,15 @@ func TestFreezeConfigurations(t *testing.T) { if err != nil { t.Fatal("unable to remove output files") } - err = os.MkdirAll("test/output/svg", 0755) + err = os.MkdirAll("test/output/svg", 0o755) if err != nil { t.Fatal("unable to create output directory") } - err = os.MkdirAll("test/golden/svg", 0755) + err = os.MkdirAll("test/golden/svg", 0o755) if err != nil { t.Fatal("unable to create output directory") } - err = os.MkdirAll("test/output/png", 0755) + err = os.MkdirAll("test/output/png", 0o755) if err != nil { t.Fatal("unable to create output directory") } @@ -299,7 +301,7 @@ func TestFreezeConfigurations(t *testing.T) { } goldenfile := "test/golden/svg/" + tc.output + ".svg" if *update { - if err := os.WriteFile(goldenfile, got, 0644); err != nil { + if err := os.WriteFile(goldenfile, got, 0o644); err != nil { t.Log(err) t.Fatal("unexpected error") } diff --git a/help.go b/help.go index 38943bc..ed695ff 100644 --- a/help.go +++ b/help.go @@ -90,7 +90,6 @@ func printFlag(f *kong.Flag) { fmt.Print(" ", dashStyle.Render(" "), " ") fmt.Print(dashStyle.Render("--"), f.Name) fmt.Print(strings.Repeat(" ", space-len(f.Name))) - } help := highlighter.ReplaceAllString(f.Help, keywordStyle.Render("$1")+"\x1b[38;5;"+helpForeground+"m") fmt.Println(helpStyle.Render(help)) diff --git a/interactive.go b/interactive.go index e7916cf..094e517 100644 --- a/interactive.go +++ b/interactive.go @@ -29,18 +29,18 @@ func runForm(config *Config) (*Config, error) { theme := huh.ThemeCharm() theme.FieldSeparator = lipgloss.NewStyle() - theme.Blurred.TextInput.Text = theme.Blurred.TextInput.Text.Copy().Foreground(lipgloss.Color("243")) + theme.Blurred.TextInput.Text = theme.Blurred.TextInput.Text.Foreground(lipgloss.Color("243")) theme.Blurred.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1) theme.Blurred.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("7")).PaddingRight(1) theme.Focused.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1) theme.Focused.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("15")).PaddingRight(1) - theme.Focused.NoteTitle = theme.Focused.NoteTitle.Copy().Margin(1, 0) - theme.Blurred.NoteTitle = theme.Blurred.NoteTitle.Copy().Margin(1, 0) - theme.Blurred.Description = theme.Blurred.Description.Copy().Foreground(lipgloss.Color("0")) - theme.Focused.Description = theme.Focused.Description.Copy().Foreground(lipgloss.Color("7")) - theme.Blurred.Title = theme.Blurred.Title.Copy().Width(18).Foreground(lipgloss.Color("7")) - theme.Focused.Title = theme.Focused.Title.Copy().Width(18).Foreground(green).Bold(true) - theme.Blurred.SelectedOption = theme.Blurred.SelectedOption.Copy().Foreground(lipgloss.Color("243")) + theme.Focused.NoteTitle = theme.Focused.NoteTitle.Margin(1, 0) + theme.Blurred.NoteTitle = theme.Blurred.NoteTitle.Margin(1, 0) + theme.Blurred.Description = theme.Blurred.Description.Foreground(lipgloss.Color("0")) + theme.Focused.Description = theme.Focused.Description.Foreground(lipgloss.Color("7")) + theme.Blurred.Title = theme.Blurred.Title.Width(18).Foreground(lipgloss.Color("7")) + theme.Focused.Title = theme.Focused.Title.Width(18).Foreground(green).Bold(true) + theme.Blurred.SelectedOption = theme.Blurred.SelectedOption.Foreground(lipgloss.Color("243")) theme.Focused.SelectedOption = lipgloss.NewStyle().Foreground(green) theme.Focused.Base.BorderForeground(green) @@ -197,7 +197,7 @@ func runForm(config *Config) (*Config, error) { config.Shadow.Blur, _ = strconv.ParseFloat(shadowBlur, 64) config.Shadow.X, _ = strconv.ParseFloat(shadowX, 64) config.Shadow.Y, _ = strconv.ParseFloat(shadowY, 64) - return config, err + return config, err //nolint: wrapcheck } func validateMargin(s string) error { @@ -256,6 +256,7 @@ func validateColor(s string) error { } func parsePadding(v string) []float64 { + //nolint: prealloc var values []float64 for _, p := range strings.Fields(v) { pi, _ := strconv.ParseFloat(p, 64) // already validated diff --git a/main.go b/main.go index 56f9892..5a710d7 100644 --- a/main.go +++ b/main.go @@ -19,8 +19,8 @@ import ( "github.com/charmbracelet/freeze/svg" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/log" - "github.com/charmbracelet/x/exp/term/ansi" - "github.com/charmbracelet/x/exp/term/ansi/parser" + "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/x/ansi/parser" "github.com/mattn/go-isatty" "github.com/muesli/reflow/wordwrap" ) @@ -60,9 +60,9 @@ func main() { printErrorFatal("Invalid Usage", err) } + //nolint: nestif if len(ctx.Args) > 0 { switch ctx.Args[0] { - case "version": if Version == "" { if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { diff --git a/png.go b/png.go index af0348f..b7d0184 100644 --- a/png.go +++ b/png.go @@ -11,15 +11,15 @@ import ( "github.com/kanrichan/resvg-go" ) -func libsvgConvert(doc *etree.Document, w, h float64, output string) error { +func libsvgConvert(doc *etree.Document, _, _ float64, output string) error { _, err := exec.LookPath("rsvg-convert") if err != nil { - return err + return err //nolint: wrapcheck } svg, err := doc.WriteToBytes() if err != nil { - return err + return err //nolint: wrapcheck } // rsvg-convert is installed use that to convert the SVG to PNG, @@ -27,26 +27,26 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string) error { rsvgConvert := exec.Command("rsvg-convert", "-o", output) rsvgConvert.Stdin = bytes.NewReader(svg) err = rsvgConvert.Run() - return err + return err //nolint: wrapcheck } func resvgConvert(doc *etree.Document, w, h float64, output string) error { svg, err := doc.WriteToBytes() if err != nil { - return err + return err //nolint: wrapcheck } worker, err := resvg.NewDefaultWorker(context.Background()) if err != nil { printErrorFatal("Unable to write output", err) } - defer worker.Close() + defer worker.Close() //nolint: errcheck fontdb, err := worker.NewFontDBDefault() if err != nil { printErrorFatal("Unable to write output", err) } - defer fontdb.Close() + defer fontdb.Close() //nolint: errcheck err = fontdb.LoadFontData(font.JetBrainsMonoTTF) if err != nil { printErrorFatal("Unable to load font", err) @@ -61,7 +61,7 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error { printError("Unable to write output", err) os.Exit(1) } - defer pixmap.Close() + defer pixmap.Close() //nolint: errcheck tree, err := worker.NewTreeFromData(svg, &resvg.Options{ Dpi: 192, @@ -75,24 +75,24 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error { printError("Unable to write output", err) os.Exit(1) } - defer tree.Close() + defer tree.Close() //nolint: errcheck err = tree.ConvertText(fontdb) if err != nil { - return err + return err //nolint: wrapcheck } err = tree.Render(resvg.TransformIdentity(), pixmap) if err != nil { - return err + return err //nolint: wrapcheck } png, err := pixmap.EncodePNG() if err != nil { - return err + return err //nolint: wrapcheck } - err = os.WriteFile(output, png, 0644) + err = os.WriteFile(output, png, 0o600) if err != nil { - return err + return err //nolint: wrapcheck } - return err + return err //nolint: wrapcheck } diff --git a/pty.go b/pty.go index 7c782b8..42fd0fd 100644 --- a/pty.go +++ b/pty.go @@ -19,6 +19,7 @@ import ( // The returned file is the pty's file descriptor and must be closed by the // caller. func (cfg Config) runInPty(c *exec.Cmd) (*os.File, error) { + //nolint: wrapcheck return pty.StartWithAttrs(c, &pty.Winsize{ Cols: 80, Rows: 10, @@ -29,17 +30,17 @@ func (cfg Config) runInPty(c *exec.Cmd) (*os.File, error) { func executeCommand(config Config) (string, error) { args, err := shellwords.Parse(config.Execute) if err != nil { - return "", err + return "", err //nolint: wrapcheck } ctx, cancel := context.WithTimeout(context.Background(), config.ExecuteTimeout) defer cancel() - cmd := exec.CommandContext(ctx, args[0], args[1:]...) + cmd := exec.CommandContext(ctx, args[0], args[1:]...) //nolint: gosec pty, err := config.runInPty(cmd) if err != nil { return "", err } - defer pty.Close() + defer pty.Close() //nolint: errcheck var out bytes.Buffer go func() { _, _ = io.Copy(&out, pty) @@ -47,7 +48,7 @@ func executeCommand(config Config) (string, error) { err = cmd.Wait() if err != nil { - return "", err + return "", err //nolint: wrapcheck } return out.String(), nil } diff --git a/svg/svg.go b/svg/svg.go index a12f9cb..729aece 100644 --- a/svg/svg.go +++ b/svg/svg.go @@ -63,7 +63,7 @@ func AddCornerRadius(e *etree.Element, radius float64) { e.CreateAttr("ry", fmt.Sprintf("%.2f", radius)) } -// Move moves the given element to the (x, y) position +// Move moves the given element to the (x, y) position. func Move(e *etree.Element, x, y float64) { e.CreateAttr("x", fmt.Sprintf("%.2fpx", x)) e.CreateAttr("y", fmt.Sprintf("%.2fpx", y)) @@ -113,7 +113,7 @@ func GetDimensions(element *etree.Element) (int, int) { } // dimensionToInt takes a string and returns the integer value. -// e.g. "500px" -> 500 +// e.g. "500px" -> 500. func dimensionToInt(px string) int { d := strings.TrimSuffix(px, "px") v, _ := strconv.ParseInt(d, 10, 64) diff --git a/test/input/tab.go b/test/input/tab.go index b46679a..ab4bdb9 100644 --- a/test/input/tab.go +++ b/test/input/tab.go @@ -2,7 +2,7 @@ package main // freeze/issues/50 -type Config struct { +type Config struct { //nolint: revive Telegram struct { Token string `env:"TG_TOKEN"` ChatID string `env:"TG_CHAT"` @@ -20,7 +20,7 @@ type Config struct { Debug bool } -func Load() (*Config, error) { +func Load() (*Config, error) { //nolint: revive var c Config var err error return &c, err