Skip to content

Commit

Permalink
Fix #7, return most accessed dir if no args is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku committed Oct 30, 2022
1 parent a45d2d8 commit 9e4a0f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
33 changes: 33 additions & 0 deletions jump/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ func (s Store) ReadEntries() (EntryList, error) {
return entries, nil
}

func (s Store) topEntry() (entry, error) {
var ent entry

file, err := os.Open(s.path)
if err != nil {
if os.IsNotExist(err) {
return ent, nil
}
return ent, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
ent, err := parseEntry(line)
if err != nil {
log.Printf("Failed to parse score from line: %v", line)
continue
}
return ent, nil
}
return ent, nil
}

func (s Store) Cleanup() error {
entries, err := s.ReadEntries()
if err != nil {
Expand All @@ -86,6 +111,14 @@ func (s Store) GetNthCandidate(args []string, index int, defaultPath string) (st
return defaultPath, nil
}

func (s Store) GetTopPath(defaultPath string) (string, error) {
ent, err := s.topEntry()
if err != nil {
return "", err
}
return ent.val, nil
}

func (s Store) saveEntries(entries EntryList) error {
folder := filepath.Dir(s.path)
if err := os.MkdirAll(folder, 0740); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ func main() {
}
fmt.Println(jump.BestGuess(entries, args))
} else {
flag.Usage()
path, err := store.GetTopPath(".")
if err != nil {
log.Fatal(err)
}
fmt.Println(path)
}
}

Expand Down

0 comments on commit 9e4a0f4

Please sign in to comment.