Format prompt toolkit key with modifier into an RST representation.
(keys: str, add_alternatives=True)
| 142 | |
| 143 | |
| 144 | def format_prompt_keys(keys: str, add_alternatives=True) -> str: |
| 145 | """Format prompt toolkit key with modifier into an RST representation.""" |
| 146 | |
| 147 | def to_rst(key): |
| 148 | escaped = key.replace("\\", "\\\\") |
| 149 | return f":kbd:`{escaped}`" |
| 150 | |
| 151 | keys_to_press: List[str] |
| 152 | |
| 153 | prefixes = { |
| 154 | "c-s-": [to_rst("ctrl"), to_rst("shift")], |
| 155 | "s-c-": [to_rst("ctrl"), to_rst("shift")], |
| 156 | "c-": [to_rst("ctrl")], |
| 157 | "s-": [to_rst("shift")], |
| 158 | } |
| 159 | |
| 160 | for prefix, modifiers in prefixes.items(): |
| 161 | if keys.startswith(prefix): |
| 162 | remainder = keys[len(prefix) :] |
| 163 | keys_to_press = [*modifiers, to_rst(remainder)] |
| 164 | break |
| 165 | else: |
| 166 | keys_to_press = [to_rst(keys)] |
| 167 | |
| 168 | result = " + ".join(keys_to_press) |
| 169 | |
| 170 | if keys in INDISTINGUISHABLE_KEYS and add_alternatives: |
| 171 | alternative = INDISTINGUISHABLE_KEYS[keys] |
| 172 | |
| 173 | result = ( |
| 174 | result |
| 175 | + " (or " |
| 176 | + format_prompt_keys(alternative, add_alternatives=False) |
| 177 | + ")" |
| 178 | ) |
| 179 | |
| 180 | return result |
| 181 | |
| 182 | |
| 183 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…