Compare commits

..

No commits in common. "adea4b4c97de3adf5778cf958e7e5f837a0d3aae" and "983ecd1ca89a4d686275247b8fb57aed12942735" have entirely different histories.

9 changed files with 5 additions and 351 deletions

View File

@ -1,61 +0,0 @@
#!/bin/bash
setup() {
source ../zk
}
test__zk_edit_uses_filename_when_passed_as_parameter() {
# shellcheck disable=SC2016
fake vi 'echo vi "${FAKE_PARAMS[@]}"'
export EDITOR=""
zettel_name="11111111111111"
expected="vi $ZKDIR/$zettel_name"
result="$(_zk_edit $zettel_name)"
assert_equals "$expected" "$result"
}
test__zk_edit_uses__zk_search_when_filename_not_passed_as_parameter() {
export EDITOR=""
zettel_name="11111111111111"
expected="vi $ZKDIR/$zettel_name"
# shellcheck disable=SC2016
fake vi 'echo vi "${FAKE_PARAMS[@]}"'
fake _zk_search echo "$zettel_name"
result="$(_zk_edit)"
assert_equals "$expected" "$result"
}
test__zk_edit_runs_EDITOR_if_EDITOR_is_set() {
export EDITOR="nano"
# shellcheck disable=SC2016
fake "$EDITOR" echo '$EDITOR ${FAKE_PARAMS[@]}'
fake vi cat
filename="11111111111111"
expected="$EDITOR $ZKDIR/$filename"
result="$(_zk_edit $filename)"
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="11111111111111"
fake date "echo $filename"
expected="vi $TMP_ZETTELKASTEN/$filename"
result="$(_zk_edit $filename)"
assert_equals "$expected" "$result"
}

View File

@ -1,37 +0,0 @@
#!/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"
}

View File

@ -1,36 +0,0 @@
#!/bin/bash
TMP_ZETTELKASTEN=""
setup() {
source ../zk
TMP_ZETTELKASTEN="$(mktemp -d)"
}
teardown() {
rm -rf "$TMP_ZETTELKASTEN"
}
test__zk_new_creates_file_in_correct_directory() {
export ZKDIR="$TMP_ZETTELKASTEN"
filename="11111111111111"
fake date "echo $filename"
fake _zk_edit echo ""
_zk_new
assert "test -e $TMP_ZETTELKASTEN/$filename"
}
test__zk_new_runs_zk_edit_on_new_file() {
export ZKDIR="$TMP_ZETTELKASTEN"
filename="11111111111111"
# shellcheck disable=SC2016
fake _zk_edit echo '_zk_edit ${FAKE_PARAMS[@]}'
fake _zk_isosec echo "$filename"
expected="_zk_edit $filename"
result="$(_zk_new)"
assert_equals "$expected" "$result"
}

View File

@ -1,19 +0,0 @@
#!/bin/bash
setup() {
source ../zk
}
test_check_zkdir_does_not_error_if_ZKDIR_exists() {
export ZKDIR="/"
assert_status_code 0 check_zkdir
}
test_check_zkdir_errors_if_ZKDIR_does_not_exist() {
export ZKDIR="/this/is/a/fake/directory/"
assert_status_code 1 check_zkdir
}

View File

@ -4,50 +4,12 @@ setup() {
source ../zk source ../zk
} }
test_get_command_returns_empty_if_no_input_given() {
result="$(get_command "$ZKCOMMANDS" "")"
assert_equals "" "$result"
}
test_get_command_returns_empty_if_unrecognized_input_given() {
result="$(get_command "$ZKCOMMANDS" "lkjdsaas")"
assert_equals "" "$result"
}
test_get_command_returns_list_for_substring() { test_get_command_returns_list_for_substring() {
commands="list new"
expected="list" expected="list"
for arg in l li lis list; do for arg in l li lis list; do
result="$(get_command "$ZKCOMMANDS" "$arg")" result="$(get_command "$commands" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_returns_search_for_substring() {
expected="search"
for arg in s se sea sear searc search; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_returns_new_for_substring() {
expected="new"
for arg in n ne new; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_returns_edit_for_substring() {
expected="edit"
for arg in e ed edi edit; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result" assert_equals "$expected" "$result"
done done
} }

View File

@ -1,15 +0,0 @@
#!/bin/bash
setup() {
source ../zk
}
test_isosec_calls_date_with_correct_parameters() {
expected="date -u +%Y%m%d%H%M%S"
# shellcheck disable=SC2016
fake date echo 'date ${FAKE_PARAMS[@]}'
result="$(_zk_isosec)"
assert_equals "$expected" "$result"
}

View File

@ -1,56 +0,0 @@
#!/bin/bash
setup() {
source ../zk
}
test_main_calls_list_when_list_command_passed() {
fake check_zkdir test
expected="_zk_list"
fake get_command 'echo list'
fake _zk_list "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_calls_search_when_search_command_passed() {
fake check_zkdir test
expected="_zk_search"
fake get_command 'echo search'
fake _zk_search "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_calls_new_when_new_command_passed() {
fake check_zkdir test
expected="_zk_new"
fake get_command 'echo new'
fake _zk_new "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_calls_edit_when_edit_command_passed() {
fake check_zkdir test
expected="_zk_edit"
fake get_command 'echo edit'
fake _zk_edit "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_errors_if_unrecognized_command_given() {
fake check_zkdir test
fake get_command echo ""
assert_status_code 2 main
}

View File

@ -1,26 +0,0 @@
#!/bin/bash
setup() {
source ../zk
}
test__zk_search_calls__zk_list() {
expected="_zk_list"
fake _zk_list echo "$expected"
fake fzf cat
result="$(_zk_search)"
assert_equals "$expected" "$result"
}
test__zk_search_extracts_id_from_chosen_zettel() {
expected="1111111111111"
# shellcheck disable=SC2016
fake _zk_list 'echo "$expected | Zettel Title"'
fake fzf cat
result="$(_zk_search)"
assert_equals "$expected" "$result"
}

64
zk
View File

@ -1,79 +1,21 @@
#!/bin/sh #!/bin/sh
set -e COMMANDS='list new delete'
ZKDIR="$HOME/.local/share/zk/"
ZKCOMMANDS='list search new edit'
check_zkdir() {
if ! test -d "$ZKDIR"; then
echo "zk: $ZKDIR directory does not exist" 1>&2
exit 1
fi
}
get_command() { get_command() {
commands="$1" commands="$1"
input="$2" input="$2"
if test -z "$input"; then
echo ""
return 0
fi
for c in $commands; do for c in $commands; do
if echo "$c" | grep "^$input" >/dev/null; then if echo "$c" | grep "^$input" >/dev/null; then
echo "$c" echo "$c"
return 0
fi fi
done done
} }
_zk_isosec() {
date -u +%Y%m%d%H%M%S
}
_zk_list() {
for zettel in "$ZKDIR"/*; do
echo "$(basename "$zettel") | $(head -1 "$zettel")"
done
}
_zk_search() {
_zk_list | fzf --prompt "ZK: " | cut -d ' ' -f 1
}
_zk_new() {
filename="$(_zk_isosec)"
touch "$ZKDIR/$filename"
_zk_edit "$filename"
}
_zk_edit() {
filename="$1"
if test -z "$filename"; then
filename="$(_zk_search)"
fi
if test -n "$EDITOR"; then
$EDITOR "$ZKDIR/$filename"
else
vi "$ZKDIR/$filename"
fi
}
main() { main() {
check_zkdir subcommand="$(get_commands "$COMMANDS" "$1")"
echo "$subcommand"
subcommand="$(get_command "$ZKCOMMANDS" "$1")"
if test -z "$subcommand"; then
echo "zk: $1 not recognized" 1>&2
exit 2
fi
_zk_"$subcommand"
} }
name="$(basename "$0")" name="$(basename "$0")"