Skip to content

Instantly share code, notes, and snippets.

@rgrannell1
Created September 3, 2021 19:46
Show Gist options
  • Save rgrannell1/17fbc8c4c522a17c24ade31961ce7398 to your computer and use it in GitHub Desktop.
Save rgrannell1/17fbc8c4c522a17c24ade31961ce7398 to your computer and use it in GitHub Desktop.
Live programming repl!
function replit () {
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "
Usage:
replit <lang>
replit <...>
replit (-h|--help)
Description:
Watch for changes in a file, and run a language against the file.
If a file is not provided, a temporary file is created.
" | awk 'sub(/^.{4}/,"")'
return 0
fi
lang="$1"
file="$2"
if [ -z "$lang" ]; then
echo "replit: please specify a language executable to use as a first parameter"
return 1
fi
if ! command -v entr &> /dev/null; then
echo "the command 'entr' must be installed for this to work! See https://eradman.com/entrproject/"
return 1
fi
if ! command -v code &> /dev/null; then
echo "the command 'code' must be installed for this to work! See https://code.visualstudio.com/"
return 1
fi
if [ -z "$file" ]; then
# -- launching a shell repl against a temp file
input=$(mktemp)
trap 'echo "💻 closing repl..." && rm -f "$input"' EXIT
echo "$input"
(code "$input" &)
echo "💻 launching live-$lang. Press q to quit, <space> to force-rerun. Save code in your file to run it!"
echo "$input" | entr -r "$lang" $input
else
# -- let people provide whataver launch options they want to to their language
trap 'echo "💻 closing repl..."' EXIT
(code "$input" &)
echo "💻 launching live-$lang. Press q to quit, <space> to force-rerun. Save code in your file to run it!"
echo "$input" | entr -r "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment