summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/sort.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-13 19:32:59 +0300
committerPaul Buetow <paul@buetow.org>2026-05-13 19:32:59 +0300
commit270c226d8443c779374ee46631c4f385126b9a81 (patch)
tree9a1d613d5ea5d6227103cbf71283e9149420a22d /internal/tui/dashboard/sort.go
parentd585f83e1c3757e1a1edf2802abfa6171b5234f3 (diff)
extract generic sortedWithState helper to deduplicate tab sort logic
Four near-identical sorted-rows functions (sortedFileSnapshots, sortedDirSnapshots, sortedSyscallSnapshots, sortedProcessTableRows) each repeated the same guard-clone-sort-tiebreak-apply pattern. Replace them with a single generic sortedWithState[T,K] in sort.go that accepts per-tab byKey and tiebreak comparators as parameters. Remove now-unused slices imports from syscalls.go and processes.go. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/tui/dashboard/sort.go')
-rw-r--r--internal/tui/dashboard/sort.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/internal/tui/dashboard/sort.go b/internal/tui/dashboard/sort.go
index 399d0c5..718746a 100644
--- a/internal/tui/dashboard/sort.go
+++ b/internal/tui/dashboard/sort.go
@@ -1,5 +1,7 @@
package dashboard
+import "slices"
+
type tableSortState[K comparable] struct {
active bool
key K
@@ -74,3 +76,35 @@ func compareStringAsc(left, right string) int {
return 0
}
}
+
+// sortedWithState is the shared sort helper for all sortable dashboard tables.
+// It avoids four near-identical functions (files, dirs, syscalls, processes)
+// that each guard on empty/inactive, clone the slice, sort with a key
+// comparator, fall back to a tiebreaker, and apply the reverse flag.
+//
+// - rows – the input slice (not modified; nil is returned when empty)
+// - sortState – carries the active flag, selected key, and reverse flag
+// - byKey – per-key comparator for the active sort column
+// - tiebreak – stable fallback comparator used when byKey returns 0
+func sortedWithState[T any, K comparable](
+ rows []T,
+ sortState tableSortState[K],
+ byKey func(left, right T, key K) int,
+ tiebreak func(left, right T) int,
+) []T {
+ if len(rows) == 0 {
+ return nil
+ }
+ if !sortState.active {
+ return rows
+ }
+ sorted := slices.Clone(rows)
+ slices.SortFunc(sorted, func(left, right T) int {
+ cmp := byKey(left, right, sortState.key)
+ if cmp == 0 {
+ cmp = tiebreak(left, right)
+ }
+ return sortState.apply(cmp)
+ })
+ return sorted
+}