Add "list" command implementation

This commit is contained in:
The Magician 2023-12-08 16:08:34 +00:00
parent 983ecd1ca8
commit eeb7f983ae
3 changed files with 70 additions and 1 deletions

37
tests/test__zk_list.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
TMP_ZETTELKASTEN=""
setup() {
source ../zk
TMP_ZETTELKASTEN="$(mktemp -d)"
}
teardown() {
rm -rf "$TMP_ZETTELKASTEN"
}
test__zk_list_outputs_correct_format_with_one_zettel() {
export ZKDIR="$TMP_ZETTELKASTEN"
test_zettel="aaaaaaaaaaaaaa"
echo "# Test Zettel" > "$TMP_ZETTELKASTEN/$test_zettel"
result="$(_zk_list)"
expected="$test_zettel | # Test Zettel"
assert_equals "$expected" "$result"
}
test__zk_list_outputs_correct_format_with_multiple_zettels() {
export ZKDIR="$TMP_ZETTELKASTEN"
test_zettel1="aaaaaaaaaaaaaa"
test_zettel2="bbbbbbbbbbbbbb"
echo "# Test Zettel 1" > "$TMP_ZETTELKASTEN/$test_zettel1"
echo "# Test Zettel 2" > "$TMP_ZETTELKASTEN/$test_zettel2"
result="$(_zk_list)"
expected="$test_zettel1 | # Test Zettel 1
$test_zettel2 | # Test Zettel 2"
assert_equals "$expected" "$result"
}

25
tests/test_main.sh Normal file
View File

@ -0,0 +1,25 @@
#!/bin/bash
setup() {
source ../zk
}
test_main_calls_new_when_new_command_passed() {
expected="_zk_new"
fake get_commands 'echo new'
fake _zk_new "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_calls_list_when_list_command_passed() {
expected="_zk_list"
fake get_commands 'echo list'
fake _zk_list "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}

9
zk
View File

@ -1,5 +1,6 @@
#!/bin/sh #!/bin/sh
ZKDIR="$HOME/.local/share/zk/"
COMMANDS='list new delete' COMMANDS='list new delete'
get_command() { get_command() {
@ -13,9 +14,15 @@ get_command() {
done done
} }
_zk_list() {
for zettel in "$ZKDIR"/*; do
echo "$(basename "$zettel") | $(head -1 "$zettel")"
done
}
main() { main() {
subcommand="$(get_commands "$COMMANDS" "$1")" subcommand="$(get_commands "$COMMANDS" "$1")"
echo "$subcommand" _zk_"$subcommand"
} }
name="$(basename "$0")" name="$(basename "$0")"