Exercism Cli Shortcut
A Exercism CLI shortcut
Exercism is a good place to learn and practice coding. It features a variety of programming languages (tracks) ranging from popular ones like python and C++, newer ones like Rust, Go and Clojure, to exquisite ones like vimscript. Each track consists of a series exercises that one need to challenge them in sequence. Some tracks feature real person mentors, which provides feedback and (dis)approves your solution! Yep, the next exercise would be unlocked only if your solution had been approved by the mentor, which may take some iterations and discussions. Most importantly, Exercism comes with a command line interface (CLI) that is very convenient to use.
For example, to download an exercise, one could enter, or more convenient, copy-and-paste from the website directly.
$ exercism download --exercise=resistor-color-duo --track=bash
Where the “resistor-color-duo
” is the exercise name and “bash
” is the
programming language (or track) name in this example.
The command will download the full package of the exercise, including a readme file, an exercise file and a unit test file. One need to edit the exercise file and pass all the unit tests locally. Then, one can submit the answer using the CLI:
$ exercism submit <exercise_file>
Because the path name and exercise name is so well organized, I created a bash function (I’m learning bash right now :) to shorten the submit command.
es () {
# shorthand to exercism submit
lang_name=$(basename $(dirname $(pwd)))
file_name=$(basename $(pwd))
case "${lang_name}" in
bash)
file_name="$file_name".sh
;;
python)
file_name="$file_name".py
;;
vimscript)
file_name="$file_name".vim
;;
*)
echo "${lang_name}" unsupported yet 1>&2
file_name=""
;;
esac
# replace hyphen with underscore
file_name=${file_name//-/_}
exercism submit $file_name
}
I put this function in the .zshrc
(or .bashrc
). Once I’m happy with the solution, I can simply:
$ es
and the es
function automatically figure out which file to submit.
UPDATE 2020-07-27
With a better knowledge of bash
and more languages to learn, I feel a
re-write of es
is needed. Now, the mapping from the language name to
suffix is managed by an associative array (aka dictionary). It also promotes a
confirmation. The function now is a standalone bash script, so it can be called
in other shells as well.
#!/usr/bin/env bash
# shorthand for submiting an exercise using exercism
# learn more about exercism: exercism.io/
es () {
# shorthand to exercism submit
lang_name=$(basename "$(dirname "$(pwd)")")
file_name=$(basename "$(pwd)")
# map language name to suffix
local -A name_map=( [bash]=sh [python]=py [vimscript]=vim [go]=go )
if [[ -v "name_map[$lang_name]" ]]; then
file_name="${file_name}"."${name_map[$lang_name]}"
else
echo "${lang_name}" unsupported yet 1>&2
exit 1
fi
# replace hyphen with underscore
file_name=${file_name//-/_}
echo "submiting $file_name ..."
read -p ">Are you sure?[Y/N]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
exercism submit "$file_name"
else
echo "submission aborted."
fi
}
es "$@"