Skip to content

Commit

Permalink
Merge pull request #41 from rikuson/29-change-quiz-file-format-to-json
Browse files Browse the repository at this point in the history
Change quizfile format into yaml
  • Loading branch information
rikuson authored Oct 15, 2022
2 parents 533e488 + 65b0d5f commit b3f0e0f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 51 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ fn main() {
```bash
$ quiz rm rust/001-macro-count-statements
rm: remove regular file ‘/home/rikuson/.quiz-
store/rust/001-macro-count-statements.txt’? y
store/rust/001-macro-count-statements.yml’? y
removed ‘/home/rikuson/.quiz-store/rust/001-macro-
count-statements.txt
count-statements.yml
```
## Extended Git Example
Expand All @@ -121,7 +121,7 @@ $ quiz git remote add origin git@github.com:rikuson/quiz-store.git
$ quiz add whoami
Enter answer for whoami:
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 whoami.txt
create mode 100644 whoami.yml
$ quiz git push -u --all
Counting objects: 4, done.
Expand All @@ -138,15 +138,15 @@ Enter answer for whoareyou:
anonymous
[master b9b6746] Added given quiz for whoareyou to store.
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 whoareyou.txt
create mode 100644 whoareyou.yml
$ quiz rm whoami
rm: remove regular file ‘/home/rikuson/.quiz-store/whoami.txt? y
removed ‘/home/zx2c4/.quiz-store/whoami.txt
rm 'whoami.txt'
rm: remove regular file ‘/home/rikuson/.quiz-store/whoami.yml? y
removed ‘/home/zx2c4/.quiz-store/whoami.yml
rm 'whoami.yml'
[master 288b379] Removed whoami from store.
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 whoami.txt
delete mode 100644 whoami.yml
$ quiz git push
Counting objects: 9, done.
Expand Down
16 changes: 8 additions & 8 deletions man/quiz.1
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ fn main() {
Remove quiz from store
.B $ quiz rm rust/001-macro-count-statements
.br
rm: remove regular file \[u2018]/home/rikuson/.quiz-store/rust/001-macro-count-statements.txt\[u2019]? y
rm: remove regular file \[u2018]/home/rikuson/.quiz-store/rust/001-macro-count-statements.yml\[u2019]? y
.br
removed \[u2018]/home/rikuson/.quiz-store/rust/001-macro-count-statements.txt\[u2019]
removed \[u2018]/home/rikuson/.quiz-store/rust/001-macro-count-statements.yml\[u2019]

.SH EXTENDED GIT EXAMPLE
Here, we initialize new quiz store, create a git repository, and then manipulate and sync quizzes. Make note of the arguments to the first call of \fBquiz git push\fP; consult
Expand All @@ -279,7 +279,7 @@ Enter answer for whoami:
.br
1 file changed, 0 insertions(+), 0 deletions(-)
.br
create mode 100644 whoami.txt
create mode 100644 whoami.yml

.B $ quiz git push -u --all
.br
Expand Down Expand Up @@ -307,21 +307,21 @@ Enter answer for whoareyou: anonymous
.br
1 file changed, 0 insertions(+), 0 deletions(-)
.br
create mode 100644 whoareyou.txt
create mode 100644 whoareyou.yml

.B $ quiz rm whoami
.br
rm: remove regular file \[u2018]/home/rikuson/.quiz-store/whoami.txt\[u2019]? y
rm: remove regular file \[u2018]/home/rikuson/.quiz-store/whoami.yml\[u2019]? y
.br
removed \[u2018]/home/rikuson/.quiz-store/whoami.txt\[u2019]
removed \[u2018]/home/rikuson/.quiz-store/whoami.yml\[u2019]
.br
rm 'whoami.txt'
rm 'whoami.yml'
.br
[master 288b379] Removed whoami from store.
.br
1 file changed, 0 insertions(+), 0 deletions(-)
.br
delete mode 100644 whoami.txt
delete mode 100644 whoami.yml

.B $ quiz git push
.br
Expand Down
2 changes: 1 addition & 1 deletion src/completion/quiz.bash-completion
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
_quiz_complete_entries () {
local prefix="${QUIZ_STORE_DIR:-$HOME/.quiz-store/}"
prefix="${prefix%/}/"
local suffix=".txt"
local suffix=".yml"
local autoexpand=${1:-0}

local IFS=$'\n'
Expand Down
2 changes: 1 addition & 1 deletion src/completion/quiz.fish-completion
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function __fish_quiz_print_entry_dirs
end

function __fish_quiz_print_entries
__fish_quiz_print ".txt" ".txt"
__fish_quiz_print ".yml" ".yml"
end

function __fish_quiz_print_entries_and_dirs
Expand Down
2 changes: 1 addition & 1 deletion src/completion/quiz.zsh-completion
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ _quiz_complete_entries_helper () {
local IFS=$'\n'
local prefix
zstyle -s ":completion:${curcontext}:" prefix prefix || prefix="${QUIZ_STORE_DIR:-$HOME/.quiz-store}"
_values -C 'quizzes' ${$(find -L "$prefix" -prune -o $@ -print 2>/dev/null | sed -e "s#${prefix}/\{0,1\}##" -e 's#\.txt##' -e 's#\\#\\\\#g' -e 's#:#\\:#g' | sort):-""}
_values -C 'quizzes' ${$(find -L "$prefix" -prune -o $@ -print 2>/dev/null | sed -e "s#${prefix}/\{0,1\}##" -e 's#\.yml##' -e 's#\\#\\\\#g' -e 's#:#\\:#g' | sort):-""}
}

_quiz_complete_entries_with_subdirs () {
Expand Down
40 changes: 21 additions & 19 deletions src/quiz-store.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ cmd_show() {

local quiz
local path="$1"
local quizfile="$PREFIX/$path.txt"
local quizfile="$PREFIX/$path.yml"
check_sneaky_paths "$path"
if [[ -f $quizfile ]]; then
quiz="$(cat "$quizfile" | $BASE64)" || exit $?
echo "$quiz" | $BASE64 -d
quiz="$(cat "$quizfile")" || exit $?
echo "$quiz"
elif [[ -d $PREFIX/$path ]]; then
if [[ -z $path ]]; then
echo "Quiz Store"
else
echo "${path%\/}"
fi
tree -N -C -l --noreport "$PREFIX/$path" 3>&- | tail -n +2 | "$SED" -E 's/\.txt(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .txt at end of line, but keep colors
tree -N -C -l --noreport "$PREFIX/$path" 3>&- | tail -n +2 | "$SED" -E 's/\.yml(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .yml at end of line, but keep colors
elif [[ -z $path ]]; then
die "Error: quiz store is empty. Try \"quiz init\"."
else
Expand All @@ -191,7 +191,7 @@ cmd_find() {
[[ $# -eq 0 ]] && die "Usage: $PROGRAM $COMMAND quiz-names..."
IFS="," eval 'echo "Search Terms: $*"'
local terms="*$(printf '%s*|*' "$@")"
tree -N -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$PREFIX" 3>&- | tail -n +2 | "$SED" -E 's/\.txt(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
tree -N -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$PREFIX" 3>&- | tail -n +2 | "$SED" -E 's/\.yml(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
}

cmd_grep() {
Expand All @@ -200,19 +200,19 @@ cmd_grep() {
while read -r -d "" quizfile; do
grepresults="$(cat "$quizfile" | grep --color=always "$@")"
[[ $? -ne 0 ]] && continue
quizfile="${quizfile%.txt}"
quizfile="${quizfile%.yml}"
quizfile="${quizfile#$PREFIX/}"
local quizfile_dir="${quizfile%/*}/"
[[ $quizfile_dir == "${quizfile}/" ]] && quizfile_dir=""
quizfile="${quizfile##*/}"
printf "\e[94m%s\e[1m%s\e[0m:\n" "$quizfile_dir" "$quizfile"
echo "$grepresults"
done < <(find -L "$PREFIX" -path '*/.git' -prune -o -path '*/.extensions' -prune -o -iname '*.txt' -print0)
done < <(find -L "$PREFIX" -path '*/.git' -prune -o -path '*/.extensions' -prune -o -iname '*.yml' -print0)
}

cmd_insert() {
local opts multiline=0 force=0
opts="$($GETOPT -o mef -l multiline,echo,force -n "$PROGRAM" -- "$@")"
opts="$($GETOPT -o mf -l multiline,force -n "$PROGRAM" -- "$@")"
local err=$?
eval set -- "$opts"
while true; do case $1 in
Expand All @@ -221,9 +221,9 @@ cmd_insert() {
--) shift; break ;;
esac done

[[ $err -ne 0 || $multiline -eq 1 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--multiline,-m] [--force,-f] quiz-name"
[[ $err -ne 0 || $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--multiline,-m] [--force,-f] quiz-name"
local path="${1%/}"
local quizfile="$PREFIX/$path.txt"
local quizfile="$PREFIX/$path.yml"
check_sneaky_paths "$path"
set_git "$quizfile"

Expand All @@ -237,9 +237,11 @@ cmd_insert() {
local quiz=$(cat)
echo "$quiz" > "$quizfile" || exit 1
else
local quiz
read -r -p "Enter answer for $path: " -e quiz
echo "$quiz" > "$quizfile" || exit 1
local quiz question answer
read -r -p "Enter question for $path: " -e question
read -r -p "Enter answer for $path: " -e answer
quiz="question: $question\nanswer: $answer"
echo -e "$quiz" > "$quizfile" || exit 1
fi
git_add_file "$quizfile" "Add given answer for $path to store."
}
Expand All @@ -250,11 +252,11 @@ cmd_edit() {
local path="${1%/}"
check_sneaky_paths "$path"
mkdir -p -v "$PREFIX/$(dirname -- "$path")"
local quizfile="$PREFIX/$path.txt"
local quizfile="$PREFIX/$path.yml"
set_git "$quizfile"

tmpdir #Defines $SECURE_TMPDIR
local tmp_file="$(mktemp -u "$SECURE_TMPDIR/XXXXXX")-${path//\//-}.txt"
local tmp_file="$(mktemp -u "$SECURE_TMPDIR/XXXXXX")-${path//\//-}.yml"

local action="Add"
if [[ -f $quizfile ]]; then
Expand Down Expand Up @@ -283,7 +285,7 @@ cmd_delete() {
check_sneaky_paths "$path"

local quizdir="$PREFIX/${path%/}"
local quizfile="$PREFIX/$path.txt"
local quizfile="$PREFIX/$path.yml"
[[ -f $quizfile && -d $quizdir && $path == */ || ! -f $quizfile ]] && quizfile="${quizdir%/}/"
[[ -e $quizfile ]] || die "Error: $path is not in the quiz store."
set_git "$quizfile"
Expand Down Expand Up @@ -317,15 +319,15 @@ cmd_copy_move() {
local old_dir="$old_path"
local new_path="$PREFIX/$2"

if ! [[ -f $old_path.txt && -d $old_path && $1 == */ || ! -f $old_path.txt ]]; then
if ! [[ -f $old_path.yml && -d $old_path && $1 == */ || ! -f $old_path.yml ]]; then
old_dir="${old_path%/*}"
old_path="${old_path}.txt"
old_path="${old_path}.yml"
fi
echo "$old_path"
[[ -e $old_path ]] || die "Error: $1 is not in the quiz store."

mkdir -p -v "${new_path%/*}"
[[ -d $old_path || -d $new_path || $new_path == */ ]] || new_path="${new_path}.txt"
[[ -d $old_path || -d $new_path || $new_path == */ ]] || new_path="${new_path}.yml"

local interactive="-i"
[[ ! -t 0 || $force -eq 1 ]] && interactive="-f"
Expand Down
2 changes: 1 addition & 1 deletion tests/t0020-show-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_expect_success 'Test "show" command' '
'

test_expect_success 'Test "show" command with spaces' '
"$QUIZ" add "I am a cred with lots of spaces"<<<"BLAH!!" &&
"$QUIZ" add -m "I am a cred with lots of spaces"<<<"BLAH!!" &&
[[ $("$QUIZ" show "I am a cred with lots of spaces") == "BLAH!!" ]]
'

Expand Down
14 changes: 7 additions & 7 deletions tests/t0050-mv-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ INITIAL_QUIZ="bla bla bla will we make it!!"
test_expect_success 'Basic move command' '
"$QUIZ" init &&
"$QUIZ" git init &&
"$QUIZ" add cred1 <<<"$INITIAL_QUIZ" &&
"$QUIZ" add -m cred1 <<<"$INITIAL_QUIZ" &&
"$QUIZ" mv cred1 cred2 &&
[[ -e $QUIZ_STORE_DIR/cred2.txt && ! -e $QUIZ_STORE_DIR/cred1.txt ]]
[[ -e $QUIZ_STORE_DIR/cred2.yml && ! -e $QUIZ_STORE_DIR/cred1.yml ]]
'

test_expect_success 'Directory creation' '
"$QUIZ" mv cred2 directory/ &&
[[ -d $QUIZ_STORE_DIR/directory && -e $QUIZ_STORE_DIR/directory/cred2.txt ]]
[[ -d $QUIZ_STORE_DIR/directory && -e $QUIZ_STORE_DIR/directory/cred2.yml ]]
'

test_expect_success 'Directory creation with file rename and empty directory removal' '
"$QUIZ" mv directory/cred2 "new directory with spaces"/cred &&
[[ -d $QUIZ_STORE_DIR/"new directory with spaces" && -e $QUIZ_STORE_DIR/"new directory with spaces"/cred.txt && ! -e $QUIZ_STORE_DIR/directory ]]
[[ -d $QUIZ_STORE_DIR/"new directory with spaces" && -e $QUIZ_STORE_DIR/"new directory with spaces"/cred.yml && ! -e $QUIZ_STORE_DIR/directory ]]
'

test_expect_success 'Directory rename' '
"$QUIZ" mv "new directory with spaces" anotherdirectory &&
[[ -d $QUIZ_STORE_DIR/anotherdirectory && -e $QUIZ_STORE_DIR/anotherdirectory/cred.txt && ! -e $QUIZ_STORE_DIR/"new directory with spaces" ]]
[[ -d $QUIZ_STORE_DIR/anotherdirectory && -e $QUIZ_STORE_DIR/anotherdirectory/cred.yml && ! -e $QUIZ_STORE_DIR/"new directory with spaces" ]]
'

test_expect_success 'Directory move into new directory' '
"$QUIZ" mv anotherdirectory "new directory with spaces"/ &&
[[ -d $QUIZ_STORE_DIR/"new directory with spaces"/anotherdirectory && -e $QUIZ_STORE_DIR/"new directory with spaces"/anotherdirectory/cred.txt && ! -e $QUIZ_STORE_DIR/anotherdirectory ]]
[[ -d $QUIZ_STORE_DIR/"new directory with spaces"/anotherdirectory && -e $QUIZ_STORE_DIR/"new directory with spaces"/anotherdirectory/cred.yml && ! -e $QUIZ_STORE_DIR/anotherdirectory ]]
'

test_expect_success 'Multi-directory creation and multi-directory empty removal' '
"$QUIZ" mv "new directory with spaces"/anotherdirectory/cred new1/new2/new3/new4/thecred &&
"$QUIZ" mv new1/new2/new3/new4/thecred cred &&
[[ ! -d $QUIZ_STORE_DIR/"new directory with spaces"/anotherdirectory && ! -d $QUIZ_STORE_DIR/new1/new2/new3/new4 && -e $QUIZ_STORE_DIR/cred.txt ]]
[[ ! -d $QUIZ_STORE_DIR/"new directory with spaces"/anotherdirectory && ! -d $QUIZ_STORE_DIR/new1/new2/new3/new4 && -e $QUIZ_STORE_DIR/cred.yml ]]
'

test_expect_success 'Password made it until the end' '
Expand Down
6 changes: 3 additions & 3 deletions tests/t0060-rm-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ test_expect_success 'Test "rm" command' '
"$QUIZ" init &&
"$QUIZ" add cred1 <<<"$($TEST_HOME/fake-answer.sh 43)" &&
"$QUIZ" rm cred1 &&
[[ ! -e $QUIZ_STORE_DIR/cred1.txt ]]
[[ ! -e $QUIZ_STORE_DIR/cred1.yml ]]
'

test_expect_success 'Test "rm" command with spaces' '
"$QUIZ" add "hello i have spaces" <<<"$($TEST_HOME/fake-answer.sh 43)" &&
[[ -e $QUIZ_STORE_DIR/"hello i have spaces".txt ]] &&
[[ -e $QUIZ_STORE_DIR/"hello i have spaces".yml ]] &&
"$QUIZ" rm "hello i have spaces" &&
[[ ! -e $QUIZ_STORE_DIR/"hello i have spaces".txt ]]
[[ ! -e $QUIZ_STORE_DIR/"hello i have spaces".yml ]]
'

test_expect_success 'Test "rm" of non-existent quiz' '
Expand Down
2 changes: 1 addition & 1 deletion tests/t0100-add-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cd "$(dirname "$0")"

test_expect_success 'Test "add" command' '
"$QUIZ" init &&
echo "Hello world" | "$QUIZ" add cred1 &&
echo "Hello world" | "$QUIZ" add -m cred1 &&
[[ $("$QUIZ" show cred1) == "Hello world" ]]
'

Expand Down
2 changes: 1 addition & 1 deletion tests/t0200-edit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cd "$(dirname "$0")"

test_expect_success 'Test "edit" command' '
"$QUIZ" init &&
"$QUIZ" add cred1 <<<"$($TEST_HOME/fake-answer.sh 90)" &&
"$QUIZ" add -m cred1 <<<"$($TEST_HOME/fake-answer.sh 90)" &&
export FAKE_EDITOR_ANSWER="big fat fake quiz" &&
export PATH="$TEST_HOME:$PATH"
export EDITOR="fake-editor-change-answer.sh" &&
Expand Down

0 comments on commit b3f0e0f

Please sign in to comment.