2018-04-17 23:18:59 +00:00
|
|
|
//! Provide higher-level abstraction to draw things on backends.
|
2015-05-15 19:16:58 +00:00
|
|
|
|
2018-04-01 22:35:37 +00:00
|
|
|
use backend::Backend;
|
2018-06-15 23:41:20 +00:00
|
|
|
use direction::Orientation;
|
2018-01-08 14:44:27 +00:00
|
|
|
use enumset::EnumSet;
|
2016-10-02 22:22:29 +00:00
|
|
|
use std::cmp::min;
|
2018-01-22 19:55:56 +00:00
|
|
|
use theme::{BorderStyle, ColorStyle, Effect, PaletteColor, Style, Theme};
|
2016-10-02 22:22:29 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2018-04-17 23:18:59 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
2018-04-18 00:22:56 +00:00
|
|
|
use utils::lines::simple::{prefix, suffix};
|
2016-07-12 02:24:00 +00:00
|
|
|
use vec::Vec2;
|
2018-04-03 23:38:54 +00:00
|
|
|
use with::With;
|
2015-05-15 18:58:47 +00:00
|
|
|
|
2015-05-23 22:58:06 +00:00
|
|
|
/// Convenient interface to draw on a subset of the screen.
|
2018-04-17 23:18:59 +00:00
|
|
|
///
|
|
|
|
/// The area it can print on is defined by `offset` and `size`.
|
|
|
|
///
|
2018-04-25 00:19:07 +00:00
|
|
|
/// The part of the content it will print is defined by `content_offset`
|
2018-04-17 23:18:59 +00:00
|
|
|
/// and `size`.
|
2018-05-21 13:53:38 +00:00
|
|
|
pub struct Printer<'a, 'b> {
|
2015-05-15 18:58:47 +00:00
|
|
|
/// Offset into the window this printer should start drawing at.
|
2018-04-17 23:18:59 +00:00
|
|
|
///
|
2018-04-25 00:19:07 +00:00
|
|
|
/// A print request at `x` will really print at `x + offset`.
|
2015-05-15 18:58:47 +00:00
|
|
|
pub offset: Vec2,
|
2018-04-05 00:48:51 +00:00
|
|
|
|
2015-05-15 18:58:47 +00:00
|
|
|
/// Size of the area we are allowed to draw on.
|
2018-04-05 00:48:51 +00:00
|
|
|
///
|
|
|
|
/// Anything outside of this should be discarded.
|
2018-04-27 23:23:07 +00:00
|
|
|
pub output_size: Vec2,
|
|
|
|
|
|
|
|
/// Size allocated to the view.
|
|
|
|
///
|
|
|
|
/// This should be the same value as the one given in the last call to
|
|
|
|
/// `View::layout`.
|
2015-05-15 18:58:47 +00:00
|
|
|
pub size: Vec2,
|
2018-04-05 00:48:51 +00:00
|
|
|
|
2018-04-17 23:18:59 +00:00
|
|
|
/// Offset into the view for this printer.
|
|
|
|
///
|
2018-04-25 00:19:07 +00:00
|
|
|
/// A print request `x`, will really print at `x - content_offset`.
|
2018-04-17 23:18:59 +00:00
|
|
|
pub content_offset: Vec2,
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
/// Whether the view to draw is currently focused or not.
|
|
|
|
pub focused: bool,
|
2018-04-05 00:48:51 +00:00
|
|
|
|
2015-06-06 01:08:05 +00:00
|
|
|
/// Currently used theme
|
2017-06-12 18:59:33 +00:00
|
|
|
pub theme: &'a Theme,
|
2016-09-23 05:00:58 +00:00
|
|
|
|
2016-10-09 22:47:06 +00:00
|
|
|
/// Backend used to actually draw things
|
2018-05-21 13:53:38 +00:00
|
|
|
backend: &'b Backend,
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 06:21:10 +00:00
|
|
|
impl<'a, 'b> Clone for Printer<'a, 'b> {
|
2018-04-03 23:38:54 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Printer {
|
|
|
|
offset: self.offset,
|
|
|
|
content_offset: self.content_offset,
|
2018-04-27 23:23:07 +00:00
|
|
|
output_size: self.output_size,
|
2018-04-03 23:38:54 +00:00
|
|
|
size: self.size,
|
|
|
|
focused: self.focused,
|
|
|
|
theme: self.theme,
|
|
|
|
backend: self.backend,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 13:53:38 +00:00
|
|
|
impl<'a, 'b> Printer<'a, 'b> {
|
2015-05-22 06:29:49 +00:00
|
|
|
/// Creates a new printer on the given window.
|
2016-09-23 05:00:58 +00:00
|
|
|
///
|
|
|
|
/// But nobody needs to know that.
|
|
|
|
#[doc(hidden)]
|
2017-10-12 23:38:55 +00:00
|
|
|
pub fn new<T: Into<Vec2>>(
|
2018-06-11 06:29:10 +00:00
|
|
|
size: T, theme: &'a Theme, backend: &'b Backend,
|
2017-10-12 23:38:55 +00:00
|
|
|
) -> Self {
|
2018-04-27 23:23:07 +00:00
|
|
|
let size = size.into();
|
2015-05-22 06:29:49 +00:00
|
|
|
Printer {
|
|
|
|
offset: Vec2::zero(),
|
2018-04-05 00:48:51 +00:00
|
|
|
content_offset: Vec2::zero(),
|
2018-04-27 23:23:07 +00:00
|
|
|
output_size: size,
|
|
|
|
size,
|
2015-05-31 04:05:34 +00:00
|
|
|
focused: true,
|
2018-04-10 18:53:25 +00:00
|
|
|
theme,
|
|
|
|
backend,
|
2015-05-22 06:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:47:06 +00:00
|
|
|
/// Clear the screen.
|
|
|
|
///
|
2016-10-15 01:18:19 +00:00
|
|
|
/// It will discard anything drawn before.
|
|
|
|
///
|
|
|
|
/// Users rarely need to call this directly.
|
2016-10-09 22:47:06 +00:00
|
|
|
pub fn clear(&self) {
|
2018-01-22 19:55:56 +00:00
|
|
|
self.backend
|
|
|
|
.clear(self.theme.palette[PaletteColor::Background]);
|
2016-10-09 22:47:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 05:10:59 +00:00
|
|
|
// TODO: use &mut self? We don't *need* it, but it may make sense.
|
|
|
|
// We don't want people to start calling prints in parallel?
|
2015-05-15 18:58:47 +00:00
|
|
|
/// Prints some text at the given position relative to the window.
|
2018-04-25 00:19:07 +00:00
|
|
|
pub fn print<S: Into<Vec2>>(&self, start: S, text: &str) {
|
2018-05-22 00:25:32 +00:00
|
|
|
// Where we are asked to start printing. Oh boy.
|
2018-04-25 00:19:07 +00:00
|
|
|
let start = start.into();
|
2018-04-18 00:22:56 +00:00
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
// We accept requests between `content_offset` and
|
2018-05-22 00:25:32 +00:00
|
|
|
// `content_offset + output_size`.
|
|
|
|
if !(start < (self.output_size + self.content_offset)) {
|
2018-04-18 00:22:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
// If start < content_offset, part of the text will not be visible.
|
|
|
|
// This is the part of the text that's hidden:
|
|
|
|
// (It should always be smaller than the content offset)
|
|
|
|
let hidden_part = self.content_offset.saturating_sub(start);
|
2018-04-18 00:22:56 +00:00
|
|
|
if hidden_part.y > 0 {
|
|
|
|
// Since we are printing a single line, there's nothing we can do.
|
2016-03-15 22:37:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
let text_width = text.width();
|
|
|
|
|
2018-04-27 23:23:07 +00:00
|
|
|
// If we're waaaay too far left, just give up.
|
|
|
|
if hidden_part.x > text_width {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-18 00:22:56 +00:00
|
|
|
// We have to drop hidden_part.x width from the start of the string.
|
|
|
|
// prefix() may be too short if there's a double-width character.
|
|
|
|
// So instead, keep the suffix and drop the prefix.
|
|
|
|
|
|
|
|
// TODO: use a different prefix method that is *at least* the width
|
|
|
|
// (and not *at most*)
|
2018-05-18 00:37:39 +00:00
|
|
|
let tail =
|
|
|
|
suffix(text.graphemes(true), text_width - hidden_part.x, "");
|
2018-04-18 00:22:56 +00:00
|
|
|
let skipped_len = text.len() - tail.length;
|
|
|
|
let skipped_width = text_width - tail.width;
|
2018-04-25 00:19:07 +00:00
|
|
|
assert_eq!(text[..skipped_len].width(), skipped_width);
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
// This should be equal most of the time, except when there's a double
|
|
|
|
// character preventing us from splitting perfectly.
|
|
|
|
assert!(skipped_width >= hidden_part.x);
|
|
|
|
|
|
|
|
// Drop part of the text, and move the cursor correspondingly.
|
|
|
|
let text = &text[skipped_len..];
|
2018-04-25 00:19:07 +00:00
|
|
|
let start = start + (skipped_width, 0);
|
|
|
|
assert!(start.fits(self.content_offset));
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
// What we did before should guarantee that this won't overflow.
|
2018-04-25 00:19:07 +00:00
|
|
|
let start = start - self.content_offset;
|
2018-04-18 00:22:56 +00:00
|
|
|
|
2015-05-24 08:49:50 +00:00
|
|
|
// Do we have enough room for the entire line?
|
2018-04-27 23:23:07 +00:00
|
|
|
let room = self.output_size.x - start.x;
|
2018-04-17 23:18:59 +00:00
|
|
|
|
|
|
|
// Drop the end of the text if it's too long
|
2015-05-24 08:49:50 +00:00
|
|
|
// We want the number of CHARACTERS, not bytes.
|
2016-06-28 05:10:59 +00:00
|
|
|
// (Actually we want the "width" of the string, see unicode-width)
|
2017-02-01 19:22:36 +00:00
|
|
|
let prefix_len = prefix(text.graphemes(true), room, "").length;
|
2016-07-26 06:54:33 +00:00
|
|
|
let text = &text[..prefix_len];
|
2018-04-25 00:19:07 +00:00
|
|
|
assert!(text.width() <= room);
|
2015-05-29 00:37:00 +00:00
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
let start = start + self.offset;
|
|
|
|
self.backend.print_at(start, text);
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 17:31:38 +00:00
|
|
|
/// Prints a vertical line using the given character.
|
2018-04-27 23:23:07 +00:00
|
|
|
pub fn print_vline<T: Into<Vec2>>(
|
|
|
|
&self, start: T, height: usize, c: &str,
|
|
|
|
) {
|
2018-04-17 23:18:59 +00:00
|
|
|
let start = start.into();
|
2018-04-18 00:22:56 +00:00
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
// Here again, we can abort if we're trying to print too far right or
|
|
|
|
// too low.
|
2018-07-25 02:06:07 +00:00
|
|
|
if !(start < (self.output_size + self.content_offset)) {
|
2018-04-18 00:22:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
// hidden_part describes how far to the top left of the viewport we are.
|
2018-04-18 00:22:56 +00:00
|
|
|
let hidden_part = self.content_offset.saturating_sub(start);
|
2018-04-25 00:19:07 +00:00
|
|
|
if hidden_part.x > 0 || hidden_part.y >= height {
|
2018-04-18 00:22:56 +00:00
|
|
|
// We're printing a single column, so we can't do much here.
|
2016-03-15 22:37:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
// Skip `hidden_part`
|
|
|
|
let start = start + hidden_part;
|
2018-04-25 00:19:07 +00:00
|
|
|
assert!(start.fits(self.content_offset));
|
|
|
|
|
|
|
|
let height = height - hidden_part.y;
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
// What we did before ensures this won't overflow.
|
|
|
|
let start = start - self.content_offset;
|
|
|
|
|
|
|
|
// Don't go overboard
|
2018-04-27 23:23:07 +00:00
|
|
|
let height = min(height, self.output_size.y - start.y);
|
2015-05-24 08:49:50 +00:00
|
|
|
|
2018-04-17 23:18:59 +00:00
|
|
|
let start = start + self.offset;
|
2018-04-25 00:19:07 +00:00
|
|
|
for y in 0..height {
|
2018-04-27 23:23:07 +00:00
|
|
|
self.backend.print_at(start + (0, y), c);
|
2016-06-25 22:52:19 +00:00
|
|
|
}
|
2015-05-20 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 23:41:20 +00:00
|
|
|
/// Prints a line using the given character.
|
|
|
|
pub fn print_line<T: Into<Vec2>>(
|
|
|
|
&self, orientation: Orientation, start: T, length: usize, c: &str,
|
|
|
|
) {
|
|
|
|
match orientation {
|
|
|
|
Orientation::Vertical => self.print_vline(start, length, c),
|
|
|
|
Orientation::Horizontal => self.print_hline(start, length, c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:31:38 +00:00
|
|
|
/// Prints a horizontal line using the given character.
|
2018-04-25 00:19:07 +00:00
|
|
|
pub fn print_hline<T: Into<Vec2>>(&self, start: T, width: usize, c: &str) {
|
2018-04-17 23:18:59 +00:00
|
|
|
let start = start.into();
|
2018-04-18 00:22:56 +00:00
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
// Nothing to be done if the start if too far to the bottom/right
|
2018-07-25 02:06:07 +00:00
|
|
|
if !(start < (self.output_size + self.content_offset)) {
|
2016-03-15 22:37:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
let hidden_part = self.content_offset.saturating_sub(start);
|
2018-04-25 00:19:07 +00:00
|
|
|
if hidden_part.y > 0 || hidden_part.x >= width {
|
2018-04-18 00:22:56 +00:00
|
|
|
// We're printing a single line, so we can't do much here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip `hidden_part`
|
|
|
|
let start = start + hidden_part;
|
2018-04-25 00:19:07 +00:00
|
|
|
assert!(start.fits(self.content_offset));
|
2018-04-18 00:22:56 +00:00
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
let width = width - hidden_part.x;
|
|
|
|
|
|
|
|
// Don't go too far
|
2018-04-18 00:22:56 +00:00
|
|
|
let start = start - self.content_offset;
|
|
|
|
|
|
|
|
// Don't write too much if we're close to the end
|
2018-05-18 00:37:39 +00:00
|
|
|
let width = min(width, (self.output_size.x - start.x) / c.width());
|
2018-04-18 00:22:56 +00:00
|
|
|
|
|
|
|
// Could we avoid allocating?
|
2018-04-25 00:19:07 +00:00
|
|
|
let text: String = ::std::iter::repeat(c).take(width).collect();
|
2015-05-24 08:49:50 +00:00
|
|
|
|
2018-04-17 23:18:59 +00:00
|
|
|
let start = start + self.offset;
|
|
|
|
self.backend.print_at(start, &text);
|
2015-05-22 06:29:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 07:09:22 +00:00
|
|
|
/// Call the given closure with a colored printer,
|
|
|
|
/// that will apply the given color on prints.
|
2015-05-26 23:48:27 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2016-07-14 06:25:54 +00:00
|
|
|
/// # use cursive::Printer;
|
2015-06-06 01:08:05 +00:00
|
|
|
/// # use cursive::theme;
|
2018-04-01 23:39:03 +00:00
|
|
|
/// # use cursive::backend;
|
|
|
|
/// # let b = backend::dummy::Backend::init();
|
2017-06-13 01:03:52 +00:00
|
|
|
/// # let t = theme::load_default();
|
2018-04-10 19:50:40 +00:00
|
|
|
/// # let printer = Printer::new((6,4), &t, &*b);
|
2018-01-17 17:35:57 +00:00
|
|
|
/// printer.with_color(theme::ColorStyle::highlight(), |printer| {
|
2015-05-26 23:48:27 +00:00
|
|
|
/// printer.print((0,0), "This text is highlighted!");
|
|
|
|
/// });
|
|
|
|
/// ```
|
2016-07-01 06:38:01 +00:00
|
|
|
pub fn with_color<F>(&self, c: ColorStyle, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
2015-05-23 22:58:06 +00:00
|
|
|
{
|
2018-05-18 00:37:39 +00:00
|
|
|
let old = self.backend.set_color(c.resolve(&self.theme.palette));
|
2018-04-01 22:35:37 +00:00
|
|
|
f(self);
|
|
|
|
self.backend.set_color(old);
|
2015-05-20 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 14:44:27 +00:00
|
|
|
/// Call the given closure with a styled printer,
|
|
|
|
/// that will apply the given style on prints.
|
2018-01-13 18:36:56 +00:00
|
|
|
pub fn with_style<F, T>(&self, style: T, f: F)
|
2018-01-08 14:44:27 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
2018-01-13 18:36:56 +00:00
|
|
|
T: Into<Style>,
|
2018-01-08 14:44:27 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
let style = style.into();
|
|
|
|
|
2018-01-08 14:44:27 +00:00
|
|
|
let color = style.color;
|
|
|
|
let effects = style.effects;
|
|
|
|
|
|
|
|
if let Some(color) = color {
|
|
|
|
self.with_color(color, |printer| {
|
|
|
|
printer.with_effects(effects, f);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.with_effects(effects, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Call the given closure with a modified printer
|
|
|
|
/// that will apply the given effect on prints.
|
2016-06-30 00:36:20 +00:00
|
|
|
pub fn with_effect<F>(&self, effect: Effect, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
2015-05-27 04:45:00 +00:00
|
|
|
{
|
2018-04-01 22:35:37 +00:00
|
|
|
self.backend.set_effect(effect);
|
|
|
|
f(self);
|
|
|
|
self.backend.unset_effect(effect);
|
2015-05-27 04:45:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 13:53:38 +00:00
|
|
|
/// Call the given closure with a modified printer
|
|
|
|
/// that will apply the given theme on prints.
|
|
|
|
pub fn with_theme<F>(&self, theme: &Theme, f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
|
|
|
{
|
|
|
|
let new_printer = Printer {
|
2018-06-11 06:29:10 +00:00
|
|
|
offset: self.offset,
|
|
|
|
size: self.size,
|
|
|
|
focused: self.focused,
|
2018-06-21 21:44:30 +00:00
|
|
|
theme,
|
2018-06-11 06:29:10 +00:00
|
|
|
backend: self.backend,
|
2018-06-16 06:21:10 +00:00
|
|
|
output_size: self.output_size,
|
|
|
|
content_offset: self.content_offset,
|
2018-05-21 13:53:38 +00:00
|
|
|
};
|
|
|
|
f(&new_printer);
|
|
|
|
}
|
|
|
|
|
2018-01-08 14:44:27 +00:00
|
|
|
/// Call the given closure with a modified printer
|
|
|
|
/// that will apply each given effect on prints.
|
|
|
|
pub fn with_effects<F>(&self, effects: EnumSet<Effect>, f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
|
|
|
{
|
|
|
|
match effects.iter().next() {
|
|
|
|
None => f(self),
|
|
|
|
Some(effect) => {
|
|
|
|
let mut effects = effects;
|
|
|
|
effects.remove(effect);
|
2018-01-10 22:58:29 +00:00
|
|
|
|
|
|
|
self.with_effect(effect, |s| s.with_effects(effects, f));
|
2018-01-08 14:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:36:35 +00:00
|
|
|
/// Prints a rectangular box.
|
|
|
|
///
|
2016-08-05 00:38:26 +00:00
|
|
|
/// If `invert` is `true`, and the theme uses `Outset` borders, then the
|
|
|
|
/// box will use an "inset" style instead.
|
|
|
|
///
|
2015-05-20 17:36:35 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2016-07-14 06:25:54 +00:00
|
|
|
/// # use cursive::Printer;
|
2015-06-06 01:08:05 +00:00
|
|
|
/// # use cursive::theme;
|
2018-04-01 23:39:03 +00:00
|
|
|
/// # use cursive::backend;
|
|
|
|
/// # let b = backend::dummy::Backend::init();
|
2017-06-13 01:03:52 +00:00
|
|
|
/// # let t = theme::load_default();
|
2018-04-10 19:50:40 +00:00
|
|
|
/// # let printer = Printer::new((6,4), &t, &*b);
|
2016-08-05 00:38:26 +00:00
|
|
|
/// printer.print_box((0,0), (6,4), false);
|
2015-05-20 17:36:35 +00:00
|
|
|
/// ```
|
2017-10-12 23:38:55 +00:00
|
|
|
pub fn print_box<T: Into<Vec2>, S: Into<Vec2>>(
|
2018-04-27 23:23:07 +00:00
|
|
|
&self, start: T, size: S, invert: bool,
|
2017-10-12 23:38:55 +00:00
|
|
|
) {
|
2016-07-17 08:20:41 +00:00
|
|
|
let start = start.into();
|
|
|
|
let size = size.into();
|
2017-08-14 23:32:01 +00:00
|
|
|
|
2016-07-17 08:20:41 +00:00
|
|
|
if size.x < 2 || size.y < 2 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let size = size - (1, 1);
|
|
|
|
|
2016-08-05 17:49:16 +00:00
|
|
|
self.with_high_border(invert, |s| {
|
|
|
|
s.print(start, "┌");
|
|
|
|
s.print(start + size.keep_y(), "└");
|
|
|
|
s.print_hline(start + (1, 0), size.x - 1, "─");
|
|
|
|
s.print_vline(start + (0, 1), size.y - 1, "│");
|
|
|
|
});
|
2016-07-17 08:20:41 +00:00
|
|
|
|
2016-08-05 17:49:16 +00:00
|
|
|
self.with_low_border(invert, |s| {
|
2016-08-05 00:27:04 +00:00
|
|
|
s.print(start + size.keep_x(), "┐");
|
|
|
|
s.print(start + size, "┘");
|
2018-05-18 00:37:39 +00:00
|
|
|
s.print_hline(start + (1, 0) + size.keep_y(), size.x - 1, "─");
|
|
|
|
s.print_vline(start + (0, 1) + size.keep_x(), size.y - 1, "│");
|
2016-08-05 00:27:04 +00:00
|
|
|
});
|
2016-06-28 05:10:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 17:49:16 +00:00
|
|
|
/// Runs the given function using a color depending on the theme.
|
|
|
|
///
|
|
|
|
/// * If the theme's borders is `None`, return without calling `f`.
|
|
|
|
/// * If the theme's borders is "outset" and `invert` is `false`,
|
|
|
|
/// use `ColorStyle::Tertiary`.
|
|
|
|
/// * Otherwise, use `ColorStyle::Primary`.
|
|
|
|
pub fn with_high_border<F>(&self, invert: bool, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
2016-08-05 17:49:16 +00:00
|
|
|
{
|
|
|
|
let color = match self.theme.borders {
|
2017-06-12 18:59:33 +00:00
|
|
|
BorderStyle::None => return,
|
2018-01-17 17:35:57 +00:00
|
|
|
BorderStyle::Outset if !invert => ColorStyle::tertiary(),
|
|
|
|
_ => ColorStyle::primary(),
|
2016-08-05 17:49:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.with_color(color, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the given function using a color depending on the theme.
|
|
|
|
///
|
|
|
|
/// * If the theme's borders is `None`, return without calling `f`.
|
|
|
|
/// * If the theme's borders is "outset" and `invert` is `true`,
|
2018-01-17 17:35:57 +00:00
|
|
|
/// use `ColorStyle::tertiary()`.
|
|
|
|
/// * Otherwise, use `ColorStyle::primary()`.
|
2016-08-05 17:49:16 +00:00
|
|
|
pub fn with_low_border<F>(&self, invert: bool, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&Printer),
|
2016-08-05 17:49:16 +00:00
|
|
|
{
|
|
|
|
let color = match self.theme.borders {
|
2017-06-12 18:59:33 +00:00
|
|
|
BorderStyle::None => return,
|
2018-01-17 17:35:57 +00:00
|
|
|
BorderStyle::Outset if invert => ColorStyle::tertiary(),
|
|
|
|
_ => ColorStyle::primary(),
|
2016-08-05 17:49:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.with_color(color, f);
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Apply a selection style and call the given function.
|
|
|
|
///
|
2018-01-17 17:35:57 +00:00
|
|
|
/// * If `selection` is `false`, simply uses `ColorStyle::primary()`.
|
2016-07-12 03:26:33 +00:00
|
|
|
/// * If `selection` is `true`:
|
|
|
|
/// * If the printer currently has the focus,
|
2018-01-17 17:35:57 +00:00
|
|
|
/// uses `ColorStyle::highlight()`.
|
|
|
|
/// * Otherwise, uses `ColorStyle::highlight_inactive()`.
|
2016-07-02 08:01:09 +00:00
|
|
|
pub fn with_selection<F: FnOnce(&Printer)>(&self, selection: bool, f: F) {
|
2017-10-12 23:38:55 +00:00
|
|
|
self.with_color(
|
|
|
|
if selection {
|
|
|
|
if self.focused {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::highlight()
|
2017-10-12 23:38:55 +00:00
|
|
|
} else {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::highlight_inactive()
|
2017-10-12 23:38:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::primary()
|
2017-10-12 23:38:55 +00:00
|
|
|
},
|
|
|
|
f,
|
|
|
|
);
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Prints a horizontal delimiter with side border `├` and `┤`.
|
2018-04-03 23:38:54 +00:00
|
|
|
pub fn print_hdelim<T>(&self, start: T, len: usize)
|
|
|
|
where
|
|
|
|
T: Into<Vec2>,
|
|
|
|
{
|
2016-07-12 02:24:00 +00:00
|
|
|
let start = start.into();
|
2016-06-28 05:10:59 +00:00
|
|
|
self.print(start, "├");
|
2017-08-14 23:32:01 +00:00
|
|
|
self.print_hline(start + (1, 0), len.saturating_sub(2), "─");
|
|
|
|
self.print(start + (len.saturating_sub(1), 0), "┤");
|
2015-05-20 17:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 23:38:54 +00:00
|
|
|
/// Returns a sub-printer with the given offset.
|
2018-04-25 00:19:07 +00:00
|
|
|
///
|
|
|
|
/// It will print in an area slightly to the bottom/right.
|
2018-04-03 23:38:54 +00:00
|
|
|
pub fn offset<S>(&self, offset: S) -> Printer
|
|
|
|
where
|
|
|
|
S: Into<Vec2>,
|
|
|
|
{
|
|
|
|
let offset = offset.into();
|
|
|
|
self.clone().with(|s| {
|
2018-04-25 00:19:07 +00:00
|
|
|
// If we are drawing a part of the content,
|
|
|
|
// let's reduce this first.
|
2018-04-03 23:38:54 +00:00
|
|
|
let consumed = Vec2::min(s.content_offset, offset);
|
|
|
|
|
2018-04-25 00:19:07 +00:00
|
|
|
let offset = offset - consumed;
|
2018-04-03 23:38:54 +00:00
|
|
|
s.content_offset = s.content_offset - consumed;
|
2018-04-25 00:19:07 +00:00
|
|
|
|
|
|
|
s.offset = s.offset + offset;
|
|
|
|
|
2018-04-27 23:23:07 +00:00
|
|
|
s.output_size = s.output_size.saturating_sub(offset);
|
2018-04-03 23:38:54 +00:00
|
|
|
s.size = s.size.saturating_sub(offset);
|
|
|
|
})
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|
2016-07-16 06:44:38 +00:00
|
|
|
|
2018-04-03 23:38:54 +00:00
|
|
|
/// 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.
|
|
|
|
pub fn focused(&self, focused: bool) -> Self {
|
|
|
|
self.clone().with(|s| {
|
|
|
|
s.focused &= focused;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
pub fn cropped<S>(&self, size: S) -> Self
|
|
|
|
where
|
|
|
|
S: Into<Vec2>,
|
|
|
|
{
|
|
|
|
self.clone().with(|s| {
|
2018-04-27 23:23:07 +00:00
|
|
|
let size = size.into();
|
|
|
|
s.output_size = Vec2::min(s.output_size, size);
|
2018-04-03 23:38:54 +00:00
|
|
|
s.size = Vec2::min(s.size, size);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new sub-printer with a shrinked area.
|
|
|
|
///
|
|
|
|
/// The printer size will be reduced by the given border from the bottom-right.
|
|
|
|
pub fn shrinked<S>(&self, borders: S) -> Self
|
|
|
|
where
|
|
|
|
S: Into<Vec2>,
|
|
|
|
{
|
|
|
|
self.cropped(self.size.saturating_sub(borders))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new sub-printer with a content offset.
|
|
|
|
pub fn content_offset<S>(&self, offset: S) -> Self
|
|
|
|
where
|
|
|
|
S: Into<Vec2>,
|
|
|
|
{
|
|
|
|
self.clone().with(|s| {
|
|
|
|
s.content_offset = s.content_offset + offset;
|
|
|
|
})
|
2016-07-16 06:44:38 +00:00
|
|
|
}
|
2018-04-27 23:23:07 +00:00
|
|
|
|
|
|
|
/// Returns a sub-printer with a different inner size.
|
|
|
|
///
|
|
|
|
/// This will not change the actual output size, but will appear bigger to
|
|
|
|
/// users of this printer.
|
|
|
|
///
|
|
|
|
/// Useful to give to children who think they're big, but really aren't.
|
|
|
|
pub fn inner_size<S>(&self, size: S) -> Self
|
|
|
|
where
|
|
|
|
S: Into<Vec2>,
|
|
|
|
{
|
|
|
|
self.clone().with(|s| {
|
|
|
|
s.size = size.into();
|
|
|
|
})
|
|
|
|
}
|
2015-05-15 18:58:47 +00:00
|
|
|
}
|