Boxy

Struct Boxy 

Source
pub struct Boxy<'a> {
Show 15 fields pub type_enum: BoxType, pub data: Vec<Vec<Cow<'a, str>>>, pub sect_count: usize, pub box_col: String, pub colors: Vec<Vec<Cow<'a, str>>>, pub int_padding: BoxPad, pub ext_padding: BoxPad, pub align: BoxAlign, pub seg_align: Vec<BoxAlign>, pub fixed_width: usize, pub fixed_height: usize, pub seg_v_div_count: Vec<usize>, pub seg_v_div_ratio: Vec<Vec<usize>>, pub tot_seg: usize, pub terminal_width_offset: i32,
}
Expand description

The main struct that represents a text box for CLI display.

Boxy contains all the configuration and content needed to render a styled text box in the terminal, including borders, text content, colors, padding, and alignment options.

§Examples

use boxy_cli::prelude::*;

// Create a simple text box
let mut my_box = Boxy::new(BoxType::Double, "#00ffff");
my_box.add_text_sgmt("Hello, World!", "#ffffff", BoxAlign::Center);
my_box.display();

Fields§

§type_enum: BoxType§data: Vec<Vec<Cow<'a, str>>>§sect_count: usize§box_col: String§colors: Vec<Vec<Cow<'a, str>>>§int_padding: BoxPad§ext_padding: BoxPad§align: BoxAlign§seg_align: Vec<BoxAlign>§fixed_width: usize§fixed_height: usize§seg_v_div_count: Vec<usize>§seg_v_div_ratio: Vec<Vec<usize>>§tot_seg: usize§terminal_width_offset: i32

Implementations§

Source§

impl<'a> Boxy<'a>

Source

pub fn new(box_type: BoxType, box_color: &str) -> Self

Creates a new instance of the Boxy struct with the specified border type and color.

§Arguments
  • box_type - The border style to use from the BoxType enum
  • box_color - A hex color code (e.g. “#00ffff”) for the border color
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Double, "#00ffff");
Source

pub fn builder() -> BoxyBuilder<'a>

Returns a new BoxyBuilder to create a textbox using the builder pattern.

The builder pattern provides a more fluent interface for configuring and creating a Boxy instance.

§Examples
use boxy_cli::prelude::*;

let my_box = Boxy::builder()
    .box_type(BoxType::Double)
    .color("#00ffff")
    .add_segment("Hello, World!", "#ffffff", BoxAlign::Center)
    .build();
Source

pub fn add_text_sgmt( &mut self, data_string: &str, color: &str, text_align: BoxAlign, )

Adds a new text segment/section to the textbox, separated by a horizontal divider.

Each segment represents a distinct section of the textbox that will be separated by horizontal dividers. This method is typically used to add the first or later major sections of content.

§Arguments
  • data_string - The text content for this segment
  • color - A hex color code (e.g. “#ffffff”) for the text color
  • text_align - The alignment (left, center, right) for this text segment
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
my_box.add_text_sgmt("Header section", "#ffffff", BoxAlign::Center);
my_box.add_text_sgmt("Content section", "#ffffff", BoxAlign::Left);
Source

pub fn add_text_line_indx( &mut self, data_string: &str, color: &str, seg_index: usize, )

Adds a new text line to the segment with a specific index.

This method allows adding additional lines of text to an existing segment by specifying the segment’s index. The new line will appear below the existing content in that segment.

§Arguments
  • data_string - The text content to add
  • color - A hex color code (e.g. “#ffffff”) for the text color
  • seg_index - The index of the segment to add this line to (0-based)
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
my_box.add_text_sgmt("First segment", "#ffffff", BoxAlign::Left);
my_box.add_text_sgmt("Second segment", "#ffffff", BoxAlign::Left);

// Add a line to the first segment (index 0)
my_box.add_text_line_indx("Additional line in first segment", "#32CD32", 0);
§Panics

Panics if seg_index is out of bounds.

Source

pub fn add_text_line(&mut self, data_string: &str, color: &str)

Adds a new text line to the most recently added segment.

This is a convenience method that adds a line of text to the last segment that was created, eliminating the need to specify the segment index.

§Arguments
  • data_string - The text content to add
  • color - A hex color code (e.g. “#ffffff”) for the text color
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
my_box.add_text_sgmt("Header", "#ffffff", BoxAlign::Center);
my_box.add_text_line("Additional details below the header", "#32CD32");
§Panics

Panics if no segments have been added yet.

Source

pub fn set_align(&mut self, align: BoxAlign)

Sets the overall alignment of the textbox within the terminal.

This controls the horizontal positioning of the entire textbox relative to the terminal width. It does not affect the alignment of text within the box segments.

§Arguments
  • align - The alignment to use: BoxAlign::Left, BoxAlign::Center, or BoxAlign::Right
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
my_box.set_align(BoxAlign::Center); // Center the box in the terminal
Source

pub fn set_int_padding(&mut self, int_padding: BoxPad)

Sets the internal padding between the textbox border and its text content.

Internal padding creates space between the border of the box and the text inside it.

§Arguments
  • int_padding - A BoxPad instance specifying the padding values
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");

// Set uniform padding of 2 spaces on all sides
my_box.set_int_padding(BoxPad::uniform(2));

// Or set different padding for each side (top, left, down, right)
my_box.set_int_padding(BoxPad::from_tldr(1, 3, 1, 3));
Source

pub fn set_ext_padding(&mut self, ext_padding: BoxPad)

Sets the external padding between the terminal edges and the textbox.

External padding creates space between the edges of the terminal and the border of the box. This affects the positioning of the box within the terminal.

§Arguments
  • ext_padding - A BoxPad instance specifying the padding values
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
 
// Add 5 spaces of padding on all sides
my_box.set_ext_padding(BoxPad::uniform(5));

// Or set different padding for each side (top, left, down, right)
my_box.set_ext_padding(BoxPad::from_tldr(0, 10, 0, 0));
Source

pub fn set_padding(&mut self, ext_padding: BoxPad, int_padding: BoxPad)

Sets both internal and external padding for the textbox in a single call.

This is a convenience method that combines set_int_padding and set_ext_padding.

§Arguments
  • ext_padding - A BoxPad instance for the external padding (between terminal edges and box)
  • int_padding - A BoxPad instance for the internal padding (between box border and text)
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");

// Set both internal and external padding
my_box.set_padding(
    BoxPad::from_tldr(1, 5, 1, 5), // external padding
    BoxPad::uniform(2)            // internal padding
);
Source

pub fn set_width(&mut self, width: usize)

Sets a fixed width for the textbox instead of dynamically sizing it to the terminal width.

By default, the textbox automatically adjusts its width based on the terminal size. This method allows you to specify a fixed width instead.

§Arguments
  • width - The desired width in characters (including borders)
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
my_box.set_width(50); // Fix the box width to 50 characters
§Note

Setting width to 0 returns to dynamic sizing based on terminal width.

Source

pub fn set_height(&mut self, height: usize)

Sets a fixed height for the textbox by adding whitespace above and below the text.

§Arguments
  • height - The desired height in characters (including borders)
§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Single, "#00ffff");
my_box.set_height(20); // Set box height to 20 lines
§Note

This feature is experimental and may not work as expected in the current version. Setting height to 0 returns to dynamic sizing based on content.

Source

pub fn set_segment_ratios(&mut self, seg_index: usize, ratios: Vec<usize>)

Sets the size-ratio between segments when using vertical divisions

This feature is still experimental and not yet implemented fully, and hence may not work in the current version of the crate.

Source

pub fn display(&mut self)

Renders and displays the textbox in the terminal.

This method performs all the necessary calculations to render the textbox with the configured settings, including border style, colors, padding, and text content. It then prints the textbox to the standard output.

§Examples
use boxy_cli::prelude::*;

let mut my_box = Boxy::new(BoxType::Double, "#00ffff");
my_box.add_text_sgmt("Hello, World!", "#ffffff", BoxAlign::Center);
my_box.display(); // Renders the box to the terminal
§Note

The appearance may vary depending on terminal support for colors and Unicode characters.

Trait Implementations§

Source§

impl<'a> Debug for Boxy<'a>

Source§

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

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

impl Default for Boxy<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Boxy<'a>

§

impl<'a> RefUnwindSafe for Boxy<'a>

§

impl<'a> Send for Boxy<'a>

§

impl<'a> Sync for Boxy<'a>

§

impl<'a> Unpin for Boxy<'a>

§

impl<'a> UnwindSafe for Boxy<'a>

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.