From 4dadcd5f9f6e73036c65fadaccb1ba99be194fec Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 12 Dec 2023 12:23:21 +0100 Subject: [PATCH] scripts: Refactor py script Made detection and setup a little more stable, will still have its kinks since it is rather hastily put together but should work for detecting kernels, invoking the right python repl and letting you set it manually with `py -c`. --- scripts/.local/bin/py | 79 ++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/scripts/.local/bin/py b/scripts/.local/bin/py index 5512fdb..2c68f67 100755 --- a/scripts/.local/bin/py +++ b/scripts/.local/bin/py @@ -5,39 +5,64 @@ cur_dir=$(pwd) search_dir="${molten_session_dir}${cur_dir}" if [ "$1" == "-c" ]; then - cmd=$(echo -e "euporie-console\njupyter-console\nptipython\nipython\npython" | fzf) -fi - -# set normal command first -if [ -z "$cmd" ]; then - if exist ptipython; then - cmd=ptipython - elif exist ipython; then - cmd=ipython - elif exist python; then - cmd=python - else - echo "No python found on system, please install." 1>&2 - exit 1 - fi + cmd_explicit=$(echo -e "euporie-console\njupyter-console\nptipython\nipython\npython" | fzf) fi # search for molten session for current dir if [ -n "$(ls -A "${search_dir}" 2>/dev/null)" ]; then - res=$(find "$search_dir" -maxdepth 1 -type f -name "*.json") - if [ "$(echo "$res" | wc -l)" -gt 1 ]; then - session=$(echo "$res" | fzf --prompt "Molten session> ") - fi + session=$(find "$search_dir" -maxdepth 1 -type f -name "*.json") + if [ "$(echo "$session" | wc -l)" -gt 1 ]; then + session=$(echo "$session" | fzf --prompt "Molten session> ") + fi +fi + +if [ -n "$cmd_explicit" ] && not exist "$cmd_explicit"; then + echo "Command $cmd_explicit not found." + exit 1 +elif [ "$cmd_explicit" == "euporie-console" ]; then + if [ -n "$session" ]; then + euporie-console --edit-mode vi --connection-file "$session" + else + echo "Starting euporie without a session to attach to." + euporie-console --edit-mode vi + fi + exit 0 +elif [ "$cmd_explicit" == "jupyter-console" ]; then + if [ -n "$session" ]; then + jupyter-console --existing "$session" + else + echo "Starting jupyter console without a session to attach to." + jupyter-console + fi + exit 0 +elif [ -n "$cmd_explicit" ]; then + "$cmd_explicit" + exit 0 +fi + +# set normal command first +if [ -z "$cmd" ]; then + if exist ptipython; then + cmd=ptipython + elif exist ipython; then + cmd=ipython + elif exist python; then + cmd=python + else + echo "No python found on system, please install." 1>&2 + exit 1 + fi fi # attach to kernel if [ -n "${session}" ]; then - if exist euporie-console || [ "$cmd" == "euporie-console" ]; then - euporie-console --edit-mode vi --connection-file "$session" - elif exist jupyter-console || [ "$cmd" == "jupyter-console" ]; then - jupyter-console --existing "$session" - fi -# or just run normal python -else - $cmd + if exist euporie-console; then + euporie-console --edit-mode vi --connection-file "$session" + exit 0 + elif exist jupyter-console; then + jupyter-console --existing "$session" + exit 0 + fi fi +# or just run normal python +$cmd