8000 Debug formatting for Style, Span, Line, Text · Issue #1382 · ratatui/ratatui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Debug formatting for Style, Span, Line, Text #1382

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

Closed
joshka opened this issue Sep 20, 2024 · 4 comments · Fixed by #1383
Closed

Debug formatting for Style, Span, Line, Text #1382

joshka opened this issue Sep 20, 2024 · 4 comments · Fixed by #1383
Labels
Type: Enhancement New feature or request

Comments

@joshka
Copy link
Member
joshka commented Sep 20, 2024

Problem

When writing some widget level code (for tui-markdown) and debugging the output of the text in tests, I found that having all fields in Style listed when they're None seemed overly verbose, and made it more difficult to navigate through any assertion errors (e.g. when taking a TDD approach to building things).

Additionally, the content field of the Span/Line/Text seems to dominate in size, as it's usually more complex than the other fields (style, alignment). This pushes them far below the start of the struct representation.

Solution

  • Replace the Debug implementation of Style with one that omits any None values
  • Move the content/spans/lines fields in Span/Line/Text below the other fields (this can be done easily in the struct or by manually defining a Debug implementation)
  • For the various style fields, omit Some(...) surrounding the value
  • Consider omitting defaults entirely

Alternatives

Write a function that provides a more custom compact debugging view of these methods:

fn compact_debug(&self) -> CompactDebugSpan {
...
}

impl Debug for CompactDebugSpan {
...
}

This doesn't work for assertions however, which use the Debug representation, so it seems like a non-starter.

Additional context

A real example from tui-markdown:

 Text {
     lines: [
         Line {
             spans: [
                 Span {
                     content: ">",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
                 Span {
                     content: " ",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
                 Span {
                     content: "Blockquote 1",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
             ],
             style: Style {
                 fg: Some(
                     Green,
                 ),
                 bg: None,
                 underline_color: None,
                 add_modifier: NONE,
                 sub_modifier: NONE,
             },
             alignment: None,
         },
         Line {
             spans: [
                 Span {
                     content: ">",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
                 Span {
                     content: ">",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
                 Span {
                     content: " ",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
                 Span {
                     content: "Nested Blockquote",
                     style: Style {
                         fg: None,
                         bg: None,
                         underline_color: None,
                         add_modifier: NONE,
                         sub_modifier: NONE,
                     },
                 },
             ],
             style: Style {
                 fg: Some(
                     Green,
                 ),
                 bg: None,
                 underline_color: None,
                 add_modifier: NONE,
                 sub_modifier: NONE,
             },
             alignment: None,
         },
     ],
     style: Style {
         fg: None,
         bg: None,
         underline_color: None,
         add_modifier: NONE,
         sub_modifier: NONE,
     },
     alignment: None,
 }

Which becomes after modification:

 Text {
    style: Style {},
    alignment: None,
    lines: [
         Line {
            style: Style { fg: Green, },
            alignment: None,
            spans: [
                 Span {
                     content: ">",
                     style: Style {},
                 },
                 Span {
                     content: " ",
                     style: Style {},
                 },
                 Span {
                     content: "Blockquote 1",
                     style: Style {},
                 },
             ],
         },
         Line {
            style: Style { fg: Green },
            alignment: None,
            spans: [
                 Span {
                     content: ">",
                     style: Style {},
                 },
                 Span {
                     content: ">",
                     style: Style {},
                 },
                 Span {
                     content: " ",
                     style: Style {},
                 },
                 Span {
                     content: "Nested Blockquote",
                     style: Style {},
                 },
             ],
         },
     ],
 }
@joshka joshka added the Type: Enhancement New feature or request label Sep 20, 2024
@joshka
Copy link
Member Author
joshka commented Sep 20, 2024

Omitting default values entirely, and collapsing spans that don't have non-default values to just a string:

 Text {
    lines: [
         Line {
            style: Style { fg: Green, },
            spans: [
                 Span { ">" },
                 Span { " " },
                 Span { "Blockquote 1" },
             ],
         },
         Line {
            style: Style { fg: Green },
            spans: [
                 Span { ">" },
                 Span { ">" },
                 Span { " " },
                 Span { "Nested Blockquote" },
             ],
         },
     ],
 }

110 lines -> 21 lines

@orhun
Copy link
Member
orhun commented Sep 20, 2024

+1 for this, sounds/looks reasonable to me.

@joshka
Copy link
Member Author
joshka commented Sep 20, 2024

Discussion in https://discord.com/channels/1070692720437383208/1286506962783047764

Given:

Text::from_iter([
    Line::from("without line fields"),
    Line::from("with line fields").bold().centered(),
    Line::from_iter([
        Span::from("without span fields"),
        Span::from("with span fields")
            .green()
            .on_black()
            .italic()
            .not_dim(),
    ]),
])

Debug:

Text [Line [Span("without line fields")], Line { style: Style::new().add_modifier(Modifier::BOLD), alignment: Some(Center), spans: [Span("with line fields")] }, Line [Span("without span fields"), Span { style: Style::new().green().on_black().add_modifier(Modifier::ITALIC).remove_modifier(Modifier::DIM), content: "with span fields" }]]

Alternate debug:

Text [
    Line [
        Span("without line fields"),
    ],
    Line {
        style: Style::new().add_modifier(Modifier::BOLD),
        alignment: Some(
            Center,
        ),
        spans: [
            Span("with line fields"),
        ],
    },
    Line [
        Span("without span fields"),
        Span {
            style: Style::new().green().on_black().add_modifier(Modifier::ITALIC).remove_modifier(Modifier::DIM),
            content: "with span fields",
        },
    ],
]

TODO

  • Modifiers
  • Alignment

@joshka
Copy link
Member Author
joshka commented Sep 20, 2024

Span::from("foo") could be a reasonable other approach here.

orhun added a commit that referenced this issue Sep 26, 2024
…#1383)

Given:

```rust
Text::from_iter([
    Line::from("without line fields"),
    Line::from("with line fields").bold().centered(),
    Line::from_iter([
        Span::from("without span fields"),
        Span::from("with span fields")
            .green()
            .on_black()
            .italic()
            .not_dim(),
    ]),
])
```

Debug:
```
Text [Line [Span("without line fields")], Line { style: Style::new().add_modifier(Modifier::BOLD), alignment: Some(Center), spans: [Span("with line fields")] }, Line [Span("without span fields"), Span { style: Style::new().green().on_black().add_modifier(Modifier::ITALIC).remove_modifier(Modifier::DIM), content: "with span fields" }]]
```

Fixes: #1382
---------

Co-authored-by: Orhun Parmaksız <orhun@archlinux.org>
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
joshka added a commit to erak/ratatui that referenced this issue Oct 14, 2024
…ratatui#1383)

Given:

```rust
Text::from_iter([
    Line::from("without line fields"),
    Line::from("with line fields").bold().centered(),
    Line::from_iter([
        Span::from("without span fields"),
        Span::from("with span fields")
            .green()
            .on_black()
            .italic()
            .not_dim(),
    ]),
])
```

Debug:
```
Text [Line [Span("without line fields")], Line { style: Style::new().add_modifier(Modifier::BOLD), alignment: Some(Center), spans: [Span("with line fields")] }, Line [Span("without span fields"), Span { style: Style::new().green().on_black().add_modifier(Modifier::ITALIC).remove_modifier(Modifier::DIM), content: "with span fields" }]]
```

Fixes: ratatui#1382
---------

Co-authored-by: Orhun Parmaksız <orhun@archlinux.org>
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0