DrawProcess

Struct DrawProcess 

Source
pub struct DrawProcess { /* private fields */ }
Expand description

A structure that can display text inside a grid.
Cloning chunk processes is bad practice! Use it only if you have to.

Implementations§

Source§

impl DrawProcess

Source

pub fn width(&self) -> usize

Gets the chunk’s width - the number of characters that can be displayed on a line.

let mut grid = grid::Frame::new(30, 30, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert_eq!(process.width(), 70);
Source

pub fn height(&self) -> usize

Gets the chunk’s height - the number of lines that can fit in it.

let mut grid = grid::Frame::new(30, 30, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert_eq!(process.height(), 70);
Source

pub fn start_x(&self) -> usize

Gets the x position where the process begins.

let mut grid = grid::Frame::new(30, 30, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert_eq!(process.start_x(), 30);
Source

pub fn start_y(&self) -> usize

Gets the y position where the process begins.

let mut grid = grid::Frame::new(30, 30, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert_eq!(process.start_y(), 30);
Source

pub fn end_x(&self) -> usize

Gets the x position where the process ends.

let mut grid = grid::Frame::new(30, 30, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert_eq!(process.end_x(), 100);
Source

pub fn end_y(&self) -> usize

Gets the y position where the process ends.

let mut grid = grid::Frame::new(30, 30, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert_eq!(process.end_y(), 100);
Source

pub fn add_to_section_lines<T, I>( &mut self, text: I, strategy: &mut T, section: Alignment, ) -> Vec<Result<(), FormatError<T>>>
where T: TrimStrategy, I: DoubleEndedIterator + Iterator<Item = T::Input>,

Adds multi-line content to the selection, using the inputted strategy inside the inputted alignment. Returns everything that can’t fit. Note that the multi-line content goes top to bottom, even if Alignment::Minus is selected. This is the exact opposite behavior of simply sending multiple lines. The content only needs to be iterated through.

§Errors

Each position represents the corresponding position of the text input. An error will be found if the call to add_to_section() returns an error.

§Examples

Usage in positive direction

let mut grid = grid::Frame::new(0, 0, 10, 3).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section_lines(vec!["Some stuff".to_string(), "More stuff".to_string()].into_iter(), &mut Ignore, grid::Alignment::Plus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("Some stuff\nMore stuff\n          \n".to_string(), output);

Usage in negative direction

let mut grid = grid::Frame::new(0, 0, 10, 3).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::End);
process.add_to_section_lines(vec!["Some stuff".to_string(), "More stuff".to_string()].into_iter(), &mut Ignore, grid::Alignment::Minus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("          \nSome stuff\nMore stuff\n".to_string(), output);

Errors in positive direction

let mut grid = grid::Frame::new(0, 0, 10, 2).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
let result = process.add_to_section_lines(vec!["Some stuff".to_string(), "More stuff".to_string(), "Even more!".to_string()].into_iter(), &mut Ignore, grid::Alignment::Plus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("Some stuff\nMore stuff\n".to_string(), output);
assert!(result[2].is_err());

Errors in negative direction

let mut grid = grid::Frame::new(0, 0, 10, 2).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::End);
let result = process.add_to_section_lines(vec!["Some stuff".to_string(), "More stuff".to_string(), "Even more!".to_string()].into_iter(), &mut Ignore, grid::Alignment::Minus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("More stuff\nEven more!\n".to_string(), output);
assert!(result[0].is_err());
Source

pub fn add_to_section<T: TrimStrategy>( &mut self, text: T::Input, strategy: &mut T, section: Alignment, ) -> Result<(), FormatError<T>>

Adds single-line content to the selection, using the inputted strategy inside the inputted alignment.

§Errors

This method will return an error if the text won’t fit. The text will be returned (although it might be trimmed from trim methods.)

§Examples

Basic printing:

let mut grid = grid::Frame::new(0, 0, 10, 3).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("Some stuff\n          \n          \n".to_string(), output);

How order works

let mut grid = grid::Frame::new(0, 0, 10, 3).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
process.add_to_section("More stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("Some stuff\nMore stuff\n          \n".to_string(), output);

Running out of space:

let mut grid = grid::Frame::new(0, 0, 10, 1).next_frame(); // creates a grid with one line
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
assert!(process.add_to_section("No more".to_string(), &mut Ignore, grid::Alignment::Plus).is_err());

Divider strategies can determine where you have space in which section.

let mut grid = grid::Frame::new(0, 0, 100, 100).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
assert!(process.add_to_section(
    "The divider is at the beginning! There's no room for negatively aligned text!"
    .to_string(),
    &mut Ignore, grid::Alignment::Minus).is_err());
Source

pub fn clear(&mut self, new_strategy: DividerStrategy)

Clears the process, allowing it to be re-used.

§Example
let mut grid = grid::Frame::new(0, 0, 10, 1).next_frame(); // creates a grid with one line
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
assert!(process.add_to_section("No more".to_string(), &mut Ignore, grid::Alignment::Plus).is_err());
process.clear(grid::DividerStrategy::Beginning);
assert!(process.add_to_section("Some more".to_string(), &mut Ignore, grid::Alignment::Plus).is_ok());
Source

pub fn split_free_space( &mut self, a: Alignment, min_left: Option<usize>, max_taken: Option<usize>, ) -> Option<Grid>

Gives up free space in the Y direction, producing a grid if there’s free space to give up. Will take up to max_taken lines of space. If max_taken is set to None, it will take up to the divider line. Will leave at least min_left lines TOTAL (in either direction). Might leave some blank lines. Returns this space in a grid if there is any. If the process is already full, nothing will be returned.

§Example
let mut grid = grid::Frame::new(0, 0, 10, 10).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
assert_eq!(process.split_free_space(grid::Alignment::Plus, None, None), Some(grid::Grid{start_x: 0, start_y: 1, end_x: 10, end_y: 10}));
assert_eq!(process.end_y(), 1);
Source

pub fn extend(&mut self, grid: Grid) -> Result<(), Grid>

Extends the grid in the either direction, either positive or negative, if the input is compatible (ie grids are next to each other and of similar dimensions) If the two grids are incompatible, it returns an error and gives the grid back.

§Example
let mut grid = grid::Frame::new(0, 0, 10, 10).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
let free_space = process.split_free_space(grid::Alignment::Plus, None, None).ok_or(())?;
assert_eq!(process.end_y(), 1);
assert!(process.extend(free_space).is_ok());
assert_eq!(process.end_y(), 10);
let incompatible_grid = grid::Frame::new(4, 4, 8, 8).next_frame();
assert!(process.extend(incompatible_grid).is_err());
Source

pub fn print<H: Handler>( &mut self, handler: &mut H, out: &mut H::OutputDevice, ) -> Result<(), H::Error>

Prints out the grid using a handler.

§Errors

Returns an error if the handler returns an error.

let mut grid = grid::Frame::new(0, 0, 10, 3).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
let mut output: String = String::new();
process.print(&mut out::OutToString, &mut output)?;
assert_eq!("Some stuff\n          \n          \n".to_string(), output);
Source

pub fn print_safe<H: SafeHandler>( &mut self, handler: &mut H, out: &mut H::OutputDevice, )

Prints safely - this method cannot return an error.

§Panics

This method panics when the handler panics.

§Examples

Safe printing:

let mut grid = grid::Frame::new(0, 0, 10, 3).next_frame();
let mut process = grid.into_process(grid::DividerStrategy::Beginning);
process.add_to_section("Some stuff".to_string(), &mut Ignore, grid::Alignment::Plus);
let mut output: String = String::new();
process.print_safe(&mut out::OutToString, &mut output);
assert_eq!("Some stuff\n          \n          \n".to_string(), output);

Trait Implementations§

Source§

impl Clone for DrawProcess

Source§

fn clone(&self) -> DrawProcess

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DrawProcess

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for DrawProcess

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DrawProcess

Source§

fn eq(&self, other: &DrawProcess) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for DrawProcess

Source§

impl StructuralPartialEq for DrawProcess

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.