From 829dd77debd08c2500a72d624ca803fe138d9ba4 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 22 Sep 2016 22:00:58 -0700 Subject: [PATCH] Add `&Backend` to Printer struct Makes `::B` public but undocumented. Backend is currently an immutable ref, may need to become mutable to be more useful (will need to change the View trait) --- src/lib.rs | 11 ++++++++--- src/printer.rs | 26 ++++++++++++++++++-------- src/view/scroll.rs | 4 +++- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3a48122..61fb6a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,6 +132,8 @@ pub struct Cursive { running: bool, + backend: B, + cb_source: mpsc::Receiver>, cb_sink: mpsc::Sender>, } @@ -140,7 +142,8 @@ new_default!(Cursive); // Use the Ncurses backend. // TODO: make this feature-driven -type B = NcursesBackend; +#[doc(hidden)] +pub type B = NcursesBackend; impl Cursive { /// Creates a new Cursive root, and initialize ncurses. @@ -163,6 +166,7 @@ impl Cursive { running: true, cb_source: rx, cb_sink: tx, + backend: NcursesBackend, }; res.screens.push(views::StackView::new()); @@ -441,7 +445,7 @@ impl Cursive { fn draw(&mut self) { // TODO: don't clone the theme // Reference it or something - let printer = Printer::new(self.screen_size(), self.theme.clone()); + let printer = Printer::new(self.screen_size(), self.theme.clone(), &mut self.backend); // Draw the currently active screen // If the menubar is active, nothing else can be. @@ -462,7 +466,8 @@ impl Cursive { let printer = printer.sub_printer(Vec2::new(0, offset), printer.size, !selected); - self.screen_mut().draw(&printer); + let id = self.active_screen; + self.screens.get_mut(id).unwrap().draw(&printer); B::refresh(); } diff --git a/src/printer.rs b/src/printer.rs index ee8dbac..31f0fa8 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -12,8 +12,7 @@ use theme::{BorderStyle, ColorStyle, Effect, Theme}; use vec::Vec2; /// Convenient interface to draw on a subset of the screen. -#[derive(Clone)] -pub struct Printer { +pub struct Printer<'a> { /// Offset into the window this printer should start drawing at. pub offset: Vec2, /// Size of the area we are allowed to draw on. @@ -22,16 +21,22 @@ pub struct Printer { pub focused: bool, /// Currently used theme pub theme: Theme, + + backend: &'a B, } -impl Printer { +impl <'a> Printer<'a> { /// Creates a new printer on the given window. - pub fn new>(size: T, theme: Theme) -> Self { + /// + /// But nobody needs to know that. + #[doc(hidden)] + pub fn new>(size: T, theme: Theme, backend: &'a B) -> Self { Printer { offset: Vec2::zero(), size: size.into(), focused: true, theme: theme, + backend: backend, } } @@ -90,7 +95,9 @@ impl Printer { /// ```no_run /// # use cursive::Printer; /// # use cursive::theme; - /// # let printer = Printer::new((6,4), theme::load_default()); + /// # use cursive::B; + /// # let b = B{}; + /// # let printer = Printer::new((6,4), theme::load_default(), &b); /// printer.with_color(theme::ColorStyle::Highlight, |printer| { /// printer.print((0,0), "This text is highlighted!"); /// }); @@ -121,7 +128,9 @@ impl Printer { /// ```no_run /// # use cursive::Printer; /// # use cursive::theme; - /// # let printer = Printer::new((6,4), theme::load_default()); + /// # use cursive::B; + /// # let b = B{}; + /// # let printer = Printer::new((6,4), theme::load_default(), &b); /// printer.print_box((0,0), (6,4), false); /// ``` pub fn print_box, S: Into>(&self, start: T, size: S, @@ -213,9 +222,9 @@ impl Printer { } /// Returns a printer on a subset of this one's area. - pub fn sub_printer, T: Into>(&self, offset: S, + pub fn sub_printer, T: Into>(&'a self, offset: S, size: T, focused: bool) - -> Printer { + -> Printer<'a> { let size = size.into(); let offset = offset.into().or_min(self.size); Printer { @@ -224,6 +233,7 @@ impl Printer { size: Vec2::min(self.size - offset, size), focused: self.focused && focused, theme: self.theme.clone(), + backend: self.backend, } } diff --git a/src/view/scroll.rs b/src/view/scroll.rs index bb8f1dd..b9abada 100644 --- a/src/view/scroll.rs +++ b/src/view/scroll.rs @@ -134,8 +134,10 @@ impl ScrollBase { /// # use cursive::view::ScrollBase; /// # use cursive::Printer; /// # use cursive::theme; + /// # use cursive::B; /// # let scrollbase = ScrollBase::new(); - /// # let printer = Printer::new((5,1), theme::load_default()); + /// # let b = B{}; + /// # let printer = Printer::new((5,1), theme::load_default(), &b); /// # let printer = &printer; /// let lines = ["Line 1", "Line number 2"]; /// scrollbase.draw(printer, |printer, i| {