Skip to content

Commit 4bf92c5

Browse files
authored
Merge pull request #39 from joereynolds/feature/recent-subcommand
Add in 'recent' subcommand
2 parents 2c15b1f + 496e36c commit 4bf92c5

File tree

13 files changed

+78
-22
lines changed

13 files changed

+78
-22
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ The complete command is:
249249
jn mv --plain
250250
```
251251

252+
### Get recent notes
253+
254+
```
255+
jn recent
256+
```
257+
258+
This will display the 15 most recently edited notes.
259+
Useful for when you forget _that_ important document
260+
261+
Both `jn r` and `jn re` also work.
262+
252263
### Creating a book
253264

254265
A book is optional. Conceptually it's where you store related notes. You might

src/files.nim

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import categories, config, console, templates/templates
33

44
type DirectoryListing = Table[string, int]
55

6+
type
7+
FileRecord* = object
8+
name*: string
9+
modifiedTime*: Time
10+
611
proc getFullNotePath*(note: string, config: Config, book: string = ""): Path =
712
let suffix = getNotesSuffix(config)
813
let dateFormat = getNotesPrefix(config)
@@ -57,15 +62,26 @@ proc createNote*(noteName: string, config: Config, book: string = "") =
5762
let message = "Created " & $name
5863
success(message)
5964

60-
proc getFilesForDir*(dir: string, filter = {pcFile, pcLinkToFile}): seq[string] =
65+
proc getFilesForDir*(dir: string, filter = {pcFile, pcLinkToFile}): seq[FileRecord] =
6166
result = @[]
6267

6368
for kind, path in walkDir(dir):
6469
if path.isHidden(): # TODO - respect --hidden flag
6570
continue
6671

6772
if kind in filter:
68-
result.add(path)
73+
74+
# Technically the file should exist but there can be times where a
75+
# symlink points to a now-removed file.
76+
# This means we still need to check if the file is legit...
77+
if not fileExists(path):
78+
continue
79+
80+
var record = FileRecord(
81+
name: path,
82+
modifiedTime: getLastModificationTime(path)
83+
)
84+
result.add(record)
6985

7086
if kind == pcDir:
7187
# Recursively add paths from subdirectories

src/fuzzy.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import config
22
import console
33
import files
44

5-
import std/[os, osproc, parsecfg, streams, strutils]
5+
import std/[os, osproc, parsecfg, sequtils, streams, strutils]
66

77
proc formatForFuzzy(matches: seq[string]): string =
88
matches.join("\n")
@@ -36,4 +36,4 @@ proc selectFromChoice*(choices: seq[string], config: Config): string =
3636

3737
proc selectFromDir*(fromDir: string, config: Config): string =
3838
let choices = getFilesForDir(fromDir)
39-
return selectFromChoice(choices, config)
39+
return selectFromChoice(choices.mapIt(it.name), config)

src/grep.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ proc getMatches*(term: string, config: Config): seq[string] =
2121

2222
for file in getFilesForDir(getNotesPath(config)):
2323

24-
if isSkippable(file):
24+
if isSkippable(file.name):
2525
continue
2626

2727
try:
28-
if readFile(file).contains(pattern):
29-
matches.add(file)
28+
if readFile(file.name).contains(pattern):
29+
matches.add(file.name)
3030
except IOError:
3131
discard
3232

src/jn.nim

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import std/parseopt
44
import std/strutils
55

66
import config
7-
import subcommands/[book, cat, config as sconfig, edit, grep, help, mv, rm, star, tags, tmpl]
7+
import subcommands/[
8+
book, cat, config as sconfig, edit,
9+
grep, help, mv, recent, rm,
10+
star, tags, tmpl
11+
]
812
import files
913
import console
1014

@@ -48,20 +52,24 @@ for kind, key, val in getopt():
4852
help.process(configuration)
4953
quit()
5054

51-
if key in tmpl.aliases:
52-
tmpl.process(configuration)
55+
if key in mv.aliases:
56+
mv.process(configuration, params)
5357
quit()
5458

55-
if key in star.aliases:
56-
star.process(configuration)
59+
if key in recent.aliases:
60+
recent.process(configuration, params)
5761
quit()
5862

5963
if key in rm.aliases:
6064
rm.process(configuration)
6165
quit()
6266

63-
if key in mv.aliases:
64-
mv.process(configuration, params)
67+
if key in star.aliases:
68+
star.process(configuration)
69+
quit()
70+
71+
if key in tmpl.aliases:
72+
tmpl.process(configuration)
6573
quit()
6674

6775
if key in grep.aliases:

src/subcommands/book.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ proc printNotesForBook(book: string, config: Config) =
1313
var lastPaths = @[""]
1414

1515
for note in notes:
16-
lastPaths.add(lastPathPart(note))
16+
lastPaths.add(lastPathPart(note.name))
1717

1818
lastPaths.sort()
1919

src/subcommands/mv.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ proc process*(config: Config, flags: seq[string]) =
3737

3838
if "--plain" in flags:
3939
fileName = newName
40-
40+
4141
let oldPath = choice
4242
let dirPath = parentDir(oldPath)
4343
let newPath = dirPath / fileName
44-
44+
4545
if fileExists(newPath):
4646
warn("File already exists at: " & newPath)
4747
quit()
48-
48+
4949
try:
5050
moveFile(oldPath, newPath)
5151
success("Renamed: " & lastPathPart(oldPath) & " -> " & fileName)

src/subcommands/recent.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import std/[algorithm, parsecfg, times]
2+
import ../[console, config, files]
3+
4+
const aliases* = @["r", "re", "recent"]
5+
6+
proc process*(config: Config, flags: seq[string]) =
7+
8+
let notes = getFilesforDir(getNotesPath(config))
9+
let byTime = notes.sortedByIt(it.modifiedTime).reversed()
10+
11+
# TODO - use --limit flag here
12+
for note in byTime[0 .. 15]:
13+
stdout.write(note.name)
14+
info("(" & $note.modifiedTime & ")")
15+
16+

src/subcommands/rm.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ proc process*(config: Config) =
2424
content = readFile(choice)
2525
except IOError:
2626
discard
27-
27+
2828
removeFile(choice)
2929

3030
let (tempFile, path) = createTempFile("jn-", "")

src/subcommands/tags.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ const aliases* = @["tag", "tags"]
1212

1313
proc process*(searchTerm: string, config: Config) =
1414
var tagTerm = searchTerm.strip()
15-
15+
1616
# Strip '#' if user provided it
1717
if tagTerm.startsWith("#"):
1818
tagTerm = tagTerm[1..^1]
19-
19+
2020
if tagTerm == "":
2121
echo "Tags command is missing the tag name"
2222
quit()
2323

2424
# Add '#' prefix to search for tags
2525
let tagSearch = "#" & tagTerm
26-
26+
2727
let notes = getFilesForDir(getNotesPath(config))
2828
let fuzzy = getFuzzyProvider(config)
2929
let matches = search(tagSearch, config)

0 commit comments

Comments
 (0)