Skip to content

Commit

Permalink
Fix print_buffer (#12)
Browse files Browse the repository at this point in the history
* Fix print_buffer

It's still not perfect, but I think it's pretty useful already.
A nuisance is that the test output starts immediately after the last character
in the last line of the widget.
I haven't found a way to prevent that.

* Ensure errors from the spawned thread are not masked

fixes #13

The error with multiple render_view() tests was caused by global logging being
initialized more than once.
See also tokio-rs/console#505

My workaround is a bit hacky, but I think there are a bunch of things, including
logging, that should be set up using a configuation struct passed to `fn new()`
anyway. Cleaning that workaround up would go with that task automatically.
  • Loading branch information
zoechi authored Feb 9, 2024
1 parent 7d52e43 commit d089a2b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
6 changes: 5 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,13 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
}
}

pub fn run(mut self) -> Result<()> {
pub fn run(self) -> Result<()> {
let _guard = setup_logging(tracing::Level::DEBUG)?;
self.run_without_logging()
}

// TODO(zoechi): setup proper configuration for App
pub fn run_without_logging(mut self) -> Result<()> {
#[cfg(not(test))]
self.init_terminal()?;

Expand Down
24 changes: 12 additions & 12 deletions src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use std::io::stdout;
use std::marker::PhantomData;
use std::sync::Arc;

use crossterm::cursor::MoveToNextLine;
use crossterm::execute;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use ratatui::backend::TestBackend;
use ratatui::layout::Size;
use ratatui::prelude::*;
Expand Down Expand Up @@ -45,15 +42,19 @@ pub fn render_view<T: Send + 'static>(
.backend_mut()
.resize(buffer_size.width, buffer_size.height);

app.run()
app.run_without_logging().unwrap()
});

let event_tx = event_rx.blocking_recv().unwrap();

let buffer = message_rx.blocking_recv().unwrap();
let buffer = message_rx.blocking_recv();
let send_quit_ack = event_tx.blocking_send(Event::Quit);

event_tx.blocking_send(Event::Quit).unwrap();
let _ = join_handle.join().unwrap();
join_handle.join().unwrap();

// delay unwrapping until after join_handle.join() to not mask errors from the spawned thread
send_quit_ack.unwrap();
let buffer = buffer.unwrap();

print_buffer(&buffer).unwrap();

Expand Down Expand Up @@ -216,18 +217,17 @@ impl Widget for DebugWidget {
/// Running it multiple times might usually leads to good output (for now, with small widget output)
pub fn print_buffer(buffer: &Buffer) -> std::io::Result<()> {
if env::var("DEBUG_SNAPSHOT").is_ok() {
enable_raw_mode()?;
execute!(stdout(), MoveToNextLine(0))?;
let mut terminal = Terminal::with_options(
CrosstermBackend::new(stdout()),
TerminalOptions {
viewport: Viewport::Inline(buffer.area.height),
viewport: Viewport::Fixed(buffer.area),
},
)?;

terminal.clear()?;
terminal.current_buffer_mut().clone_from(buffer);
terminal.flush()?;
execute!(stdout(), MoveToNextLine(0))?;
disable_raw_mode()?;
crossterm::queue!(stdout(), crossterm::cursor::MoveTo(0, buffer.area.height))?;
};
Ok(())
}
16 changes: 15 additions & 1 deletion src/view/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ mod tests {

#[test]
fn simple_block_test() {
let sut = Arc::new(block("some text".fg(Color::Cyan)));
let sut = Arc::new(block("some text".fg(Color::Cyan)).with_borders(BorderKind::Straight));
let buffer = render_view(
Size {
width: 15,
Expand All @@ -330,4 +330,18 @@ mod tests {

insta::assert_debug_snapshot!(buffer);
}

#[test]
fn too_small_block() {
let sut = Arc::new(block("some text".fg(Color::Cyan)).with_borders(BorderKind::Straight));
let buffer = render_view(
Size {
width: 7,
height: 5,
},
sut,
AppState,
);
insta::assert_debug_snapshot!(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ expression: buffer
Buffer {
area: Rect { x: 0, y: 0, width: 15, height: 5 },
content: [
"some text ",
" ",
" ",
" ",
" ",
"┌─────────────┐",
"│some text ",
" ",
" ",
"└─────────────┘",
],
styles: [
x: 0, y: 0, fg: Cyan, bg: Reset, underline: Reset, modifier: NONE,
x: 9, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 1, y: 1, fg: Cyan, bg: Reset, underline: Reset, modifier: NONE,
x: 10, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
]
}
19 changes: 19 additions & 0 deletions src/view/snapshots/trui__view__block__tests__too_small_block.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: src/view/block.rs
expression: buffer
---
Buffer {
area: Rect { x: 0, y: 0, width: 7, height: 5 },
content: [
"┌─────┐",
"│some │",
"│ │",
"│ │",
"└─────┘",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 1, y: 1, fg: Cyan, bg: Reset, underline: Reset, modifier: NONE,
x: 6, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
]
}

0 comments on commit d089a2b

Please sign in to comment.