Skip to content

[BUG] help line length miscalculated when using stylized text #1159

@zmeir

Description

@zmeir

Discussed in #1101

Originally posted by zmeir December 23, 2024

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorials in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Annotated

import typer


app = typer.Typer()


@app.command()
def example(
    a: Annotated[str, typer.Option(help="This is A")],
    b: Annotated[str, typer.Option(help=f"This is {typer.style('B', underline=True)}")],
):
    pass


if __name__ == "__main__":
    app()

Description

When using typer.style in the help text of a command option, the rightmost | in the "frame" of the Options output is misaligned. This seems to be due to a miscalculation of the help line length, caused by the invisible style characters.

Using the example script - run python example.py --help and you should get this output:
image
(highlighted the misaligned part)

Operating System

macOS

Operating System Details

macOS version: 13.7.1 (22H221)

Typer Version

0.15.1

Python Version

3.10.13

Additional Context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions