Skip to content

Commit 9261331

Browse files
committed
cli: Add [cloe-shell] prefix to prompt
1 parent 5fdff7a commit 9261331

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

cli/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ help:
2525
.PHONY: install editable conan-profile
2626
install: conan-profile
2727
command -v ${PIP} >/dev/null 2>&1
28-
mv pyproject.toml pyproject.toml.bak
28+
-mv pyproject.toml pyproject.toml.bak
2929
${PIP} install ${PIP_INSTALL_ARGS} . || ( \
3030
mv pyproject.toml.bak pyproject.toml; \
3131
exit 1; \
@@ -34,7 +34,7 @@ install: conan-profile
3434

3535
editable: conan-profile
3636
command -v ${PIP} >/dev/null 2>&1
37-
mv pyproject.toml pyproject.toml.bak
37+
-mv pyproject.toml pyproject.toml.bak
3838
${PIP} install -e ${PIP_INSTALL_ARGS} . || ( \
3939
mv pyproject.toml.bak pyproject.toml; \
4040
exit 1; \

cli/cloe_launch/exec.py

+81-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class Environment:
7171
"LANGUAGE",
7272
"LANG",
7373
"LC_.*",
74+
# Required for Zsh
75+
"ZDOTDIR",
7476
# Required for resolving relative paths:
7577
"PWD",
7678
# Required for graphical output:
@@ -383,10 +385,76 @@ def runtime_env_path(self) -> Path:
383385
return self.runtime_dir / "launcher_env.sh"
384386

385387
def _prepare_runtime_dir(self) -> None:
386-
# Clean and create runtime directory
388+
"""Clean and create runtime directory."""
387389
self.clean()
388390
logging.debug(f"Create: {self.runtime_dir}")
389391
self.runtime_dir.mkdir(parents = True)
392+
self._write_prompt_sh()
393+
self._write_bashrc()
394+
self._write_zshrc()
395+
396+
def _write_prompt_sh(self) -> None:
397+
"""Write prompt.sh file."""
398+
prompt_sh_file = self.runtime_dir / "prompt.sh"
399+
prompt_sh_data = """\
400+
CLOE_PROMPT="\u001b[2m[cloe-shell]\u001b[0m"
401+
402+
prompt_cloe() {
403+
if [[ -n $CLOE_SHELL ]]; then
404+
PROMPT="%{%F{242}%}[cloe-shell]%f ${PROMPT}"
405+
fi
406+
}
407+
408+
CURRENT_SHELL=$(basename $0)
409+
if [[ $CURRENT_SHELL = "zsh" ]]; then
410+
autoload -Uz add-zsh-hook
411+
add-zsh-hook precmd prompt_cloe
412+
else
413+
export PS1="$CLOE_PROMPT $PS1"
414+
fi
415+
"""
416+
logging.debug(f"Write: {prompt_sh_file}")
417+
with prompt_sh_file.open("w") as file:
418+
file.write(prompt_sh_data)
419+
420+
def _write_bashrc(self) -> None:
421+
"""Write .bashrc file.
422+
423+
When creating a new shell, it's not possible to just "source" a file
424+
and continue with the normal interactive session. So we need to create
425+
a bashrc file that will be used instead, which loads the normal
426+
configuration, and then augments it with the extra variables we need.
427+
"""
428+
bashrc_file = self.runtime_dir / ".bashrc"
429+
bashrc_data = """\
430+
source /etc/bash.bashrc
431+
if [ -f ~/.bashrc ]; then
432+
source ~/.bashrc
433+
fi
434+
OLD_PS1="$PS1"
435+
source "$(dirname "$BASH_SOURCE[0]")/launcher_env.sh"
436+
PS1="$OLD_PS1"
437+
source "$(dirname "$BASH_SOURCE[0]")/prompt.sh"
438+
"""
439+
logging.debug(f"Write: {bashrc_file}")
440+
with bashrc_file.open("w") as file:
441+
file.write(bashrc_data)
442+
443+
def _write_zshrc(self) -> None:
444+
"""Write .zshrc file."""
445+
zshrc_file = self.runtime_dir / ".zshrc"
446+
zshrc_data = f"""\
447+
ZDOTDIR="${{OLD_ZDOTDIR:-$HOME}}"
448+
source "${{ZDOTDIR}}/.zshenv"
449+
source "${{ZDOTDIR}}/.zshrc"
450+
OLD_PS1="$PS1"
451+
source "{self.runtime_dir}/launcher_env.sh"
452+
PS1="$OLD_PS1"
453+
source "{self.runtime_dir}/prompt.sh"
454+
"""
455+
logging.debug(f"Write: {zshrc_file}")
456+
with zshrc_file.open("w") as file:
457+
file.write(zshrc_data)
390458

391459
def _prepare_virtualenv(self) -> None:
392460
# Get conan to create a virtualenv AND virtualrunenv for us:
@@ -567,8 +635,15 @@ def shell(
567635
# Replace this process with the SHELL now.
568636
sys.stdout.flush()
569637
cmd = [shell]
570-
if arguments is not None:
638+
if arguments is not None and len(arguments) != 0:
571639
cmd.extend(arguments)
640+
else:
641+
if shell == "/bin/bash":
642+
cmd.extend(["--init-file", str(self.runtime_dir/".bashrc")])
643+
elif shell == "/bin/zsh":
644+
env.set("OLD_ZDOTDIR", str(env.get("ZDOTDIR", "")))
645+
env.set("ZDOTDIR", self.runtime_dir)
646+
logging.debug(f"Exec: {shell} {' '.join(cmd)}")
572647
os.execvpe(shell, cmd, env.as_dict())
573648

574649
def activate(
@@ -588,10 +663,14 @@ def activate(
588663
)
589664
sys.exit(2)
590665

666+
env.set("CLOE_SHELL", self.runtime_env_path())
667+
self._write_runtime_env(env)
668+
591669
print("# Please see `cloe-launch activate --help` before activating this.")
592670
print()
593671
print(f"source {self.runtime_dir / 'activate.sh'}")
594672
print(f"source {self.runtime_dir / 'activate_run.sh'}")
673+
print(f"source {self.runtime_dir / 'prompt.sh'}")
595674
for var in ["CLOE_PROFILE_HASH", "CLOE_ENGINE", "CLOE_PLUGIN_PATH"]:
596675
print(f'export {var}="{env[var]}"')
597676
print(f'export CLOE_SHELL="{self.runtime_env_path()}"')

0 commit comments

Comments
 (0)