-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
feat(border): add border! macro for easy bitflag manipulation #11
Conversation
border! macro that takes TOP, BOTTOM, LEFT, RIGHT, and ALL and returns a Borders object.
Added tests for the border! macro.
split tests up to make it easier to see why it's failing.
Fixed the treatment of border!() so that it returns Borders::NONE instead of the incorrect Borders::empty(). I also changed from the incorrect Borders::all() to Borders::ALL.
added needed import to example
needed to add another import
removed some imports that weren't needed in the new tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I think we discussed earlier to avoid proc macros. Similar to macros for borders, it can be also used for style, constraints and entire widgets. But the more macros we add the worse it'll get. They are slow to compile and difficult to debug. cc @orhun @mindoodoo |
That's a fair call. Perhaps we suggest this ends up either feature flagged or in a separate lib? |
Feature flag sounds good. |
Added a feature flag called |
Merged with description: Adds a This is gated behind a ratatui = { version = 0.21.0, features = ["macros"] } |
Description
I have added the border! macro. It can be called with any combination of TOP, BOTTOM, LEFT, RIGHT, NONE, or ALL to return a widgets::Borders object with the corrosponding flags set. This is helpful because the current methods of creating custom Borders is ugly and long. Further things such as Borders.add() or Borders.remove() do not allowing chaining which makes constructing the border you want either a multiline operation or requires oring Borders objects. This macro is cleaner, shorter, and much more readable.
Compare:
This is a use of Borders from the widgets::Block documentation
Block::default() .title("Block") .borders(Borders::LEFT | Borders::RIGHT) .border_style(Style::default().fg(Color::White)) .border_type(BorderType::Rounded) .style(Style::default().bg(Color::Black));
This is the same code with the border! macro
Block::default() .title("Block") .borders(border!(LEFT, RIGHT)) .border_style(Style::default().fg(Color::White)) .border_type(BorderType::Rounded) .style(Style::default().bg(Color::Black));
As you can see the second is much easier to read and understand. This is a fairly trivial example but if you need a border with three sides the differences becomes much clearer.
border!(TOP, LEFT, RIGHT)
instead ofBorders::TOP | Borders::LEFT | Borders::RIGHT
.The macro also provides a syntactic sugar for Borders::NONE. Instead of calling border!(NONE) or using Borders::NONE directly you can just call border!() which return Borders::NONE.
Interally the macro functions by calling Borders::empty() and then using .insert() to insert each specified flag. If no flags are specified the macro directly returns Borders::NONE and doesn't waste effort creating an empty Borders and inserting.
I have done my best to add inline documentation to the addition but I'm unsure if I did it correctly/formatted it correctly. it currently passes cargo test but a quick glance over it would be appreciated.
Testing guidelines
I have added tests for the macro to tests in border_macro.rs. These include tests for ALL, specific sides, and the syntactic sugar for NONE: border!(). All tests pass on debian linux when running cargo test.
Checklist