Struct ScaleFitView

Source
#[non_exhaustive]
pub struct ScaleFitView { pub view: View, pub empty_row_count: i64, }
Expand description

A wrapper around a View which auto resizes to fit the terminal window

ScaleFitView’s update() function should be used in place of View’s clear() function to handle auto-resizing and clearing

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§view: View

The View that this struct wraps around

§empty_row_count: i64

How many rows to leave clear below the rendered view. You might want to set this if you have more than one line of text after rendered text

Implementations§

Source§

impl ScaleFitView

Source

pub fn new(background_char: ColChar) -> Self

Create a new ScaleFitView with the given background ColChar

Examples found in repository?
examples/self-resizing.rs (line 17)
16fn main() {
17    let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
18
19    let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
20        .with_align(TextAlign::Centered);
21
22    let mut sprite =
23        Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
24
25    loop {
26        text.pos = scale_view.intended_size() / 2;
27        sprite.pos = scale_view.intended_size() / 2;
28        sprite.pos.y -= 5;
29
30        scale_view.update();
31        scale_view.view.draw(&text);
32        scale_view.view.draw(&sprite);
33        let _ = scale_view.view.display_render();
34
35        thread::sleep(Duration::from_millis(10));
36    }
37}
Source

pub const fn with_empty_row_count(self, empty_row_count: i64) -> Self

Return the ScaleFitView with an updated empty_row_count property. Consumes the original ScaleFitView

Source

pub fn intended_size(&self) -> Vec2D

Returns the size of the terminal, with the y adjusted as intended using the empty_row_count property

§Panics

Panics if there is no TTY to get the terminal size of, as per terminal_size::terminal_size()

Examples found in repository?
examples/self-resizing.rs (line 26)
16fn main() {
17    let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
18
19    let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
20        .with_align(TextAlign::Centered);
21
22    let mut sprite =
23        Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
24
25    loop {
26        text.pos = scale_view.intended_size() / 2;
27        sprite.pos = scale_view.intended_size() / 2;
28        sprite.pos.y -= 5;
29
30        scale_view.update();
31        scale_view.view.draw(&text);
32        scale_view.view.draw(&sprite);
33        let _ = scale_view.view.display_render();
34
35        thread::sleep(Duration::from_millis(10));
36    }
37}
Source

pub fn update(&mut self)

Resize and clear the View

§Panics

Panics if there is no TTY to get the terminal size of, as per terminal_size::terminal_size()

Examples found in repository?
examples/self-resizing.rs (line 30)
16fn main() {
17    let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
18
19    let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
20        .with_align(TextAlign::Centered);
21
22    let mut sprite =
23        Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
24
25    loop {
26        text.pos = scale_view.intended_size() / 2;
27        sprite.pos = scale_view.intended_size() / 2;
28        sprite.pos.y -= 5;
29
30        scale_view.update();
31        scale_view.view.draw(&text);
32        scale_view.view.draw(&sprite);
33        let _ = scale_view.view.display_render();
34
35        thread::sleep(Duration::from_millis(10));
36    }
37}

Trait Implementations§

Source§

impl Canvas for ScaleFitView

Source§

fn plot(&mut self, pos: Vec2D, c: ColChar)

Plot a ColChar to the Canvas at pos

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.