Skip to content

Commit b2fa438

Browse files
Updated renderer to correctly handle empty cells instead when rendering
buffers.
1 parent 4526615 commit b2fa438

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

src/renderer/buffer.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ impl Buffer {
4040
}
4141
}
4242

43+
/// Creates a new buffer filled with the given cell type.
44+
pub fn new_filled(size: impl Into<Vec2>, cell: impl Into<Cell>) -> Self {
45+
let cell = cell.into();
46+
let size = size.into();
47+
48+
Self {
49+
size,
50+
cells: vec![cell; size.x as usize * size.y as usize],
51+
}
52+
}
53+
4354
/// Returns the current size of the buffer.
4455
pub fn size(&self) -> Vec2 {
4556
self.size
@@ -64,6 +75,11 @@ impl Buffer {
6475
self.cells[idx] = cell;
6576
}
6677

78+
/// Sets a cell at the given location to an empty cell ('\0')
79+
pub fn del(&mut self, loc: impl Into<Vec2>) {
80+
self.set(loc, '\0');
81+
}
82+
6783
/// Sets all cells at the given location to the given cell
6884
pub fn fill<C: Into<Cell>>(&mut self, cell: C) {
6985
let cell = cell.into();
@@ -99,7 +115,7 @@ impl Buffer {
99115
Some(idx.min((self.size.x as usize * self.size.y as usize) - 1))
100116
}
101117

102-
/// Clears the buffer
118+
/// Clears the buffer, filling it with '\0' (empty) characters
103119
pub fn clear(&mut self) {
104120
*self = Self::new(self.size);
105121
}
@@ -132,17 +148,13 @@ impl Buffer {
132148
res
133149
}
134150

135-
/// Shrinks the buffer to the given size by dropping any cells that are only whitespace
151+
/// Shrinks the buffer to the given size by dropping any cells that are empty ('\0')
136152
pub fn shrink(&mut self) {
137153
let mut max_whitespace_x = 0;
138154
let mut max_whitespace_y = 0;
139155
for x in (0..self.size.x).rev() {
140156
for y in (0..self.size.y).rev() {
141-
if !self
142-
.get((x, y))
143-
.expect("Cell should be in bounds")
144-
.is_empty()
145-
{
157+
if self.get((x, y)).expect("Cell should be in bounds").text() != "\0" {
146158
max_whitespace_x = x.max(max_whitespace_x);
147159
max_whitespace_y = y.max(max_whitespace_y);
148160
}
@@ -183,26 +195,26 @@ impl Buffer {
183195

184196
impl Render for Buffer {
185197
fn render(&self, loc: Vec2, buffer: &mut Buffer) -> Vec2 {
186-
for x in 0..self.size.x {
187-
if x + loc.x >= buffer.size.x {
198+
for x in 0..self.size().x {
199+
if x + loc.x >= buffer.size().x {
188200
break;
189201
}
190-
191-
for y in 0..self.size.y {
192-
if y + loc.y >= buffer.size.y {
202+
for y in 0..self.size().y {
203+
if y + loc.y >= buffer.size().y {
193204
break;
194205
}
195206

196-
let dest = vec2(x + loc.x, y + loc.y);
207+
let source_pos = vec2(x, y);
208+
let dest_pos = vec2(x + loc.x, y + loc.y);
197209

198-
buffer.set(
199-
dest,
200-
self.get(vec2(x, y))
201-
.expect("Cell should be in bounds")
202-
.clone(),
203-
);
210+
if let Some(cell) = self.get(source_pos) {
211+
if cell.text() != "\0" {
212+
buffer.set(dest_pos, cell.clone());
213+
}
214+
}
204215
}
205216
}
217+
206218
vec2(loc.x + buffer.size().x, loc.y + buffer.size().y)
207219
}
208220
}

src/renderer/cell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Cell {
1414

1515
impl Default for Cell {
1616
fn default() -> Self {
17-
Self::chr(' ')
17+
Self::chr('\0')
1818
}
1919
}
2020

src/widgets/border.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ impl Render for Border {
3232
if self.size.x < 3 || self.size.y < 3 {
3333
return loc;
3434
}
35+
36+
// Fill the interior with spaces
37+
for y in (loc.y + 1)..(loc.y + self.size.y.saturating_sub(1)) {
38+
for x in (loc.x + 1)..(loc.x + self.size.x.saturating_sub(1)) {
39+
buffer.set(vec2(x, y), " ");
40+
}
41+
}
42+
3543
for y in (loc.y + 1)..(loc.y + self.size.y.saturating_sub(1)) {
3644
buffer.set(vec2(loc.x, y), self.vertical);
3745
buffer.set(

src/window.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ impl Window {
7070
pub fn new(io: io::Stdout) -> io::Result<Self> {
7171
Ok(Self {
7272
io,
73-
buffers: [Buffer::new(size()?), Buffer::new(size()?)],
73+
buffers: [
74+
Buffer::new_filled(size()?, ' '),
75+
Buffer::new_filled(size()?, ' '),
76+
],
7477
active_buffer: 0,
7578
events: vec![],
7679
last_cursor: (false, vec2(0, 0), SetCursorStyle::SteadyBlock),
@@ -88,7 +91,7 @@ impl Window {
8891
let size = vec2(size()?.0, height);
8992
Ok(Self {
9093
io,
91-
buffers: [Buffer::new(size), Buffer::new(size)],
94+
buffers: [Buffer::new_filled(size, ' '), Buffer::new_filled(size, ' ')],
9295
active_buffer: 0,
9396
events: vec![],
9497
last_cursor: (false, vec2(0, 0), SetCursorStyle::SteadyBlock),
@@ -164,7 +167,7 @@ impl Window {
164167
/// Swaps the buffers, clearing the old buffer. Used automatically by the window's update method.
165168
pub fn swap_buffers(&mut self) {
166169
self.active_buffer = 1 - self.active_buffer;
167-
self.buffers[self.active_buffer].clear();
170+
self.buffers[self.active_buffer].fill(' ');
168171
}
169172

170173
/// Returns the current known size of the buffer's window.
@@ -335,8 +338,10 @@ impl Window {
335338
match event {
336339
Event::Resize(width, height) => {
337340
if self.inline.is_none() {
338-
self.buffers =
339-
[Buffer::new((width, height)), Buffer::new((width, height))];
341+
self.buffers = [
342+
Buffer::new_filled((width, height), ' '),
343+
Buffer::new_filled((width, height), ' '),
344+
];
340345
self.just_resized = true;
341346
}
342347
}

0 commit comments

Comments
 (0)