Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Width of Static is miscalulated when markup is used #4447

Closed
valentingregoire opened this issue Apr 25, 2024 · 10 comments
Closed

Width of Static is miscalulated when markup is used #4447

valentingregoire opened this issue Apr 25, 2024 · 10 comments

Comments

@valentingregoire
Copy link
Contributor

When adding markup to a text in a Static (markup=True), then the width of the text is based on the content with the style characters.

Example without markup:
image

Example with markup:
image

I think the width is calculated by the amount of characters (columns) before parsing. Perhaps if markup=True, the width can be calculated after parsing?

Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

What version of Textual are you using? I seem to remember this issue (or something like it) was fixed a few months back.

@valentingregoire
Copy link
Contributor Author

Version info:

rich              13.7.1 Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal
textual           0.54.0 Modern Text User Interface framework
textual-dev       1.5.1  Development tools for working with Textual

@TomJGooding
Copy link
Contributor

Thanks, could you possibly provide a minimal reproducible example (MRE): i.e. a short piece of code that can reproduce the issue?

@valentingregoire
Copy link
Contributor Author

Here you go:

from rich.style import Style
from textual.app import App, ComposeResult
from textual.widgets import Static


class BorkedApp(App):
    """The main application class for the Songbook TUI."""

    text = "Hello, world!"
    style = Style(bgcolor="#333333", bold=True)
    text_styled = style.render(text)

    CSS_PATH = "app.tcss"
    DEFAULT_CSS = """
        Static {
            width: auto;
            border: round green;
        }
    """
    BINDINGS = [("q", "exit", "Quit")]

    def compose(self) -> ComposeResult:
        """Compose the main application."""
        yield Static(self.text)
        yield Static(self.text_styled)


if __name__ == "__main__":
    app = BorkedApp()
    app.run()

Result:

image

@valentingregoire
Copy link
Contributor Author

This also causes some weird see-through issues with the screen below btw. I can solve that by adding another entire black screen behind, it's just inconvenient, but I might open up a separate ticket for that.

@TomJGooding
Copy link
Contributor

Style.render from Rich returns the ANSI style codes. I thought you were talking about using console markup? Try instead simply something like the below:

styled_text = "[green bold]Hello World[/]"

@valentingregoire
Copy link
Contributor Author

You are right, this works well. Styling with Style objects is then not what is supposed to be done, thanks for the info!

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@darrenburns
Copy link
Member

@valentingregoire You can style with Style objects e.g. Text("hello", style=Style(...)). Then you can shove that inside a Static as you were doing before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants