Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

border! macro for the easy and pretty creation of Borders bitflags. #484

Closed
wants to merge 9 commits into from

Conversation

aghayes
Copy link

@aghayes aghayes commented May 22, 2021

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 of Borders::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

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.
semicolon in examples
added needed import to example
needed to add another import
removed some imports that weren't needed in the new tests
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants