Skip to content

Commit 655a0d1

Browse files
authored
Merge pull request #80 from Escyll/master
Add move (note mv) command
2 parents a7bef05 + c1a8d58 commit 655a0d1

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ Opens your notes folder in your default configured file explorer. Shorthand alia
124124

125125
Opens a given note in your `$EDITOR`. Name can be an absolute path, or a relative path in your notes (.md suffix optional). Shorthand alias also available with `notes o`.
126126

127+
### `notes mv <note-name> <destination>|<directory>`
128+
129+
Renames a given note to destination or moves the note to directory. Name can be an absolute path, or a relative path in your notes (.md suffix optional). Destination and directory have to be a relative path in your notes.
130+
127131
### `notes rm [-r | --recursive] <note-name>`
128132

129133
Removes the given note if it exists. If `-r` or `--recursive` is given, deletes the folders/notes recursively.

notes

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ open_note() {
201201
$EDITOR "$note_path" < /dev/tty
202202
}
203203

204+
move_note() {
205+
local source_note_path=$( get_full_note_path "$1" )
206+
local dest_or_dir_path=$2
207+
208+
if [[ ! -e "$source_note_path" ]]; then
209+
printf "mv requires a source note that exists\n"
210+
exit 1
211+
fi
212+
213+
if [[ -z "$dest_or_dir_path" ]]; then
214+
printf "mv requires a destination name or folder\n"
215+
exit 1
216+
fi
217+
218+
dir_path="$notes_dir/$dest_or_dir_path"
219+
if [[ -d "$dir_path" ]]; then
220+
mv $source_note_path $dir_path
221+
return
222+
fi
223+
224+
local dest_path=$( get_full_note_path "$dest_or_dir_path" )
225+
mkdir -p "$( dirname $dest_path)"
226+
mv $source_note_path $dest_path
227+
}
228+
204229
cat_note() {
205230
local note_path=$1
206231

@@ -227,6 +252,7 @@ Usage:
227252
$name search|s [pattern] # Search notes by filename or content
228253
$name open|o # Open your notes directory
229254
$name open|o <name> # Open a note for editing by full name
255+
$name mv <source> <dest>|<directory> # Rename a note, or move a note when a directory is given
230256
$name rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
231257
$name cat <name> # Display note
232258
echo <name> | $name open|o # Open all note filenames piped in
@@ -276,6 +302,9 @@ main() {
276302
"open"|"o" )
277303
cmd="handle_multiple_notes open"
278304
;;
305+
"mv" )
306+
cmd="move_note"
307+
;;
279308
"rm" )
280309
cmd="remove_note"
281310
;;

test/test-mv.bats

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!./test/libs/bats/bin/bats
2+
3+
load 'helpers'
4+
5+
setup() {
6+
setupNotesEnv
7+
}
8+
9+
teardown() {
10+
teardownNotesEnv
11+
}
12+
13+
export EDITOR=touch
14+
notes="./notes"
15+
16+
@test "Should rename a note" {
17+
run $notes new note
18+
run $notes mv note note2
19+
20+
assert_success
21+
assert_exists "$NOTES_DIRECTORY/note2.md"
22+
}
23+
24+
@test "Should rename a note in a subdir" {
25+
run $notes new subdir/note
26+
run $notes mv subdir/note subdir/note2
27+
28+
assert_success
29+
assert_exists "$NOTES_DIRECTORY/subdir/note2.md"
30+
}
31+
32+
@test "Should rename a note with extension" {
33+
run $notes new note
34+
run $notes mv note.md note2.md
35+
36+
assert_success
37+
assert_exists "$NOTES_DIRECTORY/note2.md"
38+
}
39+
40+
@test "Should move a note to an existing subdir" {
41+
run $notes new subdir/note
42+
run $notes new note2
43+
run $notes mv note2 subdir
44+
45+
assert_success
46+
assert_exists "$NOTES_DIRECTORY/subdir/note.md"
47+
assert_exists "$NOTES_DIRECTORY/subdir/note2.md"
48+
}
49+
50+
@test "Should move a note to a new subdir" {
51+
run $notes new note
52+
run $notes mv note.md subdir/note.md
53+
54+
assert_success
55+
assert_exists "$NOTES_DIRECTORY/subdir/note.md"
56+
}
57+
58+
@test "Should move a note to notes_directory" {
59+
run $notes new subdir/note
60+
run $notes mv subdir/note /
61+
62+
assert_success
63+
assert_exists "$NOTES_DIRECTORY/note.md"
64+
}

0 commit comments

Comments
 (0)