8000 chore(style): make Debug output for Text/Line/Span/Style more concise by joshka · Pull Request #1383 · ratatui/ratatui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore(style): make Debug output for Text/Line/Span/Style more concise #1383

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

Merged
merged 9 commits into from
Sep 26, 2024

Conversation

joshka
Copy link
Member
@joshka 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",
        },
    ],
]

Fixes: #1382


TODO

  • Modifiers
  • Alignment

Copy link
Contributor
github-actions bot commented Sep 20, 2024

🐰 Bencher Report

Branch1383/merge
Testbedubuntu-latest

⚠️ WARNING: The following Measure does not have a Threshold. Without a Threshold, no Alerts will ever be generated!

Click here to create a new Threshold
For more information, see the Threshold documentation.
To only post results if a Threshold exists, set the --ci-only-thresholds CLI flag.

Click to view all benchmark results
BenchmarkLatencynanoseconds (ns)
barchart/render/2048📈 view plot
⚠️ NO THRESHOLD
215,220.00
barchart/render/256📈 view plot
⚠️ NO THRESHOLD
145,580.00
barchart/render/64📈 view plot
⚠️ NO THRESHOLD
94,025.00
barchart/render_grouped/2048📈 view plot
⚠️ NO THRESHOLD
364,970.00
barchart/render_grouped/256📈 view plot
⚠️ NO THRESHOLD
155,850.00
barchart/render_grouped/64📈 view plot
⚠️ NO THRESHOLD
143,600.00
barchart/render_horizontal/2048📈 view plot
⚠️ NO THRESHOLD
166,650.00
barchart/render_horizontal/256📈 view plot
⚠️ NO THRESHOLD
83,081.00
barchart/render_horizontal/64📈 view plot
⚠️ NO THRESHOLD
76,851.00
block/render_all_feature/100x50📈 view plot
⚠️ NO THRESHOLD
10,794.00
block/render_all_feature/200x50📈 view plot
⚠️ NO THRESHOLD
18,700.00
block/render_all_feature/256x256📈 view plot
⚠️ NO THRESHOLD
86,993.00
block/render_empty/100x50📈 view plot
⚠️ NO THRESHOLD
5,713.60
block/render_empty/200x50📈 view plot
⚠️ NO THRESHOLD
11,118.00
block/render_empty/256x256📈 view plot
⚠️ NO THRESHOLD
73,234.00
buffer/empty/16📈 view plot
⚠️ NO THRESHOLD
787.85
buffer/empty/255📈 view plot
⚠️ NO THRESHOLD
229,770.00
buffer/empty/64📈 view plot
⚠️ NO THRESHOLD
13,428.00
buffer/filled/16📈 view plot
⚠️ NO THRESHOLD
778.78
buffer/filled/255📈 view plot
⚠️ NO THRESHOLD
225,710.00
buffer/filled/64📈 view plot
⚠️ NO THRESHOLD
13,118.00
buffer/with_lines/16📈 view plot
⚠️ NO THRESHOLD
15,526.00
buffer/with_lines/255📈 view plot
⚠️ NO THRESHOLD
14,812.00
buffer/with_lines/64📈 view plot
⚠️ NO THRESHOLD
16,478.00
line_render/Center/0📈 view plot
⚠️ NO THRESHOLD
4.05
line_render/Center/10📈 view plot
⚠️ NO THRESHOLD
641.29
line_ren 8000 der/Center/3📈 view plot
⚠️ NO THRESHOLD
314.18
line_render/Center/4📈 view plot
⚠️ NO THRESHOLD
354.86
line_render/Center/42📈 view plot
⚠️ NO THRESHOLD
833.59
line_render/Center/6📈 view plot
⚠️ NO THRESHOLD
396.12
line_render/Center/7📈 view plot
⚠️ NO THRESHOLD
453.92
line_render/Left/0📈 view plot
⚠️ NO THRESHOLD
4.06
line_render/Left/10📈 view plot
⚠️ NO THRESHOLD
593.33
line_render/Left/3📈 view plot
⚠️ NO THRESHOLD
225.18
line_render/Left/4📈 view plot
⚠️ NO THRESHOLD
239.94
line_render/Left/42📈 view plot
⚠️ NO THRESHOLD
834.77
line_render/Left/6📈 view plot
⚠️ NO THRESHOLD
384.00
line_render/Left/7📈 view plot
⚠️ NO THRESHOLD
397.88
line_render/Right/0📈 view plot
⚠️ NO THRESHOLD
4.05
line_render/Right/10📈 view plot
⚠️ NO THRESHOLD
576.83
line_render/Right/3📈 view plot
⚠️ NO THRESHOLD
286.54
line_render/Right/4📈 view plot
⚠️ NO THRESHOLD
346.07
line_render/Right/42📈 view plot
⚠️ NO THRESHOLD
833.80
line_render/Right/6📈 view plot
⚠️ NO THRESHOLD
461.87
line_render/Right/7📈 view plot
⚠️ NO THRESHOLD
525.72
list/render/16384📈 view plot
⚠️ NO THRESHOLD
1,218,200.00
list/render/2048📈 view plot
⚠️ NO THRESHOLD
326,800.00
list/render/64📈 view plot
⚠️ NO THRESHOLD
196,640.00
list/render_scroll_half/16384📈 view plot
⚠️ NO THRESHOLD
1,230,400.00
list/render_scroll_half/2048📈 view plot
⚠️ NO THRESHOLD
330,240.00
list/render_scroll_half/64📈 view plot
⚠️ NO THRESHOLD
136,720.00
paragraph/new/2048📈 view plot
⚠️ NO THRESHOLD
259,560.00
paragraph/new/64📈 view plot
⚠️ NO THRESHOLD
6,756.20
paragraph/new/65535📈 view plot
⚠️ NO THRESHOLD
8,306,300.00
paragraph/render/2048📈 view plot
⚠️ NO THRESHOLD
706,660.00
paragraph/render/64📈 view plot
⚠️ NO THRESHOLD
665,210.00
paragraph/render/65535📈 view plot
⚠️ NO THRESHOLD
1,801,100.00
paragraph/render_scroll_full/2048📈 view plot
⚠️ NO THRESHOLD
575,270.00
paragraph/render_scroll_full/64📈 view plot
⚠️ NO THRESHOLD
691,720.00
paragraph/render_scroll_full/65535📈 view plot
⚠️ NO THRESHOLD
1,672,600.00
paragraph/render_scroll_half/2048📈 view plot
⚠️ NO THRESHOLD
575,270.00
paragraph/render_scroll_half/64📈 view plot
⚠️ NO THRESHOLD
706,860.00
paragraph/render_scroll_half/65535📈 view plot
⚠️ NO THRESHOLD
1,663,500.00
paragraph/render_wrap/2048📈 view plot
⚠️ NO THRESHOLD
311,660.00
paragraph/render_wrap/64📈 view plot
⚠️ NO THRESHOLD
279,670.00
paragraph/render_wrap/65535📈 view plot
⚠️ NO THRESHOLD
1,472,800.00
paragraph/render_wrap_scroll_full/2048📈 view plot
⚠️ NO THRESHOLD
313,750.00
paragraph/render_wrap_scroll_full/64📈 view plot
⚠️ NO THRESHOLD
279,790.00
paragraph/render_wrap_scroll_full/65535📈 view plot
⚠️ NO THRESHOLD
1,463,500.00
rect_rows/rows/1024📈 view plot
⚠️ NO THRESHOLD
485.58
rect_rows/rows/16📈 view plot
⚠️ NO THRESHOLD
7.47
rect_rows/rows/65535📈 view plot
⚠️ NO THRESHOLD
30,762.00
sparkline/render/2048📈 view plot
⚠️ NO THRESHOLD
121,980.00
sparkline/render/256📈 view plot
⚠️ NO THRESHOLD
120,960.00
sparkline/render/64📈 view plot
⚠️ NO THRESHOLD
38,365.00
🐰 View full continuous benchmarking report in Bencher

Copy link
Member
@orhun orhun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just gave this a try and it works well - however I'm a bit concerned about the complexity that this implementation might gain in the future. I see that the debug output is now as close as possible to the actual API and doing that might require more string work in the future. Also, there are some parts that can be extracted or can be simplified, e.g. Color -> snake_case color name conversion.

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

I just gave this a try and it works well - however I'm a bit concerned about the complexity that this implementation might gain in the future. I 8000 see that the debug output is now as close as possible to the actual API and doing that might require more string work in the future. Also, there are some parts that can be extracted or can be simplified, e.g. Color -> snake_case color name conversion.

I'm not too worried about the future concerns as we can handle those in the future when we need it. Keeping it simple as possible though makes sense. I wouldn't introduce a crate for case conversion unless it was also used in some other part of Ratatui, but I would extract color name conversion into a small function and consider making this public if it made sense.

Mostly I wanted to get some general buy-in that this was useful. It certainly was for me when implementing some stuff for tui-markdown. It was easy to write a broken test asserting some output (which was a Text instance), and then look at the assertion failure message to make it easy to know exactly what to implement.

@orhun
Copy link
Member
orhun commented Sep 25, 2024

Yup, extracting that to a small function would be nice. Also, what else do you have in mind for going forward with this?

I see some todos which I can take a look if you want.

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

I see some todos which I can take a look if you want.

Go for it - I'm just catching up after a few days away, but this should be an easy one to complete quickly:

  • Modifiers should be .bold() .not_bold() etc.
  • alignment should be Alignment::Left, etc. or hidden
  • move the implementations of the things that make sense to the stylize module
  • I was thinking maybe Span::from("") instead of Span(""), but can take or leave it.
  • likely keep everything pub(crate) unless there's an obvious usefulness for it to be pub

A guiding idea is that debug view should not be considered stable, so we can change this as needed.

Copy link
codecov bot commented Sep 26, 2024

Codecov Report

Attention: Patch coverage is 42.17687% with 85 lines in your changes missing coverage. Please review.

Project coverage is 93.5%. Comparing base (784f67a) to head (a75abe5).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/text/text.rs 0.0% 39 Missing ⚠️
src/style.rs 47.5% 21 Missing ⚠️
src/text/line.rs 0.0% 14 Missing ⚠️
src/text/span.rs 0.0% 9 Missing ⚠️
src/style/stylize.rs 95.2% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1383     +/-   ##
=======================================
- Coverage   94.0%   93.5%   -0.5%     
=======================================
  Files         67      67             
  Lines      15750   15897    +147     
=======================================
+ Hits       14809   14871     +62     
- Misses       941    1026     +85     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@orhun
Copy link
Member
orhun commented Sep 26, 2024

Addressed the comments, can you take a look? :)

Copy link
Member Author
@joshka joshka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one small nit on naming.

@joshka joshka marked this pull request as ready for review September 26, 2024 10:15
@joshka joshka requested a review from a team as a code owner September 26, 2024 10:15
@joshka
Copy link
Member Author
joshka commented Sep 26, 2024

LGTM. I can't approve this as the initial Author

@orhun orhun merged commit bc10af5 into main Sep 26, 2024
39 of 40 checks passed
@orhun orhun changed the title chore: make Debug output for Text/Line/Span/Style more concise chore(style): make Debug output for Text/Line/Span/Style more concise Sep 26, 2024
@orhun orhun deleted the jm/concise-debug branch September 26, 2024 17:38
joshka added a commit that referenced this pull request Oct 8, 2024
joshka added a commit that referenced this pull request Oct 8, 2024
orhun pushed a commit that referenced this pull request Oct 8, 2024 6D40
Improves #1383

The following now round trips when formatted for debug.
This will make it easier to use insta when testing text related views of
widgets.

```rust
Text::from_iter([
    Line::from("Hello, world!"),
    Line::from("How are you?").bold().left_aligned(),
    Line::from_iter([
        Span::from("I'm "),
        Span::from("doing ").italic(),
        Span::from("great!").bold(),
    ]),
]).on_blue().italic().centered()
```
joshka added a commit to erak/ratatui that referenced this pull request 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>
joshka added a commit to erak/ratatui that referenced this pull request Oct 14, 2024
…tui#1410)

Improves ratatui#1383

The following now round trips when formatted for debug.
This will make it easier to use insta when testing text related views of
widgets.

```rust
Text::from_iter([
    Line::from("Hello, world!"),
    Line::from("How are you?").bold().left_aligned(),
    Line::from_iter([
        Span::from("I'm "),
        Span::from("doing ").italic(),
        Span::from("great!").bold(),
    ]),
]).on_blue().italic().centered()
```
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

Successfully merging this pull request may close these issues.

Debug formatting for Style, Span, Line, Text
2 participants
0