dotfiles/scripts/.local/bin/py

69 lines
1.7 KiB
Text
Raw Permalink Normal View History

#!/usr/bin/env bash
molten_session_dir="/run/user/$UID/molten-sessions"
cur_dir=$(pwd)
search_dir="${molten_session_dir}${cur_dir}"
if [ "$1" == "-c" ]; then
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
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; 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