Skip to content

Instantly share code, notes, and snippets.

@spencerpogo
Last active June 30, 2021 22:31
Show Gist options
  • Save spencerpogo/0c961c2d22535fc5e35bd7a40aca3ec0 to your computer and use it in GitHub Desktop.
Save spencerpogo/0c961c2d22535fc5e35bd7a40aca3ec0 to your computer and use it in GitHub Desktop.
newterm.sh - easily spawn new gnome-terminals
#!/bin/bash
set -euo pipefail
shopt -s inherit_errexit
detectshell() {
SCRIPT=$(cat <<'EOF'
import os
from shellingham import detect_shell, ShellDetectionFailure
def get_shell():
try:
name, path = detect_shell(os.getpid())
except (RuntimeError, ShellDetectionFailure):
shell = None
if os.name == "posix":
shell = os.environ.get("SHELL")
elif os.name == "nt":
shell = os.environ.get("COMSPEC")
if not shell:
raise RuntimeError("Unable to detect the current shell.")
name, path = Path(shell).stem, shell
return name, path
# name and path are usually the same
name, path = get_shell()
print(path)
EOF
)
python -c "$SCRIPT"
return "$?"
}
spawnshell () {
SCRIPT=$(cat <<'EOF'
# Based off https://github.com/python-poetry/poetry/blob/d2485b8f849f0bbfd8f6933b625ea07f11f80615/poetry/utils/shell.py#L37
import os
import sys
from clikit.utils.terminal import Terminal
import pexpect
cmd = sys.argv[1]
shell = sys.argv[2]
no_pyenv = bool(int(sys.argv[3]))
if no_pyenv:
for v in os.environ.keys():
if v.startswith("PYENV_"):
del os.environ[v]
terminal = Terminal()
c = pexpect.spawn(
shell, ["-i"], dimensions=(terminal.height, terminal.width)
)
if shell == "zsh":
c.setecho(False)
c.sendline(cmd)
c.interact(escape_character=None)
c.close()
sys.exit(c.exitstatus)
EOF
)
python -c "$SCRIPT" "$1" "$SHELL" "$DELETE_PYENV_VARIABLES"
return "$?"
}
export -f spawnshell
tab () {
gnome-terminal --tab "--working-directory=$1" -- /bin/bash -c 'spawnshell "$1"' -- "$2" #/bin/bash -c 'spawnshellee "$1"' -- "$1"
}
# The preferred shell. Replace 'SHELL=shell' with 'SHELL=$(detectshell)' to detect the
# running shell instead. Must support "-i" argument.
SHELL=$(detectshell)
# Should all PYENV_* variables be deleted by python before spawning the shell.
# The PYENV_* environment variables are set by pyenv when a pyenv python executable
# is invoked. These variables are then propagated down to the spawned shell.
# I use this because my prompt changes if the pyenv version is being overridden by the
# PYENV_VERSION environment variable.
# No reason to disable if you don't use pyenv, it will simply be a no-op if there are
# no matching environment variables.
# 0 = disable, 1 = enable
DELETE_PYENV_VARIABLES=1
export SHELL
export DELETE_PYENV_VARIABLES
if [ $# -le 1 ]; then
echo newterm.sh - Easily spawn new \`gnome-terminal\`s >&2
echo "Usage: $0 <working dir> <cmd>" >&2
echo "with arguments repeated as many times as necessary" >&2
echo 'Example: newterm.sh . "echo hi" /some/path "echo foo"' >&2
echo 'Shell is configurable by editing the script.'
exit 1
fi
while [[ $# -ge 2 ]]; do
tab "$1" "$2"
shift
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment