1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-10 21:49:16 +00:00

Merge pull request #290 from alexandear-org/refactor/use-slices-clone

refactor: simplify with slices.Clone
This commit is contained in:
Tw93
2026-01-11 08:54:37 +08:00
committed by GitHub
2 changed files with 6 additions and 22 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"sync" "sync"
"time" "time"
@@ -27,8 +28,8 @@ var (
func snapshotFromModel(m model) historyEntry { func snapshotFromModel(m model) historyEntry {
return historyEntry{ return historyEntry{
Path: m.path, Path: m.path,
Entries: cloneDirEntries(m.entries), Entries: slices.Clone(m.entries),
LargeFiles: cloneFileEntries(m.largeFiles), LargeFiles: slices.Clone(m.largeFiles),
TotalSize: m.totalSize, TotalSize: m.totalSize,
TotalFiles: m.totalFiles, TotalFiles: m.totalFiles,
Selected: m.selected, Selected: m.selected,
@@ -45,24 +46,6 @@ func cacheSnapshot(m model) historyEntry {
return entry return entry
} }
func cloneDirEntries(entries []dirEntry) []dirEntry {
if len(entries) == 0 {
return nil
}
copied := make([]dirEntry, len(entries))
copy(copied, entries) //nolint:all
return copied
}
func cloneFileEntries(files []fileEntry) []fileEntry {
if len(files) == 0 {
return nil
}
copied := make([]fileEntry, len(files))
copy(copied, files) //nolint:all
return copied
}
func ensureOverviewSnapshotCacheLocked() error { func ensureOverviewSnapshotCacheLocked() error {
if overviewSnapshotLoaded { if overviewSnapshotLoaded {
return nil return nil

View File

@@ -9,6 +9,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"slices"
"sort" "sort"
"strings" "strings"
"sync/atomic" "sync/atomic"
@@ -982,8 +983,8 @@ func (m model) enterSelectedDir() (tea.Model, tea.Cmd) {
} }
if cached, ok := m.cache[m.path]; ok && !cached.Dirty { if cached, ok := m.cache[m.path]; ok && !cached.Dirty {
m.entries = cloneDirEntries(cached.Entries) m.entries = slices.Clone(cached.Entries)
m.largeFiles = cloneFileEntries(cached.LargeFiles) m.largeFiles = slices.Clone(cached.LargeFiles)
m.totalSize = cached.TotalSize m.totalSize = cached.TotalSize
m.totalFiles = cached.TotalFiles m.totalFiles = cached.TotalFiles
m.selected = cached.Selected m.selected = cached.Selected