|
| 1 | +MND LINTER REMEDIATION PLAN (HIGHLY DETAILED) |
| 2 | + |
| 3 | +Objective |
| 4 | +- Enable and satisfy the mnd (magic number) linter across the codebase. |
| 5 | +- Replace repeated numeric literals with named constants. |
| 6 | +- Add targeted //nolint:mnd with concise justification where constants reduce clarity. |
| 7 | +- Preserve behavior 100%. No feature changes. |
| 8 | + |
| 9 | +Prerequisites |
| 10 | +- Ensure golangci-lint is available (already present: v2.5.0). |
| 11 | +- Work on a feature branch (e.g., mnd-remediation). |
| 12 | +- Build baseline: CGO_ENABLED=0 go build -o bin/spf ./src/cmd |
| 13 | +- Lint baseline: golangci-lint run --enable=mnd |
| 14 | + |
| 15 | +Step 1 — Enable mnd in existing linter config (uncomment only) |
| 16 | +1. File to edit: .golangci.yaml (do NOT add .golangci.yml). |
| 17 | +2. In the `linters.enable` list, locate the commented mnd entry: |
| 18 | + `# - mnd # detects magic numbers` and UNCOMMENT it to: `- mnd # detects magic numbers`. |
| 19 | + - Location: around lines 100–115 in .golangci.yaml, under `linters: enable:`. |
| 20 | +3. Do not change the existing `mnd:` settings block (already present at ~line 356). Keep it as-is. |
| 21 | +4. Exclusions: follow the repo’s current convention only if needed. |
| 22 | + - If tests begin flagging noisy mnd hits, append `mnd` to the existing per-path exclusion rule for tests: |
| 23 | + `.golangci.yaml -> exclusions.rules -> - path: '_test\.go' -> linters: [ … add mnd here … ]`. |
| 24 | + - Keep order/format consistent with current style. Do not add new exclusion sections. |
| 25 | + |
| 26 | +Step 2 — Add shared UI/layout constants |
| 27 | +1. Create file: src/internal/common/ui_consts.go |
| 28 | +2. Add the following constants with comments: |
| 29 | + - HelpKeyColumnWidth = 55 // width of help key column in CLI help |
| 30 | + - DefaultCLIContextTimeout = 5 * time.Second // default CLI context timeout |
| 31 | + - PanelPadding = 3 // rows reserved around file list for borders/header/footer |
| 32 | + - BorderPadding = 2 // rows/cols for outer border frame |
| 33 | + - InnerPadding = 4 // cols for inner content padding (truncate widths) |
| 34 | + - FooterGroupCols = 3 // columns per group in footer layout math |
| 35 | + - FilePanelMax = 10 // max number of file panels supported |
| 36 | + - MinWidthForRename = 18 // minimal width for rename input to render |
| 37 | + - ResponsiveWidthThreshold = 95 // width breakpoint for layout behavior |
| 38 | + - HeightBreakA = 30; HeightBreakB = 35; HeightBreakC = 40; HeightBreakD = 45 // stacked height breakpoints |
| 39 | + - ReRenderChunkDivisor = 100 // divisor for re-render throttling |
| 40 | +3. Import time at file top. Ensure package is common. |
| 41 | + |
| 42 | +Step 3 — CLI fixes (src/cmd/main.go) |
| 43 | +1. Replace 55 in fmt.Printf("%-*s %s\n", 55, ...) with common.HelpKeyColumnWidth in lines printing colored help entries (approx lines 52, 54, 56, 58, 60). |
| 44 | + - Add above the first print: // use shared help column width (mnd) |
| 45 | +2. Replace 5*time.Second in context.WithTimeout(..., 5*time.Second) with common.DefaultCLIContextTimeout (approx line 270). |
| 46 | + - Add comment: // shared CLI timeout (mnd) |
| 47 | + |
| 48 | +Step 4 — Core model/layout (src/internal/model.go) |
| 49 | +1. Replace literal 10 with common.FilePanelMax for file panel max check (approx line 237). |
| 50 | +2. Replace height threshold literals: |
| 51 | + - if height < 30 → if height < common.HeightBreakA |
| 52 | + - else if height < 35 → else if height < common.HeightBreakB |
| 53 | + - else if height < 40 → else if height < common.HeightBreakC |
| 54 | + - else if height < 45 → else if height < common.HeightBreakD |
| 55 | + - if m.fullHeight > 35 → > common.HeightBreakB |
| 56 | + - if m.fullWidth > 95 → > common.ResponsiveWidthThreshold |
| 57 | + - Add comment near block: // responsive layout breakpoints (mnd) |
| 58 | +3. Replace +2 with +common.BorderPadding for: |
| 59 | + - m.fileModel.filePreview.SetHeight(m.mainPanelHeight + 2) |
| 60 | + - Bottom/footer widths where +2 is used (search for SetHeight/SetDimensions with +2). |
| 61 | +4. Replace /3, /2 usages for modal sizing: |
| 62 | + - m.promptModal.SetMaxHeight(m.fullHeight / 3) |
| 63 | + - m.promptModal.SetWidth(m.fullWidth / 2) |
| 64 | + - m.zoxideModal.SetMaxHeight(m.fullHeight / 2) |
| 65 | + - m.zoxideModal.SetWidth(m.fullWidth / 2) |
| 66 | + Options: |
| 67 | + - Prefer constants ModalThird=3, ModalHalf=2 in common; OR |
| 68 | + - Keep divisions and add //nolint:mnd with comment “modal uses thirds/halves”. |
| 69 | +5. Replace -4 and -3 style paddings using constants when adjusting widths/heights in render/layout helpers: |
| 70 | + - Use common.InnerPadding for -4 |
| 71 | + - Use common.PanelPadding for -3 |
| 72 | +6. Replace m.fileModel.width < 18 with < common.MinWidthForRename (approx line 566). Add comment. |
| 73 | +7. Replace reRenderTime := int(float64(len(...))/100) with /common.ReRenderChunkDivisor (approx line 731). Add comment. |
| 74 | + |
| 75 | +Step 5 — Panel navigation & operations |
| 76 | +- src/internal/handle_panel_navigation.go: replace +2 with +common.BorderPadding when setting preview height (approx line 111). Add comment. |
| 77 | +- src/internal/handle_file_operations.go: replace width-4 with width-common.InnerPadding when creating rename input (approx line 100). Add comment. |
| 78 | + |
| 79 | +Step 6 — Rendering (src/internal/model_render.go) |
| 80 | +1. Replace +2 with +common.BorderPadding at: |
| 81 | + - FilePanelRenderer(mainPanelHeight+2, filePanelWidth+2, …) (approx line 65) |
| 82 | + - ClipboardRenderer(m.footerHeight+2, bottomWidth+2) (approx line 217) |
| 83 | +2. Replace filePanelWidth-4 with filePanelWidth-common.InnerPadding (approx line 77) |
| 84 | +3. Replace bottom width calc utils.FooterWidth(m.fullWidth + m.fullWidth%3 + 2): |
| 85 | + - Use %common.FooterGroupCols |
| 86 | + - Replace +2 with +common.BorderPadding (approx line 213) |
| 87 | +4. Replace -3 when truncating display width within metadata/preview draws with -common.PanelPadding (approx line 236). Add comment. |
| 88 | +5. Replace ModalWidth-4 with ModalWidth-common.InnerPadding (approx line 297). |
| 89 | +6. Replace panel.sortOptions.width-2 with -common.BorderPadding (approx line 457). |
| 90 | + |
| 91 | +Step 7 — Directory listing & sorting (src/internal/function.go) |
| 92 | +1. func panelElementHeight(mainPanelHeight int) int { return mainPanelHeight - 3 } |
| 93 | + - Replace 3 with common.PanelPadding (approx line 244). Add comment. |
| 94 | +2. In suffixRegexp.FindStringSubmatch(name); len(match) == 3 (approx line 294): |
| 95 | + - Keep 3 and add inline: //nolint:mnd — 3 = full match + 2 capture groups (regex) |
| 96 | + |
| 97 | +Step 8 — Preview subsystem |
| 98 | +- src/internal/ui/preview/model.go: replace 500*time.Millisecond with a named constant. |
| 99 | + Options: |
| 100 | + - Add in common: DefaultPreviewTimeout = 500*time.Millisecond |
| 101 | + - Or local const in preview package with comment. |
| 102 | + Add comment: // preview operation timeout (mnd) |
| 103 | + |
| 104 | +Step 9 — Image preview utils (src/pkg/file_preview/image_preview.go) |
| 105 | +1. Replace 100 with DefaultThumbnailWidth (const in this file). Comment: // default thumb width (mnd) |
| 106 | +2. Replace 5*time.Minute with DefaultCacheExpiration. Comment. |
| 107 | +3. Replace /2 on ticker with either const DividerTwo or //nolint:mnd “half expiration interval”. |
| 108 | +4. Replace 16, 8, 0xFF, 255 with named consts: Shift16, Shift8, MaskFF, OpaqueAlpha; add comment block explaining RGB math. |
| 109 | +5. Replace 8 in fmt.Sprintf("#%02x%02x%02x", uint8(r>>8), …) with Shift8. |
| 110 | + |
| 111 | +Step 10 — Image resize (src/pkg/file_preview/image_resize.go) |
| 112 | +1. Switch cases 2..8: |
| 113 | + - Prefer //nolint:mnd on each case with comment: // 2=low … 8=ultra quality levels |
| 114 | + - Or introduce Quality2..Quality8 if reused elsewhere. |
| 115 | +2. imaging.Fit(img, maxWidth, maxHeight*2, …): replace 2 with HeightScaleFactor const or //nolint:mnd with comment “fit scales height x2”. |
| 116 | + |
| 117 | +Step 11 — Kitty utils (src/pkg/file_preview/kitty.go) |
| 118 | +1. Replace 42 with KittyDefaultSeed |
| 119 | +2. Replace 31 with HashPrime |
| 120 | +3. Replace 0xFFFF with MaskFFFF |
| 121 | +4. Replace +1000 with NonZeroOffset |
| 122 | +5. Add one-line comments next to const block. |
| 123 | + |
| 124 | +Step 12 — Preview terminal metrics (src/pkg/file_preview/utils.go) |
| 125 | +1. Replace PixelsPerColumn: 8, PixelsPerRow: 16 with named consts (PixelsPerColumnDefault, PixelsPerRowDefault) and comment on typical terminal cell sizes. |
| 126 | + |
| 127 | +Step 13 — External path detection (optional cleanup) |
| 128 | +- src/internal/function.go:isExternalDiskPath |
| 129 | + - Create consts: TimeMachinePrefix, VolumesPrefix, MediaPrefixes (slice) |
| 130 | + - Use them in HasPrefix checks. Rationale: clarity; not necessarily for mnd. |
| 131 | + |
| 132 | +Step 14 — Commenting & //nolint practices |
| 133 | +- Only add //nolint:mnd where constants reduce clarity or are inherently part of API/math: |
| 134 | + - Regex submatch count (len(match)==3): add concise reason |
| 135 | + - Switch cases for fixed, human-defined quality levels (2..8): add mapping comment |
| 136 | + - Simple halves/thirds if not centralized: add “half/third sizing” comments |
| 137 | +- For every //nolint:mnd, add a short, explicit justification on the same line. |
| 138 | + |
| 139 | +Validation Checklist |
| 140 | +1. golangci-lint run --enable=mnd — should show a decreasing count; iterate until 0 or only justified //nolint sites remain. |
| 141 | +2. Build: CGO_ENABLED=0 go build -o bin/spf ./src/cmd |
| 142 | +3. Tests: run focused suites to ensure no behavior change |
| 143 | + - go test ./src/internal -run '^TestInitialFilePathPositionsCursor|TestInitialFilePathPositionsCursorWindow$' |
| 144 | + - go test ./src/internal -run '^TestReturnDirElement$' |
| 145 | +4. Manual smoke: |
| 146 | + - Launch spf in a directory with many files; verify layout unchanged. |
| 147 | + - Open with a file path; ensure cursor targets file and remains visible. |
| 148 | + |
| 149 | +Commit Strategy |
| 150 | +- Commit 1: Add ui_consts.go and .golangci.yml mnd enablement. |
| 151 | +- Commit 2: Apply constants to src/cmd and internal model/layout. |
| 152 | +- Commit 3: Rendering replacements. |
| 153 | +- Commit 4: Preview and image utils (with //nolint where needed). |
| 154 | +- Commit 5: Kitty utils and optional path-prefix constants. |
| 155 | +- Keep each commit small and scoped; include brief messages referencing mnd. |
0 commit comments