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
impl DrawProcess
Sourcepub fn width(&self) -> usize
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);
Sourcepub fn height(&self) -> usize
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);
Sourcepub fn start_x(&self) -> usize
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);
Sourcepub fn start_y(&self) -> usize
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);
Sourcepub fn end_x(&self) -> usize
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);
Sourcepub fn end_y(&self) -> usize
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);
Sourcepub fn add_to_section_lines<T, I>(
&mut self,
text: I,
strategy: &mut T,
section: Alignment,
) -> Vec<Result<(), FormatError<T>>>
pub fn add_to_section_lines<T, I>( &mut self, text: I, strategy: &mut T, section: Alignment, ) -> Vec<Result<(), FormatError<T>>>
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());
Sourcepub fn add_to_section<T: TrimStrategy>(
&mut self,
text: T::Input,
strategy: &mut T,
section: Alignment,
) -> Result<(), FormatError<T>>
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());
Sourcepub fn clear(&mut self, new_strategy: DividerStrategy)
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());
Sourcepub fn split_free_space(
&mut self,
a: Alignment,
min_left: Option<usize>,
max_taken: Option<usize>,
) -> Option<Grid>
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);
Sourcepub fn extend(&mut self, grid: Grid) -> Result<(), Grid>
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());
Sourcepub fn print<H: Handler>(
&mut self,
handler: &mut H,
out: &mut H::OutputDevice,
) -> Result<(), H::Error>
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);
Sourcepub fn print_safe<H: SafeHandler>(
&mut self,
handler: &mut H,
out: &mut H::OutputDevice,
)
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
impl Clone for DrawProcess
Source§fn clone(&self) -> DrawProcess
fn clone(&self) -> DrawProcess
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more