Skip to content

Commit

Permalink
Add a workaround for rust/issues/67295 by adding the feature "doctests"
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp-M committed Feb 18, 2024
1 parent 06052ea commit 3a30b11
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ rand = "0.8"

[lints.clippy]
dbg_macro = "warn"

[features]
# INTERNAL USE ONLY, workaround for https://github.com/rust-lang/rust/issues/67295
doctests = []
44 changes: 21 additions & 23 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use anyhow::Result;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
use crossterm::{
cursor,
event::{DisableFocusChange, DisableMouseCapture, EnableFocusChange, EnableMouseCapture},
Expand All @@ -23,23 +23,21 @@ use crossterm::event::{poll, read, Event as CxEvent, KeyCode, KeyEvent};
use directories::ProjectDirs;
use ratatui::Terminal;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
use std::io::stdout;

use std::{
collections::HashSet,
sync::Arc,
time::{Duration, Instant},
collections::HashSet, sync::Arc, time::{Duration, Instant}
};
use tracing_subscriber::{fmt::writer::MakeWriterExt, layer::SubscriberExt, Registry};
use xilem_core::{AsyncWake, Id, IdPath, MessageResult};

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
use ratatui::backend::TestBackend;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
use ratatui::backend::CrosstermBackend;
#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
use std::io::{Stdout, Write};

// TODO less hardcoding and cross-platform support
Expand All @@ -64,13 +62,13 @@ pub struct App<T: Send + 'static, V: View<T> + 'static> {
return_chan: tokio::sync::mpsc::Sender<(V, V::State, HashSet<Id>)>,
event_chan: tokio::sync::mpsc::Receiver<Event>,

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
event_tx: tokio::sync::mpsc::Sender<Event>,

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
terminal: Terminal<TestBackend>,

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
terminal: Terminal<CrosstermBackend<Stdout>>,
time_since_last_rebuild: Instant,
size: Size,
Expand Down Expand Up @@ -137,10 +135,10 @@ enum UiState {

impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
pub fn new(data: T, app_logic: impl FnMut(&mut T) -> V + Send + 'static) -> Self {
#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
let backend = CrosstermBackend::new(stdout()); // TODO handle errors...

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
let backend = TestBackend::new(80, 40);

let terminal = Terminal::new(backend).unwrap();
Expand Down Expand Up @@ -253,7 +251,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
return_chan: return_tx,
event_chan: event_rx,

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
event_tx: event_tx.clone(),

terminal,
Expand Down Expand Up @@ -344,17 +342,17 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {

root_pod.paint(&mut paint_cx);

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
queue!(stdout(), BeginSynchronizedUpdate)?;

self.terminal.flush()?;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
execute!(stdout(), EndSynchronizedUpdate)?;

self.terminal.swap_buffers();

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
self.terminal.backend_mut().flush()?;
}
Ok(())
Expand Down Expand Up @@ -411,7 +409,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {

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

self.terminal.clear()?;
Expand Down Expand Up @@ -459,7 +457,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
Ok(())
}

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
fn init_terminal(&self) -> Result<()> {
enable_raw_mode()?;
execute!(
Expand All @@ -472,7 +470,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
Ok(())
}

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
fn restore_terminal(&self) -> Result<()> {
execute!(
stdout(),
Expand All @@ -485,12 +483,12 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
Ok(())
}

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
pub fn event_tx(&self) -> tokio::sync::mpsc::Sender<Event> {
self.event_tx.clone()
}

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
pub fn terminal_mut(&mut self) -> &mut Terminal<TestBackend> {
&mut self.terminal
}
Expand All @@ -499,7 +497,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
/// Restore the terminal no matter how the app exits
impl<T: Send + 'static, V: View<T> + 'static> Drop for App<T, V> {
fn drop(&mut self) {
#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
self.restore_terminal()
.unwrap_or_else(|e| eprint!("Restoring the terminal failed: {e}"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub use ratatui::style::{Color, Modifier, Style};
pub use view::*;
pub use widget::CatchMouseButton;

#[cfg(any(test, doctest))]
#[cfg(test)]
mod test_helper;
4 changes: 2 additions & 2 deletions src/widget.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod border;
mod box_constraints;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
mod core;

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
pub(crate) mod core;

mod events;
Expand Down
10 changes: 5 additions & 5 deletions src/widget/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use ratatui::Terminal;
use std::{any::Any, ops::DerefMut};
use xilem_core::{message, Id};

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
use ratatui::backend::TestBackend;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
use ratatui::backend::CrosstermBackend;

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
use std::io::Stdout;

message!(Send);
Expand Down Expand Up @@ -55,10 +55,10 @@ pub struct PaintCx<'a, 'b> {
// TODO mutable? (xilem doesn't do this, but I think there are use cases for this...)
pub(crate) widget_state: &'a mut WidgetState,

#[cfg(any(test, doctest))]
#[cfg(any(test, doctest, feature = "doctests"))]
pub(crate) terminal: &'a mut Terminal<TestBackend>,

#[cfg(not(any(test, doctest)))]
#[cfg(not(any(test, doctest, feature = "doctests")))]
pub(crate) terminal: &'a mut Terminal<CrosstermBackend<Stdout>>,
// TODO this kinda feels hacky, find a better solution for this issue:
// this is currently necessary because the most outer styleable widget should be able to override the style for a styleable widget
Expand Down

0 comments on commit 3a30b11

Please sign in to comment.