pub struct Printer<'a, 'b> {
pub offset: XY<usize>,
pub output_size: XY<usize>,
pub size: XY<usize>,
pub content_offset: XY<usize>,
pub focused: bool,
pub enabled: bool,
pub theme: &'a Theme,
/* private fields */
}
Expand description
Convenient interface to draw on a subset of the screen.
The printing area is defined by offset
and size
.
The content that will be printed is defined by Self::content_offset
and Self::size
.
If the printer is asked to print outside of the printing area,
then the string to be printed shall be truncated without throwing errors.
Refer to the crate::traits::View
to understand how to change its size.
Fields§
§offset: XY<usize>
Offset into the window this printer should start drawing at.
A print request at x
will really print at x + offset
.
output_size: XY<usize>
Size of the area we are allowed to draw on.
Anything outside of this should be discarded.
The view being drawn can ignore this, but anything further than that
will be ignored.
size: XY<usize>
Size allocated to the view.
This should be the same value as the one given in the last call to
View::layout
.
content_offset: XY<usize>
Offset into the view for this printer.
The view being drawn can ignore this, but anything to the top-left of this will actually be ignored, so it can be used to skip this part.
A print request x
, will really print at x - content_offset
.
focused: bool
Whether the view to draw is currently focused or not.
enabled: bool
Whether the view to draw is currently enabled or not.
theme: &'a Theme
Currently used theme
Implementations§
Source§impl<'a, 'b> Printer<'a, 'b>
impl<'a, 'b> Printer<'a, 'b>
Sourcepub fn output_window(&self) -> Rect
pub fn output_window(&self) -> Rect
Returns the region of the window where this printer can write.
Sourcepub fn buffer_size(&self) -> XY<usize>
pub fn buffer_size(&self) -> XY<usize>
Returns the size of the entire buffer.
This is the size of the entire terminal, not just the area this printer can write into.
Sourcepub fn clear(&self)
pub fn clear(&self)
Clear the screen.
It will discard anything drawn before.
Users rarely need to call this directly.
Sourcepub fn print_styled<V, S>(&self, start: V, text: S)
pub fn print_styled<V, S>(&self, start: V, text: S)
Prints some styled text at the given position.
Sourcepub fn print<S>(&self, start: S, text: &str)
pub fn print<S>(&self, start: S, text: &str)
Prints some text at the given position.
§Parameters
start
is the offset used to print the text in the view.text
is a string to print on the screen. It must be a single line, no line wrapping will be done.
§Description
Prints some text at the given position. The text could be truncated if it exceed the drawing area size.
§Example
use cursive::{Printer, Vec2, View, XY};
pub struct CustomView {
word: String,
}
impl CustomView {
pub fn new() -> Self {
Self {
word: String::from("Eh, tu connais Rust?"),
}
}
}
impl View for CustomView {
fn draw(&self, printer: &Printer<'_, '_>) {
printer.print(XY::new(0, 0), &self.word);
}
}
Sourcepub fn print_vline<T>(&self, start: T, height: usize, c: &str)
pub fn print_vline<T>(&self, start: T, height: usize, c: &str)
Prints a vertical line using the given character.
Sourcepub fn on_window<F, R>(&self, f: F) -> R
pub fn on_window<F, R>(&self, f: F) -> R
Calls a closure on the output window for this printer.
Sourcepub fn print_line<T>(
&self,
orientation: Orientation,
start: T,
length: usize,
c: &str,
)
pub fn print_line<T>( &self, orientation: Orientation, start: T, length: usize, c: &str, )
Prints a line using the given character.
Sourcepub fn print_rect(&self, rect: Rect, c: &str)
pub fn print_rect(&self, rect: Rect, c: &str)
Fills a rectangle using the given character.
Sourcepub fn print_hline<T>(&self, start: T, width: usize, c: &str)
pub fn print_hline<T>(&self, start: T, width: usize, c: &str)
Prints a horizontal line using the given character.
Sourcepub fn current_color(&self) -> ColorPair
pub fn current_color(&self) -> ColorPair
Returns the color currently used by the printer.
Sourcepub fn current_style(&self) -> ConcreteStyle
pub fn current_style(&self) -> ConcreteStyle
Returns the style currently used by the printer.
Sourcepub fn set_color(&mut self, color: ColorStyle)
pub fn set_color(&mut self, color: ColorStyle)
Sets the color used by this printer.
Sourcepub fn unset_effect(&mut self, effect: Effect)
pub fn unset_effect(&mut self, effect: Effect)
Deactivate the given effect for this printer.
Sourcepub fn set_effect(&mut self, effect: Effect)
pub fn set_effect(&mut self, effect: Effect)
Active the given effect for this printer.
Sourcepub fn with_color<F>(&self, c: ColorStyle, f: F)
pub fn with_color<F>(&self, c: ColorStyle, f: F)
Call the given closure with a colored printer, that will apply the given color on prints.
Does not change the current set of active effects (bold/underline/…).
Sourcepub fn with_style<F, T>(&self, style: T, f: F)
pub fn with_style<F, T>(&self, style: T, f: F)
Call the given closure with a styled printer, that will apply the given style on prints.
§Examples
printer.with_style(style::PaletteStyle::Highlight, |printer| {
printer.print((0, 0), "This text is highlighted!");
});
Sourcepub fn with_effect<F>(&self, effect: Effect, f: F)
pub fn with_effect<F>(&self, effect: Effect, f: F)
Call the given closure with a modified printer that will apply the given effect on prints.
Sourcepub fn with_theme<F>(&self, theme: &Theme, f: F)
pub fn with_theme<F>(&self, theme: &Theme, f: F)
Call the given closure with a modified printer that will apply the given theme on prints.
Sourcepub fn theme<'c>(&self, theme: &'c Theme) -> Printer<'c, 'b>where
'a: 'c,
pub fn theme<'c>(&self, theme: &'c Theme) -> Printer<'c, 'b>where
'a: 'c,
Create a new sub-printer with the given theme.
Sourcepub fn with_effects<F>(&self, effects: EnumSet<Effect>, f: F)
pub fn with_effects<F>(&self, effects: EnumSet<Effect>, f: F)
Call the given closure with a modified printer that will apply each given effect on prints.
Note that this does not unset any active effect.
Sourcepub fn print_box<T, S>(&self, start: T, size: S, invert: bool)
pub fn print_box<T, S>(&self, start: T, size: S, invert: bool)
Prints a rectangular box.
If invert
is true
, and the theme uses Outset
borders, then the
box will use an “inset” style instead.
§Examples
printer.print_box((0, 0), (6, 4), false);
Sourcepub fn with_high_border<F>(&self, invert: bool, f: F)
pub fn with_high_border<F>(&self, invert: bool, f: F)
Runs the given function using a color depending on the theme.
- If the theme’s borders is
None
, return without callingf
. - If the theme’s borders is “outset” and
invert
isfalse
, usePaletteStyle::Tertiary
. - Otherwise, use
PaletteStyle::Primary
.
Sourcepub fn with_low_border<F>(&self, invert: bool, f: F)
pub fn with_low_border<F>(&self, invert: bool, f: F)
Runs the given function using a color depending on the theme.
- If the theme’s borders is
None
, return without callingf
. - If the theme’s borders is “outset” and
invert
istrue
, usePaletteStyle::Tertiary
. - Otherwise, use
PaletteStyle::Primary
.
Sourcepub fn with_selection<F>(&self, selection: bool, f: F)
pub fn with_selection<F>(&self, selection: bool, f: F)
Apply a selection style and call the given function.
- If
selection
isfalse
, simply uses the current style. - If
selection
istrue
:- If the printer currently has the focus, uses
PaletteStyle::Highlight
. - Otherwise, uses
PaletteStyle::HighlightInactive
.
- If the printer currently has the focus, uses
Sourcepub fn print_hdelim<T>(&self, start: T, len: usize)
pub fn print_hdelim<T>(&self, start: T, len: usize)
Prints a horizontal delimiter with side border ├
and ┤
.
Sourcepub fn offset<S>(&self, offset: S) -> Printer<'a, 'b>
pub fn offset<S>(&self, offset: S) -> Printer<'a, 'b>
Returns a sub-printer with the given offset.
It will print in an area slightly to the bottom/right.
Sourcepub fn focused(&self, focused: bool) -> Printer<'a, 'b>
pub fn focused(&self, focused: bool) -> Printer<'a, 'b>
Returns a new sub-printer inheriting the given focus.
If self
is focused and focused == true
, the child will be focused.
Otherwise, he will be unfocused.
Sourcepub fn enabled(&self, enabled: bool) -> Printer<'a, 'b>
pub fn enabled(&self, enabled: bool) -> Printer<'a, 'b>
Returns a new sub-printer inheriting the given enabled state.
If self
is enabled and enabled == true
, the child will be enabled.
Otherwise, he will be disabled.
Sourcepub fn windowed(&self, viewport: Rect) -> Printer<'a, 'b>
pub fn windowed(&self, viewport: Rect) -> Printer<'a, 'b>
Returns a new sub-printer for the given viewport.
This is a combination of offset + cropped.
Sourcepub fn cropped<S>(&self, size: S) -> Printer<'a, 'b>
pub fn cropped<S>(&self, size: S) -> Printer<'a, 'b>
Returns a new sub-printer with a cropped area.
The new printer size will be the minimum of size
and its current size.
Any size reduction happens at the bottom-right.
Sourcepub fn cropped_centered<S>(&self, size: S) -> Printer<'a, 'b>
pub fn cropped_centered<S>(&self, size: S) -> Printer<'a, 'b>
Returns a new sub-printer with a cropped area.
The new printer size will be the minimum of size
and its current size.
The view will stay centered.
Note that if shrinking by an odd number, the view will round to the top-left.
Sourcepub fn shrinked<S>(&self, borders: S) -> Printer<'a, 'b>
pub fn shrinked<S>(&self, borders: S) -> Printer<'a, 'b>
Returns a new sub-printer with a shrinked area.
The printer size will be reduced by the given border from the bottom-right.
Sourcepub fn shrinked_centered<S>(&self, borders: S) -> Printer<'a, 'b>
pub fn shrinked_centered<S>(&self, borders: S) -> Printer<'a, 'b>
Returns a new sub-printer with a shrinked area.
The printer size will be reduced by the given border, and will stay centered.
Note that if shrinking by an odd number, the view will round to the top-left.
Sourcepub fn content_offset<S>(&self, offset: S) -> Printer<'a, 'b>
pub fn content_offset<S>(&self, offset: S) -> Printer<'a, 'b>
Returns a new sub-printer with a content offset.
This is useful for parent views that only show a subset of their
child, like crate::views::ScrollView
.
Sourcepub fn inner_size<S>(&self, size: S) -> Printer<'a, 'b>
pub fn inner_size<S>(&self, size: S) -> Printer<'a, 'b>
Returns a sub-printer with a different inner size.
This will not change the actual output size, but will appear bigger
(or smaller) to users of this printer.
Useful to give to children who think they’re big, but really aren’t.