Skip to content

Commit 3094b9f

Browse files
committed
📝 Add docs and scripts to test completion in different shells
1 parent 0974135 commit 3094b9f

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

docs/contributing.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,147 @@ $ bash scripts/test-cov-html.sh
7070

7171
This command generates a directory `./htmlcov/`, if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing.
7272

73+
## Completion
74+
75+
To try and test the completion for different shells and check that they are working you can use a Docker container.
76+
77+
There's a `Dockerfile` and a a Docker Compose file `compose.yaml` at `./scripts/docker/`.
78+
79+
It has installed `bash`, `zsh`, `fish`, and `pwsh` (PowerShell for Linux).
80+
81+
It also has installed `nano` and `vim`, so that you can check the modified configuration files for the shells (for example `.bashrc`, `.zshrc`, etc).
82+
83+
It also has `uv` installed, so you can install the dependencies and the project quickly.
84+
85+
The Docker Compose file mounts the main directory as `/code` inside the container, so you can change things and try them out.
86+
87+
Go to the `./scripts/docker/` directory:
88+
89+
```console
90+
$ cd scripts/docker/
91+
```
92+
93+
Then run an interactive session with `bash` inside the container:
94+
95+
```console
96+
$ docker compose run typer bash
97+
98+
root@79c4b9b70cbe:/code#
99+
```
100+
101+
Then inside the container, you can install `typer` with:
102+
103+
```console
104+
$ uv pip install -r requirements.txt
105+
```
106+
107+
Then, you can start the shell you want to use, the one where you want to try out completion:
108+
109+
* `bash`
110+
* `fish`
111+
* `pwsh`
112+
* `zsh`
113+
114+
For example:
115+
116+
```console
117+
$ zsh
118+
```
119+
120+
Then install `typer` completion:
121+
122+
```console
123+
$ typer --install-completion
124+
```
125+
126+
/// info
127+
128+
In `pwsh` you will probably get a warning of:
129+
130+
```plaintext
131+
Set-ExecutionPolicy: Operation is not supported on this platform.
132+
```
133+
134+
this is because that configuration is only available in Windows (and needed there), not in PowerShell for Linux.
135+
136+
///
137+
138+
For completion to take effect, you need to restart the shell. So, exit the current shell:
139+
140+
```console
141+
$ exit
142+
```
143+
144+
and start a new shell (for the same shell you installed completion in) again. For example:
145+
146+
```console
147+
$ zsh
148+
```
149+
150+
Now you could create a demo file on the same Typer directory in your editor, for example `demo.py`:
151+
152+
```python
153+
import typer
154+
155+
app = typer.Typer()
156+
157+
158+
@app.command()
159+
def hello():
160+
print("Hello")
161+
162+
163+
@app.command()
164+
def goodbye():
165+
print("Goodbye")
166+
167+
168+
if __name__ == "__main__":
169+
app()
170+
```
171+
172+
Because the directory is mounted as a volume, you will be able to access the file from inside the container.
173+
174+
So, you can try running it with the `typer` command, that will use the installed shell completion:
175+
176+
```console
177+
$ typer demo.py <TAB>
178+
```
179+
180+
And you should see the completion working:
181+
182+
```console
183+
run -- Run the provided Typer app.
184+
utils -- Extra utility commands for Typer apps.
185+
```
186+
187+
And the same for the commands in your `demo.py` file:
188+
189+
```console
190+
$ typer demo.py run <TAB>
191+
192+
hello goodbye
193+
```
194+
195+
You can also check the configuration file using `nano` or `vim`, for example:
196+
197+
```bash
198+
nano ~/.zshrc
199+
```
200+
201+
It will show some content like:
202+
203+
```bash
204+
fpath+=~/.zfunc; autoload -Uz compinit; compinit
205+
206+
207+
zstyle ':completion:*' menu select
208+
```
209+
210+
If you exit from the container, you can start a new one, you will probably have to install the packages again and install completion again.
211+
212+
Using this process, you can test all the shells, with their completions, being able to start from scratch quickly in a fresh container, and verifying that everything works as expected.
213+
73214
## Docs
74215

75216
First, make sure you set up your environment as described above, that will install all the requirements.

scripts/docker/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:latest
2+
3+
# Add fish
4+
RUN echo 'deb http://download.opensuse.org/repositories/shells:/fish:/release:/3/Debian_12/ /' | tee /etc/apt/sources.list.d/shells:fish:release:3.list
5+
RUN curl -fsSL https://download.opensuse.org/repositories/shells:fish:release:3/Debian_12/Release.key | gpg --dearmor | tee /etc/apt/trusted.gpg.d/shells_fish_release_3.gpg > /dev/null
6+
7+
# Install packages including Fish, Zsh, PowerShell
8+
RUN apt-get update && apt-get install -y \
9+
wget \
10+
apt-transport-https \
11+
software-properties-common \
12+
nano \
13+
vim \
14+
fish \
15+
zsh \
16+
&& wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.4/powershell_7.4.4-1.deb_amd64.deb \
17+
&& dpkg -i powershell_7.4.4-1.deb_amd64.deb
18+
19+
# Install uv
20+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
21+
22+
ENV UV_SYSTEM_PYTHON=1

scripts/docker/compose.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
typer:
3+
build: .
4+
volumes:
5+
- ../../:/code
6+
working_dir: /code
7+
command: sleep infinity

0 commit comments

Comments
 (0)