From ae82716afa0fb33c1f65793079c19e8f576a3f9e Mon Sep 17 00:00:00 2001 From: Marcus Ramberg Date: Mon, 29 Dec 2025 03:48:56 +0100 Subject: [PATCH] core: apply gopls automatic modernizers (#1198) --- core/cmd/dms/commands_brightness.go | 2 +- core/cmd/dms/commands_features.go | 4 +- core/cmd/dms/commands_greeter.go | 10 ++--- core/cmd/dms/dpms_client.go | 10 +---- core/cmd/dms/shell.go | 15 +++---- core/cmd/dms/utils.go | 9 +---- core/internal/colorpicker/picker.go | 15 ++----- core/internal/colorpicker/state.go | 4 +- core/internal/colorpicker/state_test.go | 40 +++++++++---------- core/internal/config/dms.go | 4 +- core/internal/dank16/dank16.go | 2 +- core/internal/dank16/dank16_test.go | 2 +- core/internal/distros/arch.go | 12 +++--- core/internal/distros/manual_packages.go | 28 ++++++------- core/internal/keybinds/discovery.go | 2 +- .../keybinds/providers/jsonfile_test.go | 10 ++--- 16 files changed, 71 insertions(+), 98 deletions(-) diff --git a/core/cmd/dms/commands_brightness.go b/core/cmd/dms/commands_brightness.go index 5b736885..8e7aafd5 100644 --- a/core/cmd/dms/commands_brightness.go +++ b/core/cmd/dms/commands_brightness.go @@ -179,7 +179,7 @@ func runBrightnessList(cmd *cobra.Command, args []string) { fmt.Printf("%-*s %-12s %-*s %s\n", idPad, "Device", "Class", namePad, "Name", "Brightness") sepLen := idPad + 2 + 12 + 2 + namePad + 2 + 15 - for i := 0; i < sepLen; i++ { + for range sepLen { fmt.Print("─") } fmt.Println() diff --git a/core/cmd/dms/commands_features.go b/core/cmd/dms/commands_features.go index 8dae3c52..e1acc421 100644 --- a/core/cmd/dms/commands_features.go +++ b/core/cmd/dms/commands_features.go @@ -377,7 +377,7 @@ func updateDMSBinary() error { } version := "" - for _, line := range strings.Split(string(output), "\n") { + for line := range strings.SplitSeq(string(output), "\n") { if strings.Contains(line, "\"tag_name\"") { parts := strings.Split(line, "\"") if len(parts) >= 4 { @@ -443,7 +443,7 @@ func updateDMSBinary() error { decompressedPath := filepath.Join(tempDir, "dms") - if err := os.Chmod(decompressedPath, 0755); err != nil { + if err := os.Chmod(decompressedPath, 0o755); err != nil { return fmt.Errorf("failed to make binary executable: %w", err) } diff --git a/core/cmd/dms/commands_greeter.go b/core/cmd/dms/commands_greeter.go index b6f0b941..b4dd872a 100644 --- a/core/cmd/dms/commands_greeter.go +++ b/core/cmd/dms/commands_greeter.go @@ -211,8 +211,8 @@ func checkGroupExists(groupName string) bool { return false } - lines := strings.Split(string(data), "\n") - for _, line := range lines { + lines := strings.SplitSeq(string(data), "\n") + for line := range lines { if strings.HasPrefix(line, groupName+":") { return true } @@ -521,7 +521,7 @@ func enableGreeter() error { newConfig := strings.Join(finalLines, "\n") tmpFile := "/tmp/greetd-config.toml" - if err := os.WriteFile(tmpFile, []byte(newConfig), 0644); err != nil { + if err := os.WriteFile(tmpFile, []byte(newConfig), 0o644); err != nil { return fmt.Errorf("failed to write temp config: %w", err) } @@ -592,8 +592,8 @@ func checkGreeterStatus() error { if data, err := os.ReadFile(configPath); err == nil { configContent := string(data) if strings.Contains(configContent, "dms-greeter") { - lines := strings.Split(configContent, "\n") - for _, line := range lines { + lines := strings.SplitSeq(configContent, "\n") + for line := range lines { trimmed := strings.TrimSpace(line) if strings.HasPrefix(trimmed, "command =") || strings.HasPrefix(trimmed, "command=") { parts := strings.SplitN(trimmed, "=", 2) diff --git a/core/cmd/dms/dpms_client.go b/core/cmd/dms/dpms_client.go index 7087bb66..98f66d96 100644 --- a/core/cmd/dms/dpms_client.go +++ b/core/cmd/dms/dpms_client.go @@ -87,20 +87,14 @@ func newDPMSClient() (*dpmsClient, error) { switch e.Interface { case wlr_output_power.ZwlrOutputPowerManagerV1InterfaceName: powerMgr := wlr_output_power.NewZwlrOutputPowerManagerV1(c.ctx) - version := e.Version - if version > 1 { - version = 1 - } + version := min(e.Version, 1) if err := registry.Bind(e.Name, e.Interface, version, powerMgr); err == nil { c.powerMgr = powerMgr } case "wl_output": output := wlclient.NewOutput(c.ctx) - version := e.Version - if version > 4 { - version = 4 - } + version := min(e.Version, 4) if err := registry.Bind(e.Name, e.Interface, version, output); err == nil { outputID := fmt.Sprintf("output-%d", output.ID()) state := &outputState{ diff --git a/core/cmd/dms/shell.go b/core/cmd/dms/shell.go index 1f8e888b..05ca0de1 100644 --- a/core/cmd/dms/shell.go +++ b/core/cmd/dms/shell.go @@ -7,6 +7,7 @@ import ( "os/exec" "os/signal" "path/filepath" + "slices" "strconv" "strings" "syscall" @@ -371,13 +372,7 @@ func killShell() { func runShellDaemon(session bool) { isSessionManaged = session - isDaemonChild := false - for _, arg := range os.Args { - if arg == "--daemon-child" { - isDaemonChild = true - break - } - } + isDaemonChild := slices.Contains(os.Args, "--daemon-child") if !isDaemonChild { fmt.Fprintf(os.Stderr, "dms %s\n", Version) @@ -534,9 +529,9 @@ func runShellDaemon(session bool) { func parseTargetsFromIPCShowOutput(output string) ipcTargets { targets := make(ipcTargets) var currentTarget string - for _, line := range strings.Split(output, "\n") { - if strings.HasPrefix(line, "target ") { - currentTarget = strings.TrimSpace(strings.TrimPrefix(line, "target ")) + for line := range strings.SplitSeq(output, "\n") { + if after, ok := strings.CutPrefix(line, "target "); ok { + currentTarget = strings.TrimSpace(after) targets[currentTarget] = make(map[string][]string) } if strings.HasPrefix(line, " function") && currentTarget != "" { diff --git a/core/cmd/dms/utils.go b/core/cmd/dms/utils.go index aa0269ce..9a2163c0 100644 --- a/core/cmd/dms/utils.go +++ b/core/cmd/dms/utils.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os/exec" + "slices" "strings" ) @@ -36,13 +37,7 @@ func checkSystemdServiceEnabled(serviceName string) (string, bool, error) { if err != nil { knownStates := []string{"disabled", "masked", "masked-runtime", "not-found", "enabled", "enabled-runtime", "static", "indirect", "alias"} - isKnownState := false - for _, known := range knownStates { - if stateStr == known { - isKnownState = true - break - } - } + isKnownState := slices.Contains(knownStates, stateStr) if !isKnownState { return stateStr, false, fmt.Errorf("systemctl is-enabled failed: %w (output: %s)", err, stateStr) diff --git a/core/internal/colorpicker/picker.go b/core/internal/colorpicker/picker.go index bd5dff17..71ab3281 100644 --- a/core/internal/colorpicker/picker.go +++ b/core/internal/colorpicker/picker.go @@ -221,10 +221,7 @@ func (p *Picker) handleGlobal(e client.RegistryGlobalEvent) { case client.OutputInterfaceName: output := client.NewOutput(p.ctx) - version := e.Version - if version > 4 { - version = 4 - } + version := min(e.Version, 4) if err := p.registry.Bind(e.Name, e.Interface, version, output); err == nil { p.outputsMu.Lock() p.outputs[e.Name] = &Output{ @@ -239,20 +236,14 @@ func (p *Picker) handleGlobal(e client.RegistryGlobalEvent) { case wlr_layer_shell.ZwlrLayerShellV1InterfaceName: layerShell := wlr_layer_shell.NewZwlrLayerShellV1(p.ctx) - version := e.Version - if version > 4 { - version = 4 - } + version := min(e.Version, 4) if err := p.registry.Bind(e.Name, e.Interface, version, layerShell); err == nil { p.layerShell = layerShell } case wlr_screencopy.ZwlrScreencopyManagerV1InterfaceName: screencopy := wlr_screencopy.NewZwlrScreencopyManagerV1(p.ctx) - version := e.Version - if version > 3 { - version = 3 - } + version := min(e.Version, 3) if err := p.registry.Bind(e.Name, e.Interface, version, screencopy); err == nil { p.screencopy = screencopy } diff --git a/core/internal/colorpicker/state.go b/core/internal/colorpicker/state.go index d9278a3f..ed6e8cf1 100644 --- a/core/internal/colorpicker/state.go +++ b/core/internal/colorpicker/state.go @@ -1157,7 +1157,7 @@ func drawGlyph(data []byte, stride, width, height, x, y int, r rune, col Color, rOff, bOff = 2, 0 } - for row := 0; row < fontH; row++ { + for row := range fontH { yy := y + row if yy < 0 || yy >= height { continue @@ -1165,7 +1165,7 @@ func drawGlyph(data []byte, stride, width, height, x, y int, r rune, col Color, rowPattern := g[row] dstRowOff := yy * stride - for colIdx := 0; colIdx < fontW; colIdx++ { + for colIdx := range fontW { if (rowPattern & (1 << (fontW - 1 - colIdx))) == 0 { continue } diff --git a/core/internal/colorpicker/state_test.go b/core/internal/colorpicker/state_test.go index 6daef67f..a6599aa6 100644 --- a/core/internal/colorpicker/state_test.go +++ b/core/internal/colorpicker/state_test.go @@ -14,11 +14,11 @@ func TestSurfaceState_ConcurrentPointerMotion(t *testing.T) { const goroutines = 50 const iterations = 100 - for i := 0; i < goroutines; i++ { + for i := range goroutines { wg.Add(1) go func(id int) { defer wg.Done() - for j := 0; j < iterations; j++ { + for j := range iterations { s.OnPointerMotion(float64(id*10+j), float64(id*10+j)) } }(i) @@ -34,21 +34,21 @@ func TestSurfaceState_ConcurrentScaleAccess(t *testing.T) { const goroutines = 30 const iterations = 100 - for i := 0; i < goroutines/2; i++ { + for i := range goroutines / 2 { wg.Add(1) go func(id int) { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { s.SetScale(int32(id%3 + 1)) } }(i) } - for i := 0; i < goroutines/2; i++ { + for range goroutines / 2 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { scale := s.Scale() assert.GreaterOrEqual(t, scale, int32(1)) } @@ -65,21 +65,21 @@ func TestSurfaceState_ConcurrentLogicalSize(t *testing.T) { const goroutines = 20 const iterations = 100 - for i := 0; i < goroutines/2; i++ { + for i := range goroutines / 2 { wg.Add(1) go func(id int) { defer wg.Done() - for j := 0; j < iterations; j++ { + for j := range iterations { _ = s.OnLayerConfigure(1920+id, 1080+j) } }(i) } - for i := 0; i < goroutines/2; i++ { + for range goroutines / 2 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { w, h := s.LogicalSize() _ = w _ = h @@ -97,31 +97,31 @@ func TestSurfaceState_ConcurrentIsDone(t *testing.T) { const goroutines = 30 const iterations = 100 - for i := 0; i < goroutines/3; i++ { + for range goroutines / 3 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { s.OnPointerButton(0x110, 1) } }() } - for i := 0; i < goroutines/3; i++ { + for range goroutines / 3 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { s.OnKey(1, 1) } }() } - for i := 0; i < goroutines/3; i++ { + for range goroutines / 3 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { picked, cancelled := s.IsDone() _ = picked _ = cancelled @@ -139,11 +139,11 @@ func TestSurfaceState_ConcurrentIsReady(t *testing.T) { const goroutines = 20 const iterations = 100 - for i := 0; i < goroutines; i++ { + for range goroutines { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { _ = s.IsReady() } }() @@ -159,11 +159,11 @@ func TestSurfaceState_ConcurrentSwapBuffers(t *testing.T) { const goroutines = 20 const iterations = 100 - for i := 0; i < goroutines; i++ { + for range goroutines { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < iterations; j++ { + for range iterations { s.SwapBuffers() } }() diff --git a/core/internal/config/dms.go b/core/internal/config/dms.go index 6fa6aec7..7842fa58 100644 --- a/core/internal/config/dms.go +++ b/core/internal/config/dms.go @@ -21,7 +21,7 @@ func LocateDMSConfig() (string, error) { dataDirs = "/usr/local/share:/usr/share" } - for _, dir := range strings.Split(dataDirs, ":") { + for dir := range strings.SplitSeq(dataDirs, ":") { if dir != "" { primaryPaths = append(primaryPaths, filepath.Join(dir, "quickshell", "dms")) } @@ -33,7 +33,7 @@ func LocateDMSConfig() (string, error) { configDirs = "/etc/xdg" } - for _, dir := range strings.Split(configDirs, ":") { + for dir := range strings.SplitSeq(configDirs, ":") { if dir != "" { primaryPaths = append(primaryPaths, filepath.Join(dir, "quickshell", "dms")) } diff --git a/core/internal/dank16/dank16.go b/core/internal/dank16/dank16.go index 33d6a3c4..413d1461 100644 --- a/core/internal/dank16/dank16.go +++ b/core/internal/dank16/dank16.go @@ -345,7 +345,7 @@ func EnsureContrastDPSLstar(hexColor, hexBg string, minLc float64, isLightMode b } step := 0.5 - for i := 0; i < 120; i++ { + for range 120 { Lf = math.Max(0, math.Min(100, Lf+dir*step)) cand := labToHex(Lf, af, bf) if DeltaPhiStarContrast(cand, hexBg, isLightMode) >= minLc { diff --git a/core/internal/dank16/dank16_test.go b/core/internal/dank16/dank16_test.go index b09332af..b539e2f9 100644 --- a/core/internal/dank16/dank16_test.go +++ b/core/internal/dank16/dank16_test.go @@ -658,7 +658,7 @@ func TestContrastAlgorithmComparison(t *testing.T) { } differentCount := 0 - for i := 0; i < 16; i++ { + for i := range 16 { if wcagColors[i].Hex != dpsColors[i].Hex { differentCount++ } diff --git a/core/internal/distros/arch.go b/core/internal/distros/arch.go index 2a5daa7c..59913a86 100644 --- a/core/internal/distros/arch.go +++ b/core/internal/distros/arch.go @@ -7,6 +7,7 @@ import ( "os/exec" "path/filepath" "runtime" + "slices" "strings" "github.com/AvengeMedia/DankMaterialShell/core/internal/deps" @@ -514,12 +515,9 @@ func (a *ArchDistribution) reorderAURPackages(packages []string) []string { dmsShell = append(dmsShell, pkg) } else { isDep := false - for _, dep := range dmsDepencies { - if pkg == dep { - deps = append(deps, pkg) - isDep = true - break - } + if slices.Contains(dmsDepencies, pkg) { + deps = append(deps, pkg) + isDep = true } if !isDep { others = append(others, pkg) @@ -545,7 +543,7 @@ func (a *ArchDistribution) installSingleAURPackage(ctx context.Context, pkg, sud a.log(fmt.Sprintf("Warning: failed to clean existing cache for %s: %v", pkg, err)) } - if err := os.MkdirAll(buildDir, 0755); err != nil { + if err := os.MkdirAll(buildDir, 0o755); err != nil { return fmt.Errorf("failed to create build directory: %w", err) } defer func() { diff --git a/core/internal/distros/manual_packages.go b/core/internal/distros/manual_packages.go index 13cb6f00..2f38161a 100644 --- a/core/internal/distros/manual_packages.go +++ b/core/internal/distros/manual_packages.go @@ -18,8 +18,8 @@ type ManualPackageInstaller struct { // parseLatestTagFromGitOutput parses git ls-remote output and returns the latest tag func (m *ManualPackageInstaller) parseLatestTagFromGitOutput(output string) string { - lines := strings.Split(output, "\n") - for _, line := range lines { + lines := strings.SplitSeq(output, "\n") + for line := range lines { if strings.Contains(line, "refs/tags/") && !strings.Contains(line, "^{}") { parts := strings.Split(line, "refs/tags/") if len(parts) > 1 { @@ -103,12 +103,12 @@ func (m *ManualPackageInstaller) installDgop(ctx context.Context, sudoPassword s } cacheDir := filepath.Join(homeDir, ".cache", "dankinstall") - if err := os.MkdirAll(cacheDir, 0755); err != nil { + if err := os.MkdirAll(cacheDir, 0o755); err != nil { return fmt.Errorf("failed to create cache directory: %w", err) } tmpDir := filepath.Join(cacheDir, "dgop-build") - if err := os.MkdirAll(tmpDir, 0755); err != nil { + if err := os.MkdirAll(tmpDir, 0o755); err != nil { return fmt.Errorf("failed to create temp directory: %w", err) } defer os.RemoveAll(tmpDir) @@ -160,10 +160,10 @@ func (m *ManualPackageInstaller) installNiri(ctx context.Context, sudoPassword s homeDir, _ := os.UserHomeDir() buildDir := filepath.Join(homeDir, ".cache", "dankinstall", "niri-build") tmpDir := filepath.Join(homeDir, ".cache", "dankinstall", "tmp") - if err := os.MkdirAll(buildDir, 0755); err != nil { + if err := os.MkdirAll(buildDir, 0o755); err != nil { return fmt.Errorf("failed to create build directory: %w", err) } - if err := os.MkdirAll(tmpDir, 0755); err != nil { + if err := os.MkdirAll(tmpDir, 0o755); err != nil { return fmt.Errorf("failed to create temp directory: %w", err) } defer func() { @@ -237,12 +237,12 @@ func (m *ManualPackageInstaller) installQuickshell(ctx context.Context, variant } cacheDir := filepath.Join(homeDir, ".cache", "dankinstall") - if err := os.MkdirAll(cacheDir, 0755); err != nil { + if err := os.MkdirAll(cacheDir, 0o755); err != nil { return fmt.Errorf("failed to create cache directory: %w", err) } tmpDir := filepath.Join(cacheDir, "quickshell-build") - if err := os.MkdirAll(tmpDir, 0755); err != nil { + if err := os.MkdirAll(tmpDir, 0o755); err != nil { return fmt.Errorf("failed to create temp directory: %w", err) } defer os.RemoveAll(tmpDir) @@ -273,7 +273,7 @@ func (m *ManualPackageInstaller) installQuickshell(ctx context.Context, variant } buildDir := tmpDir + "/build" - if err := os.MkdirAll(buildDir, 0755); err != nil { + if err := os.MkdirAll(buildDir, 0o755); err != nil { return fmt.Errorf("failed to create build directory: %w", err) } @@ -343,12 +343,12 @@ func (m *ManualPackageInstaller) installHyprland(ctx context.Context, sudoPasswo } cacheDir := filepath.Join(homeDir, ".cache", "dankinstall") - if err := os.MkdirAll(cacheDir, 0755); err != nil { + if err := os.MkdirAll(cacheDir, 0o755); err != nil { return fmt.Errorf("failed to create cache directory: %w", err) } tmpDir := filepath.Join(cacheDir, "hyprland-build") - if err := os.MkdirAll(tmpDir, 0755); err != nil { + if err := os.MkdirAll(tmpDir, 0o755); err != nil { return fmt.Errorf("failed to create temp directory: %w", err) } defer os.RemoveAll(tmpDir) @@ -406,12 +406,12 @@ func (m *ManualPackageInstaller) installGhostty(ctx context.Context, sudoPasswor } cacheDir := filepath.Join(homeDir, ".cache", "dankinstall") - if err := os.MkdirAll(cacheDir, 0755); err != nil { + if err := os.MkdirAll(cacheDir, 0o755); err != nil { return fmt.Errorf("failed to create cache directory: %w", err) } tmpDir := filepath.Join(cacheDir, "ghostty-build") - if err := os.MkdirAll(tmpDir, 0755); err != nil { + if err := os.MkdirAll(tmpDir, 0o755); err != nil { return fmt.Errorf("failed to create temp directory: %w", err) } defer os.RemoveAll(tmpDir) @@ -528,7 +528,7 @@ func (m *ManualPackageInstaller) installDankMaterialShell(ctx context.Context, v } configDir := filepath.Dir(dmsPath) - if err := os.MkdirAll(configDir, 0755); err != nil { + if err := os.MkdirAll(configDir, 0o755); err != nil { return fmt.Errorf("failed to create quickshell config directory: %w", err) } diff --git a/core/internal/keybinds/discovery.go b/core/internal/keybinds/discovery.go index ef8c1477..49909755 100644 --- a/core/internal/keybinds/discovery.go +++ b/core/internal/keybinds/discovery.go @@ -23,7 +23,7 @@ func DefaultDiscoveryConfig() *DiscoveryConfig { configDirs := os.Getenv("XDG_CONFIG_DIRS") if configDirs != "" { - for _, dir := range strings.Split(configDirs, ":") { + for dir := range strings.SplitSeq(configDirs, ":") { if dir != "" { searchPaths = append(searchPaths, filepath.Join(dir, "DankMaterialShell", "cheatsheets")) } diff --git a/core/internal/keybinds/providers/jsonfile_test.go b/core/internal/keybinds/providers/jsonfile_test.go index 02d9c6f7..ded43853 100644 --- a/core/internal/keybinds/providers/jsonfile_test.go +++ b/core/internal/keybinds/providers/jsonfile_test.go @@ -12,7 +12,7 @@ func TestNewJSONFileProvider(t *testing.T) { tmpDir := t.TempDir() testFile := filepath.Join(tmpDir, "test.json") - if err := os.WriteFile(testFile, []byte("{}"), 0644); err != nil { + if err := os.WriteFile(testFile, []byte("{}"), 0o644); err != nil { t.Fatalf("Failed to create test file: %v", err) } @@ -81,7 +81,7 @@ func TestJSONFileProviderGetCheatSheet(t *testing.T) { } }` - if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { + if err := os.WriteFile(testFile, []byte(content), 0o644); err != nil { t.Fatalf("Failed to write test file: %v", err) } @@ -135,7 +135,7 @@ func TestJSONFileProviderGetCheatSheetNoProvider(t *testing.T) { "binds": {} }` - if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { + if err := os.WriteFile(testFile, []byte(content), 0o644); err != nil { t.Fatalf("Failed to write test file: %v", err) } @@ -181,7 +181,7 @@ func TestJSONFileProviderFlatArrayBackwardsCompat(t *testing.T) { ] }` - if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { + if err := os.WriteFile(testFile, []byte(content), 0o644); err != nil { t.Fatalf("Failed to write test file: %v", err) } @@ -216,7 +216,7 @@ func TestJSONFileProviderInvalidJSON(t *testing.T) { tmpDir := t.TempDir() testFile := filepath.Join(tmpDir, "invalid.json") - if err := os.WriteFile(testFile, []byte("not valid json"), 0644); err != nil { + if err := os.WriteFile(testFile, []byte("not valid json"), 0o644); err != nil { t.Fatalf("Failed to write test file: %v", err) }