@@ -2,22 +2,21 @@ use crate::prelude::*;
22
33/// A basic border type.
44/// Rendering this will put the next content inside of the function
5+ /// Borders will skip rendering if their size is under a 3x3
56pub struct Border {
6- width : u16 ,
7- height : u16 ,
8- horizontal : & ' static str ,
9- vertical : & ' static str ,
10- top_left : & ' static str ,
11- top_right : & ' static str ,
12- bottom_left : & ' static str ,
13- bottom_right : & ' static str ,
7+ pub size : Vec2 ,
8+ pub horizontal : & ' static str ,
9+ pub vertical : & ' static str ,
10+ pub top_left : & ' static str ,
11+ pub top_right : & ' static str ,
12+ pub bottom_left : & ' static str ,
13+ pub bottom_right : & ' static str ,
1414}
1515
1616impl Border {
1717 pub const fn square ( width : u16 , height : u16 ) -> Border {
1818 Border {
19- width,
20- height,
19+ size : vec2 ( width, height) ,
2120 horizontal : "─" ,
2221 vertical : "│" ,
2322 top_right : "┐" ,
@@ -27,22 +26,29 @@ impl Border {
2726 }
2827 }
2928}
29+
3030impl Render for Border {
3131 fn render ( & self , loc : Vec2 , buffer : & mut Buffer ) -> Vec2 {
32- for y in ( loc. y + 1 ) ..( loc. y + self . height - 1 ) {
32+ if self . size . x < 3 || self . size . y < 3 {
33+ return loc;
34+ }
35+ for y in ( loc. y + 1 ) ..( loc. y + self . size . y . saturating_sub ( 1 ) ) {
3336 buffer. set ( vec2 ( loc. x , y) , self . vertical ) ;
34- buffer. set ( vec2 ( loc. x + self . width - 1 , y) , self . vertical ) ;
37+ buffer. set (
38+ vec2 ( loc. x + self . size . x . saturating_sub ( 1 ) , y) ,
39+ self . vertical ,
40+ ) ;
3541 }
3642
3743 let _ = render ! ( buffer,
38- loc => [ self . top_left, self . horizontal. repeat( self . width as usize - 2 ) , self . top_right] ,
39- vec2( loc. x, loc. y + self . height - 1 ) => [ self . bottom_left, self . horizontal. repeat( self . width as usize - 2 ) , self . bottom_right]
44+ loc => [ self . top_left, self . horizontal. repeat( self . size . x . saturating_sub ( 2 ) as usize ) , self . top_right] ,
45+ vec2( loc. x, loc. y + self . size . y . saturating_sub ( 1 ) ) => [ self . bottom_left, self . horizontal. repeat( self . size . x . saturating_sub ( 2 ) as usize ) , self . bottom_right]
4046 ) ;
4147
4248 vec2 ( loc. x + 1 , loc. y + 1 )
4349 }
4450 fn size ( & self ) -> Vec2 {
45- vec2 ( self . width , self . height )
51+ self . size
4652 }
4753}
4854
@@ -55,6 +61,13 @@ mod test {
5561 window:: { Buffer , Render } ,
5662 } ;
5763
64+ #[ test]
65+ fn render_small ( ) {
66+ let border = Border :: square ( 0 , 0 ) ;
67+ // Ensure no panics
68+ let _ = Buffer :: sized_element ( border) ;
69+ }
70+
5871 #[ test]
5972 fn check_size ( ) {
6073 let border = Border :: square ( 16 , 16 ) ;
0 commit comments