|
| 1 | +# Accessibility |
| 2 | + |
| 3 | +This page goes over information relevant to both developers and users relating to accessibility considerations. |
| 4 | + |
| 5 | +Accessibility is very important to consider when developing applications. |
| 6 | +Colorblindness impacts roughly 4% of the general population, meaning that if your application gets even a small user base, there is a high probability that it is being used by someone with colorblindness. |
| 7 | + |
| 8 | +Fortunately, there are ways as both developers and users to address these accessibility concerns, as detailed on this page. |
| 9 | + |
| 10 | +## For users |
| 11 | + |
| 12 | +If you are a user of a **rich-click** CLI, there are a few options you have to improve accessibility for yourself. |
| 13 | + |
| 14 | +### 1. Use the `NO_COLOR` environment variable |
| 15 | + |
| 16 | +Rich uses the `NO_COLOR` standard ([more information here](https://no-color.org/)), giving rich-click built-in capability to allow the user to suppress color. |
| 17 | + |
| 18 | +So, to run any rich-click CLI program without colour, you can do: |
| 19 | + |
| 20 | +```shell |
| 21 | +export NO_COLOR=1 # Set environment variable in shell |
| 22 | +python cli.py # Run CLI tool |
| 23 | + |
| 24 | +# ... Or run as a single line: |
| 25 | +NO_COLOR=1 python cli.py |
| 26 | +``` |
| 27 | + |
| 28 | +In order to set this environment variable automatically every time you use the terminal, you can add it to your `~/.bashrc` (if using bash) or `~/.zshrc` (if using zsh): |
| 29 | + |
| 30 | +=== "bash" |
| 31 | + ```shell |
| 32 | + echo "export NO_COLOR=1" >> ~/.bashrc |
| 33 | + ``` |
| 34 | + |
| 35 | +=== "zsh" |
| 36 | + ```shell |
| 37 | + echo "export NO_COLOR=1" >> ~/.zshrc |
| 38 | + ``` |
| 39 | + |
| 40 | +!!!tip |
| 41 | + Note that other programs may also respect `NO_COLOR`, so it could have other effects! |
| 42 | + |
| 43 | +### 2. Configure your terminal's 4-bit ANSI colors |
| 44 | + |
| 45 | +The 4-bit ANSI color system is a set of 16 different colors implemented in every terminal. This is the most common way to set colors. |
| 46 | +These colors are **not** deterministic; different terminals use slightly different hex values for the ANSI colors. [Wikipedia has a full breakdown of all the variations in these colors](https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit) |
| 47 | + |
| 48 | +Most modern terminals have the ability to customize all of these colors in the terminals' settings. |
| 49 | +If you are having difficulty distinguishing colors, it is recommended that you adjust these settings. |
| 50 | + |
| 51 | +!!! note |
| 52 | + This will only work for CLIs that utilize the 4-bit ANSI color system. |
| 53 | + CLIs that utilize hex values or other color systems will not be impacted by your terminal's ANSI color settings. |
| 54 | + |
| 55 | +## For developers |
| 56 | + |
| 57 | +If you would like to make your CLI more accessible for others, there are a few rules of thumb you can follow: |
| 58 | + |
| 59 | +### 1. Use Rich features over Click features |
| 60 | + |
| 61 | +There are some Click features that rich-click doesn't override such as print statements and interactive prompts (see [Comparison of Click and rich-click](comparison_of_click_and_rich_click.md#click-features-that-rich-click-does-not-override)). |
| 62 | + |
| 63 | +In these cases, we recommend using native Rich functionality so that your end users can benefit from `NO_COLOR`, which Click does not support. |
| 64 | + |
| 65 | +So, for example: |
| 66 | + |
| 67 | +- `#!python Confirm.ask("[red]Are you sure?[/]")` is more accessible because it works with `NO_COLOR`. |
| 68 | +- `#!python click.confirm(click.echo("Are you sure?", fg="red"))` is less accessible because it cannot be overridden by `NO_COLOR`. |
| 69 | + |
| 70 | +### 2. Use 4-bit ANSI colors |
| 71 | + |
| 72 | +The 4-bit ANSI color system is a set of 16 different colors implemented in effectively every terminal, and they are the most common way to set colors. |
| 73 | +These colors are **not** deterministic; different terminals use slightly different hex values for the ANSI colors. [Wikipedia has a full breakdown of all the variations in these colors](https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit) |
| 74 | + |
| 75 | +!!! note |
| 76 | + **rich-click**'s logo references the ANSI colors! 😁 |
| 77 | + |
| 78 | +There are 16 total ANSI colors: 8 base ANSI colors, with each one having a "bright" variant: |
| 79 | + |
| 80 | +- `black`, `bright_black` |
| 81 | +- `red`, `bright_red` |
| 82 | +- `green`, `bright_green` |
| 83 | +- `yellow`, `bright_yellow` |
| 84 | +- `blue`, `bright_blue` |
| 85 | +- `magenta`, `bright_magenta` |
| 86 | +- `cyan`, `bright_cyan` |
| 87 | +- `white`, `bright_white` |
| 88 | + |
| 89 | +Additionally, each one of these can be modified with `dim`, which in modern terminals just applies a change to the opacity of the color, giving developers a total of 32 different colors that can be shown. |
| 90 | + |
| 91 | +Below is a quick script that renders all of these colors: |
| 92 | + |
| 93 | +```python |
| 94 | +{!code_snippets/accessibility/colors.py!} |
| 95 | +``` |
| 96 | + |
| 97 | +<!-- RICH-CODEX |
| 98 | +working_dir: docs/code_snippets/accessibility |
| 99 | +hide_command: true |
| 100 | +terminal_width: 48 |
| 101 | +--> |
| 102 | +{.screenshot} |
| 103 | + |
| 104 | +(The colors you see when running this locally will differ from the colors in the image.) |
| 105 | + |
| 106 | +The fact that the colors are not deterministic is a _benefit_ for accessibility; it means, for example, a user can customize their terminal so that the ANSI "red" is more suitable for them. |
| 107 | +Nearly every modern terminal allows for this sort of customization. |
| 108 | + |
| 109 | +This means that developers looking to create a more accessible experience should prefer ANSI colors. |
| 110 | + |
| 111 | +So, for example: |
| 112 | + |
| 113 | +- `#!python RichHelpConfiguration(style_option="red")` is more accessible because users can configure the hex value of this red. |
| 114 | +- `#!python RichHelpConfiguration(style_option="#FF0000")` is less accessible because it is not configurable by the end user. |
0 commit comments