dotfiles/scripts/.local/bin/py

44 lines
1.2 KiB
Text
Raw 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=$(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
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
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
fi