Skip to content

Commit f1ec87a

Browse files
committed
Support ZDOTDIR when installing zsh completion
This both supports the correct location of .zshrc, as well as putting .zfunc as a subdirectory of ZDOTDIR. Tested with ZDOTDIR set to ~/.config/zsh and everything worked as expected. Fixes #171
1 parent af8fc27 commit f1ec87a

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

typer/_completion_shared.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,22 @@ def install_bash(*, prog_name: str, complete_var: str, shell: str) -> Path:
123123

124124
def install_zsh(*, prog_name: str, complete_var: str, shell: str) -> Path:
125125
# Setup Zsh and load ~/.zfunc
126-
zshrc_path = Path.home() / ".zshrc"
127-
zshrc_path.parent.mkdir(parents=True, exist_ok=True)
126+
zdotdir = os.getenv("ZDOTDIR")
127+
zdotdir_path = Path(zdotdir).expanduser() if zdotdir else Path.home()
128+
zshrc_path = zdotdir_path / ".zshrc"
129+
zfunc_path = zdotdir_path / ".zfunc"
130+
zfunc_path.mkdir(parents=True, exist_ok=True)
131+
132+
# If zfunc_path is in home, replace it with ~ for the fpath line
133+
if zfunc_path.is_relative_to(Path.home()):
134+
fpath = zfunc_path.as_posix().replace(Path.home().as_posix(), "~")
135+
else:
136+
fpath = zfunc_path.as_posix()
137+
128138
zshrc_content = ""
129139
if zshrc_path.is_file():
130140
zshrc_content = zshrc_path.read_text()
131-
completion_line = "fpath+=~/.zfunc; autoload -Uz compinit; compinit"
141+
completion_line = f"fpath+={fpath}; autoload -Uz compinit; compinit"
132142
if completion_line not in zshrc_content:
133143
zshrc_content += f"\n{completion_line}\n"
134144
style_line = "zstyle ':completion:*' menu select"
@@ -140,8 +150,7 @@ def install_zsh(*, prog_name: str, complete_var: str, shell: str) -> Path:
140150
zshrc_content = f"{zshrc_content.strip()}\n"
141151
zshrc_path.write_text(zshrc_content)
142152
# Install completion under ~/.zfunc/
143-
path_obj = Path.home() / f".zfunc/_{prog_name}"
144-
path_obj.parent.mkdir(parents=True, exist_ok=True)
153+
path_obj = zfunc_path / f"_{prog_name}"
145154
script_content = get_completion_script(
146155
prog_name=prog_name, complete_var=complete_var, shell=shell
147156
)

0 commit comments

Comments
 (0)