Skip to content

Commit 7c966f3

Browse files
Implemented Size checking of widgets
Added border widget to start off widget implementations
1 parent fe9c2cd commit 7c966f3

File tree

7 files changed

+104
-7
lines changed

7 files changed

+104
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ repository = "https://github.com/TheEmeraldBee/ascii-forge"
88
license = "MIT OR Apache-2.0"
99

1010
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
11+
[lib]
12+
doctest = false
1113

1214
[dependencies]
1315
compact_str = "0.8.0"

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
test:
2+
cargo test
3+
4+
15
VERSION_FILE := Cargo.toml
26

37
publish:
48
sed -i -r "s/version=\"0\.0\.0\"/version=\"${VERSION}\"/g" $(VERSION_FILE) \
5-
&& cargo publish --allow-dirty \
9+
&& cargo publish --allow-dirty

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub mod window;
44

55
pub mod math;
66

7+
pub mod widgets;
8+
79
pub mod prelude;
810

911
// Export required crates

src/renderer/render.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::prelude::*;
99
/// This render will return the location of which the last element finished rendering.
1010
/**
1111
`Example`
12-
```rust, no_run
12+
```rust
1313
use crate::prelude::*;
1414
1515
// Create a window
@@ -18,8 +18,8 @@ let window = Window::init()?;
1818
// Render This works! and Another Element! To the window's buffer
1919
render!(
2020
window,
21-
vec2(16, 16) => [ "This works!" ]
22-
vec2(0, 0) => [ "Another Element!" ]
21+
(16, 16) => [ "This works!" ]
22+
(0, 0) => [ "Another Element!" ]
2323
);
2424
```
2525
*/
@@ -29,8 +29,9 @@ macro_rules! render {
2929
#[allow(unused_mut)]
3030
let mut loc;
3131
$(
32-
loc = $loc;
33-
$(loc = $render.render(loc, $buffer.as_mut());)*
32+
loc = Vec2::from($loc);
33+
$(loc = $render.render(loc, $buffer.as_mut()));*;
34+
let _ = loc;
3435
)*
3536
loc
3637
}};
@@ -40,6 +41,12 @@ macro_rules! render {
4041
/// Render's return type is the location the render ended at.
4142
pub trait Render {
4243
fn render(&self, loc: Vec2, buffer: &mut Buffer) -> Vec2;
44+
fn size(&self) -> Vec2 {
45+
let mut buf = Buffer::new((u16::MAX, u16::MAX));
46+
render!(buf, vec2(0, 0) => [ self ]);
47+
buf.shrink();
48+
buf.size()
49+
}
4350
}
4451

4552
/* --------------- Implementations --------------- */
@@ -48,12 +55,18 @@ impl Render for char {
4855
buffer.set(loc, *self);
4956
loc
5057
}
58+
fn size(&self) -> Vec2 {
59+
vec2(1, 1)
60+
}
5161
}
5262

5363
impl Render for &str {
5464
fn render(&self, loc: Vec2, buffer: &mut Buffer) -> Vec2 {
5565
render!(buffer, loc => [ StyledContent::new(ContentStyle::default(), self) ])
5666
}
67+
fn size(&self) -> Vec2 {
68+
StyledContent::new(ContentStyle::default(), self).size()
69+
}
5770
}
5871

5972
impl<R: Render + 'static> From<R> for Box<dyn Render> {
@@ -114,4 +127,13 @@ impl<D: Display> Render for StyledContent<D> {
114127
loc.y -= 1;
115128
loc
116129
}
130+
fn size(&self) -> Vec2 {
131+
let mut width = 0;
132+
let mut height = 0;
133+
for line in format!("{}", self.content()).split('\n') {
134+
width = line.chars().count().max(width);
135+
height += 1;
136+
}
137+
vec2(width as u16, height)
138+
}
117139
}

src/widgets/border.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use crate::prelude::*;
2+
3+
/// A basic border type.
4+
/// Rendering this will put the next content inside of the function
5+
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,
14+
}
15+
16+
impl Border {
17+
pub const fn square(width: u16, height: u16) -> Border {
18+
Border {
19+
width,
20+
height,
21+
horizontal: "─",
22+
vertical: "│",
23+
top_right: "┐",
24+
top_left: "┌",
25+
bottom_left: "└",
26+
bottom_right: "┘",
27+
}
28+
}
29+
}
30+
impl Render for Border {
31+
fn render(&self, loc: Vec2, buffer: &mut Buffer) -> Vec2 {
32+
for y in (loc.y + 1)..(loc.y + self.height - 1) {
33+
buffer.set(vec2(loc.x, y), self.vertical);
34+
buffer.set(vec2(loc.x + self.width - 1, y), self.vertical);
35+
}
36+
37+
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]
40+
);
41+
42+
vec2(loc.x + 1, loc.y + 1)
43+
}
44+
fn size(&self) -> Vec2 {
45+
vec2(self.width, self.height)
46+
}
47+
}
48+
49+
#[cfg(test)]
50+
mod test {
51+
use crate::{
52+
math::Vec2,
53+
render,
54+
widgets::border::Border,
55+
window::{Buffer, Render},
56+
};
57+
58+
#[test]
59+
fn check_size() {
60+
let border = Border::square(16, 16);
61+
let mut buf = Buffer::new((80, 80));
62+
render!(buf, (0, 0) => [ border ]);
63+
buf.shrink();
64+
assert_eq!(buf.size(), border.size())
65+
}
66+
}

src/widgets/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod border;

src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl AsMut<Buffer> for Window {
3131
/// Represents the terminal window, allowing it to be used similar to a buffer,
3232
/// but has extra event handling.
3333
/**
34-
```rust, no_run
34+
```rust,
3535
use ascii_forge::prelude::*;
3636
3737
let mut window = Window::init()?;

0 commit comments

Comments
 (0)