Add "new" command implementation

This commit is contained in:
The Magician 2023-12-08 17:18:24 +00:00
parent 621340140e
commit ab5425d59d
2 changed files with 41 additions and 1 deletions

View File

@ -12,6 +12,9 @@ teardown() {
} }
test__zk_new_creates_file_in_correct_directory() { test__zk_new_creates_file_in_correct_directory() {
fake "$EDITOR" echo ""
fake vi echo ""
export ZKDIR="$TMP_ZETTELKASTEN" export ZKDIR="$TMP_ZETTELKASTEN"
filename="aaaaaaaaaaaaaa" filename="aaaaaaaaaaaaaa"
fake date "echo $filename" fake date "echo $filename"
@ -20,3 +23,34 @@ test__zk_new_creates_file_in_correct_directory() {
assert "test -e $TMP_ZETTELKASTEN/$filename" assert "test -e $TMP_ZETTELKASTEN/$filename"
} }
test__zk_new_runs_EDITOR_if_EDITOR_is_set() {
export EDITOR="nano"
# shellcheck disable=SC2016
fake "$EDITOR" echo '$EDITOR ${FAKE_PARAMS[@]}'
export ZKDIR="$TMP_ZETTELKASTEN"
filename="aaaaaaaaaaaaaa"
fake date "echo $filename"
expected="$EDITOR $TMP_ZETTELKASTEN/$filename"
result="$(_zk_new)"
assert_equals "$expected" "$result"
}
test__zk_new_runs_vi_if_EDITOR_is_not_set() {
export EDITOR=""
# shellcheck disable=SC2016
fake vi echo 'vi ${FAKE_PARAMS[@]}'
export ZKDIR="$TMP_ZETTELKASTEN"
filename="aaaaaaaaaaaaaa"
fake date "echo $filename"
expected="vi $TMP_ZETTELKASTEN/$filename"
result="$(_zk_new)"
assert_equals "$expected" "$result"
}

8
zk
View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
ZKDIR="$HOME/.local/share/zk/" ZKDIR="$HOME/.local/share/zk/"
COMMANDS='list new delete' COMMANDS='list new'
get_command() { get_command() {
commands="$1" commands="$1"
@ -27,6 +27,12 @@ _zk_list() {
_zk_new() { _zk_new() {
filename="$(_zk_isosec)" filename="$(_zk_isosec)"
touch "$ZKDIR/$filename" touch "$ZKDIR/$filename"
if test -n "$EDITOR"; then
$EDITOR "$ZKDIR/$filename"
else
vi "$ZKDIR/$filename"
fi
} }
main() { main() {