From cdeff3519111f923d68cf0b501d80e00782eaa7c Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 15 Oct 2024 10:10:36 +0200 Subject: refactor(kdev): Simplify main --- cmd/kdev/main.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'cmd/kdev') diff --git a/cmd/kdev/main.go b/cmd/kdev/main.go index 94d4d72..f639716 100644 --- a/cmd/kdev/main.go +++ b/cmd/kdev/main.go @@ -72,8 +72,23 @@ func main() { kcore.Expect(err, "Error getting HEAD") head, err := repo.CommitObject(ref.Hash()) kcore.Expect(err, "Error getting commit object") + kcore.Expect(walkRepo(keywords, head, records), "Error walking directory") + + records = lo.Filter(records, func(record Record, index int) bool { return record.date.After(after) }) + if *sortBy == "random" { + records = lo.Shuffle(records) + } else { + slices.SortFunc(records, func(a, b Record) int { return -int(a.date.Sub(b.date).Nanoseconds()) }) + } + fmt.Println("") + for _, record := range records[:min(*maxRecords, len(records))] { + fmt.Printf("%s\t %s:%d\n", record.date.Format("2006-01-02"), record.path, record.line) + } +} + +func walkRepo(keywords []string, head *object.Commit, records []Record) error { progress := progressbar.Default(-1, "Scanning") - err = filepath.WalkDir(*repoPath, func(path string, d fs.DirEntry, err error) error { + return filepath.WalkDir(*repoPath, func(path string, d fs.DirEntry, err error) error { relativePath := strings.TrimPrefix(path, *repoPath) progress.Describe(relativePath) if err != nil { @@ -95,18 +110,6 @@ func main() { } return nil }) - kcore.Expect(err, "Error walking directory") - - records = lo.Filter(records, func(record Record, index int) bool { return record.date.After(after) }) - if *sortBy == "random" { - records = lo.Shuffle(records) - } else { - slices.SortFunc(records, func(a, b Record) int { return -int(a.date.Sub(b.date).Nanoseconds()) }) - } - fmt.Println("") - for _, record := range records[:min(*maxRecords, len(records))] { - fmt.Printf("%s\t %s:%d\n", record.date.Format("2006-01-02"), record.path, record.line) - } } type MatchingLine struct { -- cgit v1.3