Skip to content

Commit 46f2fda

Browse files
Fixed issue with border panicking
Made Border fields public Made vec2 method const Fixed issues with doctests, and made them run or compile
1 parent e23249f commit 46f2fda

File tree

6 files changed

+56
-43
lines changed

6 files changed

+56
-43
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ readme = "README.md"
77
repository = "https://github.com/TheEmeraldBee/ascii-forge"
88
license = "MIT OR Apache-2.0"
99

10-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
11-
[lib]
12-
doctest = false
13-
1410
[dependencies]
1511
compact_str = "0.9.0"
1612
crossterm = "0.29.0"

src/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ impl<V: Into<Vec2>> SubAssign<V> for Vec2 {
5959
}
6060

6161
/// Creates a Vec2 from the given inputs.
62-
pub fn vec2(x: u16, y: u16) -> Vec2 {
62+
pub const fn vec2(x: u16, y: u16) -> Vec2 {
6363
Vec2 { x, y }
6464
}

src/renderer/buffer.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ A screen buffer that can be rendered to, has a size
66
This is the backbone of ascii-forge
77
88
`Example`
9-
```rust, no_run
10-
use ascii_forge::prelude::*;
11-
9+
```rust
10+
# use ascii_forge::prelude::*;
11+
# fn main() {
1212
// A 30x30 buffer window
13-
let mut buffer = Buffer::new(30, 30);
13+
let mut buffer = Buffer::new((30, 30));
1414
1515
// Render Hello World to the top left of the buffer
16-
render!(
17-
buffer, {
18-
(0, 0) => "Hello World!"
19-
}
20-
);
16+
render!(buffer, (0, 0) => [ "Hello World!" ]);
17+
# }
2118
```
2219
2320
*/

src/renderer/render.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ use crate::prelude::*;
1010
/**
1111
`Example`
1212
```rust
13-
use crate::prelude::*;
14-
15-
// Create a window
16-
let window = Window::init()?;
13+
# use ascii_forge::prelude::*;
14+
# fn main() -> std::io::Result<()> {
15+
// Create a buffer
16+
let mut buffer = Buffer::new((32, 32));
1717
1818
// Render This works! and Another Element! To the window's buffer
1919
render!(
20-
window,
21-
(16, 16) => [ "This works!" ]
20+
buffer,
21+
(16, 16) => [ "This works!" ],
2222
(0, 0) => [ "Another Element!" ]
2323
);
24+
25+
# Ok(())
26+
# }
2427
```
2528
*/
2629
#[macro_export]

src/widgets/border.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
56
pub 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

1616
impl 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+
3030
impl 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);

src/window.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ impl AsMut<Buffer> for Window {
3030
/// Represents the terminal window, allowing it to be used similar to a buffer,
3131
/// but has extra event handling.
3232
/**
33-
```rust,
34-
use ascii_forge::prelude::*;
33+
```rust, no_run
34+
# use ascii_forge::prelude::*;
3535
36+
# fn main() -> std::io::Result<()> {
3637
let mut window = Window::init()?;
3738
38-
render!(
39-
window,
40-
[
41-
(10, 10) => "Element Here!"
42-
]
43-
)
39+
render!(window, (10, 10) => [ "Element Here!" ]);
40+
41+
# Ok(())
42+
# }
4443
```
4544
*/
4645
pub struct Window {
@@ -375,7 +374,12 @@ impl Window {
375374
/**
376375
Example
377376
```rust, no_run
377+
# use ascii_forge::prelude::*;
378+
# fn main() -> std::io::Result<()> {
379+
# let mut window = Window::init()?;
378380
event!(window, Event::Key(e) => e.code == KeyCode::Char('q'));
381+
# Ok(())
382+
# }
379383
```
380384
*/
381385
#[macro_export]

0 commit comments

Comments
 (0)