Skip to content

Commit

Permalink
feat: Stylise completion msgs using rich.
Browse files Browse the repository at this point in the history
  • Loading branch information
badshah400 committed Mar 2, 2024
1 parent 97c3a94 commit ef17404
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 95 deletions.
8 changes: 5 additions & 3 deletions src/tartex/_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pathlib import Path
from shutil import copy2

from rich import print as richprint

from tartex.__about__ import __appname__ as APPNAME # noqa

COMPFILE = {
Expand Down Expand Up @@ -42,9 +44,9 @@ def install(self, install_dir=None):
inst_path = Path(
copy2(self.completion_file, path.joinpath(self.install_filename))
)
print(
f"Completion file for {self.shell} shell installed to"
f" {inst_path.parent}"
richprint(
f"Completion file for [bold]{self.shell}[/] shell installed to"
f" [link={inst_path.parent.as_uri()}]{inst_path.parent}[/]"
)


Expand Down
147 changes: 55 additions & 92 deletions src/tartex/_parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

import argparse
from pathlib import Path
from textwrap import fill, wrap
from textwrap import wrap

from rich import print as richprint
from rich.markdown import Markdown
from rich.syntax import Syntax

from tartex.__about__ import __appname__ as APPNAME # noqa
from tartex.__about__ import __version__
Expand All @@ -34,6 +38,51 @@
]


BASH_COMP_PATH = BashCompletion().install_dir.joinpath(f"{APPNAME}")
COMPLETIONS_GUIDE = f"""
Completions are currently supported for bash, fish, and zsh shells.
Please consider [contributing](https://github.com/badshah400/tartex) if you
would like completion for any other shell.
__Note__: XDG_DATA_HOME defaults to `~/.local/share`.
## Bash ##
The option `--bash-completion` will install bash completions for {APPNAME} to
the directory: $XDG_DATA_HOME/{COMPFILE["bash"]}.
Bash automatically searches this dir for completions, so completion for
tartex should work immediately after starting a new terminal session. If it
does not, you may have to add the following lines to your
'~/.bashrc' file:
```bash
# Source {APPNAME} completion
source ~/{BASH_COMP_PATH.relative_to(Path.home())}
```
## Zsh ##
The option `--zsh-completion` will install a zsh completions file for {APPNAME}
to the directory: $XDG_DATA_HOME/{COMPFILE['zsh'].parent!s}. It will also
print what to add to your .zshrc file to enable these completions.
## Fish ##
The option `--fish-completion` will install a fish completions file for
{APPNAME} to the directory: $XDG_DATA_HOME/{COMPFILE['fish'].parent!s}.
No further configuration should be needed. Simply start a new fish terminal et
voila!
"""

ZSH_GUIDE = f"""# Update fpath to include completions dir
# Note: Must be done before initialising compinit
fpath=(~/{ZshCompletion().install_dir.relative_to(Path.home())} $fpath)
# If the following two lines already appear in your .zshrc do not add them
# again, but move the fpath line above the 'autoload compinit' line
autoload -U compinit
compinit"""


class CompletionPrintAction(argparse.Action):

"""
Expand All @@ -57,83 +106,10 @@ def __init__(
help=help,
)

# TODO: This is a mess of print calls; see if it can be simplified
# Note that correct __call__ signature requires all positional args even if
# they are not used in this method itself
def __call__(self, parser, namespace, values, option_string=None): #noqa
fill_width = 80
print(
"Completion is currently supported for bash, fish, and zsh shells."
)
print(
"Please consider contributing if you would like completion for"
" any other shell.\n"
)

print(
"----\n" # do not join
"Bash\n" # do not join
"----\n" # do not join
+ fill(
"The option `--bash-completion` will install bash completions"
f" for {APPNAME} to the directory:",
width=fill_width,
)
+ f"\n${{XDG_DATA_HOME}}/{COMPFILE['bash'].parent!s}\n"
)
print(
fill(
"Bash automatically searches this dir for completions, so"
f" completion for {APPNAME} should work immediately after"
" starting a new terminal session."
" If it does not, you may have to add the following lines"
" to your .bashrc:",
replace_whitespace=False,
width=fill_width,
)
)
bash_comp_path = BashCompletion().install_dir.joinpath(f"{APPNAME}")
print(
f"\n# Source {APPNAME} completion\n"
f"source ~/{bash_comp_path.relative_to(Path.home())}",
)
print(
"\n" # do not join
"---\n" # do not join
"Zsh\n" # do not join
"---\n" # do not join
+ fill(
"The option `--zsh-completion` will install a zsh completion"
f" file for {APPNAME} to the directory:",
width=fill_width,
)
+ "\n"
f"${{XDG_DATA_HOME}}/{COMPFILE['zsh'].parent!s}/\n"
"\n"
+ fill(
"It will also print what to add to your .zshrc file to enable"
" these completions",
width=fill_width,
)
)
print(
"\n" # do not join
"----\n" # do not join
"Fish\n" # do not join
"----\n" # do not join
+ fill(
"The option `--fish-completion` will install a fish completion"
f" file for {APPNAME} to the directory:",
width=fill_width,
)
+ f"\n${{XDG_DATA_HOME}}/{COMPFILE['fish'].parent!s}/\n"
)
print(
fill(
"No further configuration should be needed. Simply start a"
" new fish terminal et voila!"
)
)
def __call__(self, parser, namespace, values, option_string=None): # noqa
richprint(Markdown(COMPLETIONS_GUIDE))
parser.exit()


Expand Down Expand Up @@ -192,24 +168,11 @@ class ZshCompletionInstall(CompletionInstall):

def __call__(self, parser, namespace, values, option_strings=None):
ZshCompletion().install()
print(
"\nAdd the following to your .zshrc if not already present:"
"\n-----------------------------------------------------------"
)
print(
"# Update fpath to include completions dir\n"
"# Note: Must be done before initialising compinit\n"
"fpath"
f"=(~/{ZshCompletion().install_dir.relative_to(Path.home())}"
" $fpath)\n"
richprint(
"\n"
"# If the following two lines already appear in your .zshrc\n"
"# do not add them again, but move the fpath line above the\n"
"# 'autoload compinit' line\n"
"autoload -U compinit\n"
"compinit"
"\n-----------------------------------------------------------\n"
"Add the following to your [bold].zshrc[/] if not already present:"
)
richprint(Syntax(ZSH_GUIDE, "zsh"))
super().__call__(parser, namespace, values, option_strings)
parser.exit()

Expand Down

0 comments on commit ef17404

Please sign in to comment.