-
Notifications
You must be signed in to change notification settings - Fork 254
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
Remove block::Title
and block::Position
, add Block::top_title()
and Block::bottom_title()
for ergonomics
#738
Comments
block::Title
and block::Position
, add Block::top_title()
and Block::bottom_title()
block::Title
and block::Position
, add Block::top_title()
and Block::bottom_title()
block::Title
and block::Position
, add Block::top_title()
and Block::bottom_title()
for ergonomics
I like this idea! I think additionally let block = Block::default()
.left_title(Line::from("xxx").alignment(Alignment::Center))
.right_title(Line::from("xxx").alignment(Alignment::End)) This language would map more closely to CSS https://developer.mozilla.org/en-US/docs/Web/CSS/align-self If we wanted to be fully consistent, we would include |
I think it's a good idea too. Could you make your list a task list so we have this as a tracking issue.
This is a good idea IMO.
Could you clarify this one? I'm not sure what you're referencing. Perhaps you meant Change |
Done. (markdown ate the angle brackets.) |
This adds the ability to add titles to the top and bottom of a block without having to use the `Title` struct (which will be removed in a future release - likely v0.28.0). Fixes a subtle bug if the title was created from a right aligned Line and was also right aligned. The title would be rendered one cell too far to the right. ```rust Block::bordered() .title_top(Line::raw("A").left_aligned()) .title_top(Line::raw("B").centered()) .title_top(Line::raw("C").right_aligned()) .title_bottom(Line::raw("D").left_aligned()) .title_bottom(Line::raw("E").centered()) .title_bottom(Line::raw("F").right_aligned()) .render(buffer.area, &mut buffer); // renders "┌A─────B──────C", "│ │", "└D─────E──────F", ``` Addresses part of #738
This adds the ability to add titles to the top and bottom of a block without having to use the `Title` struct (which will be removed in a future release - likely v0.28.0). Fixes a subtle bug if the title was created from a right aligned Line and was also right aligned. The title would be rendered one cell too far to the right. ```rust Block::bordered() .title_top(Line::raw("A").left_aligned()) .title_top(Line::raw("B").centered()) .title_top(Line::raw("C").right_aligned()) .title_bottom(Line::raw("D").left_aligned()) .title_bottom(Line::raw("E").centered()) .title_bottom(Line::raw("F").right_aligned()) .render(buffer.area, &mut buffer); // renders "┌A─────B─────C┐", "│ │", "└D─────E─────F┘", ``` Addresses part of #738
This adds the ability to add titles to the top and bottom of a block without having to use the `Title` struct (which will be removed in a future release - likely v0.28.0). Fixes a subtle bug if the title was created from a right aligned Line and was also right aligned. The title would be rendered one cell too far to the right. ```rust Block::bordered() .title_top(Line::raw("A").left_aligned()) .title_top(Line::raw("B").centered()) .title_top(Line::raw("C").right_aligned()) .title_bottom(Line::raw("D").left_aligned()) .title_bottom(Line::raw("E").centered()) .title_bottom(Line::raw("F").right_aligned()) .render(buffer.area, &mut buffer); // renders "┌A─────B─────C┐", "│ │", "└D─────E─────F┘", ``` Addresses part of #738 <!-- Please read CONTRIBUTING.md before submitting any pull request. -->
216 bytes doesn't matter (unless you're writing a TUI to run on a microcontroller). At apple prices for RAM that's $0.0000027 Methods taking self and returning Self is pretty much the standard way all setter methods work in Ratatui (and won't change due to this). We inherited this pattern from tui-rs and intend to keep consistent with the approach at least in the foreseeable future. The pattern has a name: Builder-lite. That said we could consider setting all the titles at once, perhaps something like: let block = Block::new().top_titles([Line::from("left").left_aligned(), Line::from("center").centered(),...); There's possibly an idea where we could change the pattern to a more canonical builder pattern and gate that behind some feature flag, but it's something we'd want to do for every setter in the entire library, and it would be a huge mindset change that would require lots of docs and patience to get right. |
I happen to remember a general solution in Rust when you're stuck with temporary ownership taking like self.block = replace_with::replace_with(
&mut self.block, // take &mut, thus assignment works
|| custom_block_default(),
|block| block.title(title) // block is owned, and no need to clone it
) With this solution, we don't have to modify the current builder-lite APIs. The reason things like |
Yeah, there's going to be some breakage no matter what we do on this. It's worth always starting at the design stage, thinking about how each usage breaks as each increment part of the problem is released (and check the impact using github searches for code that calls the breaking methods). If you haven't done this before, it's worth doing the exercise in full detail rather than assuming the deprecation steps I listed above are correct (I get things wrong sometimes, and it was a while since I suggested the approach to deprecate). Our goal is as much as possible to let people have ~2 major releases of notification of breaking changes whenever possible (so that they can generally upgrade to a release and be guided by build warnings about what changes need to be made in their code). |
Should I make a separate branch and wait till we make a major release? Or it's too early? How often are major releases made? I mean the earlier I make the PR the more merging I will have to do. |
Don't stress too much about it. A PR is a branch. The work to merge / integrate this will be the same order of magnitude whether there's an formal branch taken and tracked at a particular point or not. |
Okay assign me to this issue please. (I've already removed it, now just to edit docs and tests) |
Can you pleas make a note of the plan you're following for this so we're on the same page. As a clarification, this is what I meant when I wrote the following:
|
I've done all of the checklisted stuff in #1012 since the changes can't be made separately. |
@joshka The signature of title_top is the same as |
Hey, sorry for letting this one drop. I've had other priorities on my time and this hasn't bubbled up to the top yet. I'll hopefully get back to looking at this soon. |
Heads up, I just opened #1090 which, if accepted, would impact this issue. In that issue I described a solution that accomplishes the part of the goal of this PR (removing |
In my opinion title should be deprecated aswell , because it's name is ambiguous, and people can just find and replace title( with title_top(, there are several utilities to do so. Instead, we should add a title_at_position function that accepts Into, and an argument of TitlePosition (located in block.rs), which will call title_top and title_bottom respectively. I've already implemented it in my deprecated title PR |
Description
block::Title
andblock::Position
sit weirdly in the API. They can't be included in the prelude as the names would clash pretty annoyingly with many other type names in common use in apps, but also Title does not actually need to exist.Title has 3 fields -
content (Line)
,alignment
andposition
. ButLine
already has analignment
field, so the title's alignment is superfluous.Position
is a property of where the title is rendered, and so is a property of theBlock
, not the title itself. So if you remove the superfluous fields, that leavesTitle
just being aLine
.We should remove
Title
andPosition
from the API entirely.Suggested fixes
Transition to having just:
To do this nicely, we'd need to:
top_title<T: Into<Line>>(line: T)
and similarlybottom_title(...)
Title as deprecated
Title::position()
deprecated and point atBlock::top_title()
/Block::bottom::title
Position
as deprecated`title_on_bottom
(already deprecated)Block::title_position
It's possible that we could add a
From<Title>
for Line implementation that drops the position and make existing code still work except for things with bottom titles for a while too.It's a good idea to split this up into a few PRs:
top_
andbottom_
fields and setters, update the rendering to use these instead of the current titles field (non breaking change)From<Title>
for LineBlock::title
to acceptInto<Line>
instead ofInto<Title>
The external impact of this is 77 files, so fairly small right now:
https://github.com/search?q=ratatui+AND+block+AND+%22Title%3A%3Afrom%22+AND+%28NOT+repo%3Aratatui-org%2Fratatui%29+AND+%28NOT+is%3Afork%29&type=code
The text was updated successfully, but these errors were encountered: