use colored::Colorize;
use hex_color::HexColor;
use crate::templates::*;
use crate::constructs::*;
#[derive(Debug)]
pub struct Boxy {
pub type_enum: BoxType,
pub data : Vec<Vec<String>>,
pub sect_count: usize,
pub box_col : String,
pub colors : Vec<Vec<String>>,
pub int_padding: BoxPad,
pub ext_padding: BoxPad,
pub align : 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,
}
impl Default for Boxy {
fn default() -> Self {
Self {
type_enum: BoxType::Single,
data : Vec::<Vec<String>>::new(),
sect_count: 0usize,
box_col: "#ffffff".to_string(),
colors : Vec::<Vec<String>>::new(),
int_padding: BoxPad::new(),
ext_padding: BoxPad::new(),
align : BoxAlign::Left,
fixed_width: 0usize,
fixed_height: 0usize,
seg_v_div_count: Vec::<usize>::new(),
seg_v_div_ratio: Vec::<Vec<usize>>::new(),
tot_seg: 0usize,
}
}
}
impl Boxy {
pub fn new(box_type: BoxType, box_color : &str) -> Self {
Boxy{
type_enum: box_type,
data : Vec::<Vec<String>>::new(),
sect_count: 0usize,
box_col : (&box_color).to_string(),
colors : Vec::<Vec<String>>::new(),
int_padding: BoxPad::new(),
ext_padding: BoxPad::new(),
align : BoxAlign::Left,
fixed_width: 0usize,
fixed_height: 0usize,
seg_v_div_count: Vec::<usize>::new(),
seg_v_div_ratio: Vec::<Vec<usize>>::new(),
tot_seg: 0usize,
}
}
pub fn add_text_sgmt(&mut self, data_string : &str, color : &str) {
self.data.push(vec![data_string.to_owned()]);
self.colors.push(vec![String::from(color)]);
self.sect_count+=1;
}
pub fn add_text_line_indx(&mut self, data_string : &str, seg_index : usize) {
self.data[seg_index].push(data_string.to_owned());
}
pub fn add_text_line(&mut self, data_string : &str) {
self.data[self.sect_count-1].push(String::from(data_string));
}
pub fn set_align(&mut self, align: BoxAlign) {
self.align = align;
}
pub fn set_int_padding(&mut self, int_padding : BoxPad) {
self.int_padding = int_padding;
}
pub fn set_ext_padding(&mut self, ext_padding : BoxPad) {
self.ext_padding = ext_padding;
}
pub fn set_padding(&mut self, ext_padding : BoxPad, int_padding : BoxPad) {
self.int_padding = int_padding;
self.ext_padding = ext_padding;
}
pub fn set_width(&mut self, width : usize) {
self.fixed_width = width;
}
pub fn set_height(&mut self, height : usize) {
self.fixed_height = height;
}
pub fn set_segment_ratios(&mut self, seg_index: usize, ratios: Vec<usize>) {
if seg_index >= self.seg_v_div_ratio.len() {
self.seg_v_div_ratio.resize(seg_index + 1, Vec::new());
}
self.seg_v_div_ratio[seg_index] = ratios;
}
pub fn display(&mut self) {
let disp_width = if self.fixed_width !=0 {
self.fixed_width
} else {
let size = termsize::get();
if size.is_some() {
size.unwrap().cols as usize - 20
} else {
return;
}
};
let col_truevals = HexColor::parse(&self.box_col).unwrap();
let box_pieces = map_box_type(&self.type_enum);
let horiz =box_pieces.horizontal.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b);
print!("{:>width$}", box_pieces.top_left.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b), width=self.ext_padding.left+1);
for _ in 0..(disp_width -2*self.ext_padding.right) {
print!("{}", horiz);
}
println!("{}", box_pieces.top_right.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b));
for i in 0..self.sect_count {
if i > 0 {
self.print_h_divider(&col_truevals, &disp_width);
}
self.display_segment(i, &disp_width);
}
print!("{:>width$}", box_pieces.bottom_left.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b), width=self.ext_padding.left+1);
for _ in 0..disp_width -2*self.ext_padding.right {
print!("{}", horiz);
}
println!("{}", box_pieces.bottom_right.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b));
}
fn display_segment(&mut self, seg_index: usize, disp_width: &usize) {
let col_truevals = HexColor::parse(&self.box_col).unwrap();
for i in 0..self.data[seg_index].len() {
let mut processed_data = String::with_capacity(self.data[seg_index][i].len()+1);
processed_data.push_str(self.data[seg_index][i].trim());
processed_data.push(' ');
let mut ws_indices = Vec::new();
let mut k = 0usize;
while k < processed_data.len() {
if processed_data.as_bytes()[k] == b' ' {
ws_indices.push(k);
}
k += 1;
}
let liner: Vec<String> = text_wrap_vec(&processed_data, &mut ws_indices, &disp_width.clone(), &self.ext_padding, &self.int_padding);
iter_line_prnt(&liner, map_box_type(&self.type_enum), &col_truevals, disp_width, &self.ext_padding, &self.int_padding, &self.align);
if i < self.data[seg_index].len() - 1 {
println!("{1:>width$}{}{1}", " ".repeat(disp_width - self.ext_padding.lr()),
map_box_type(&self.type_enum)
.vertical.to_string()
.truecolor(col_truevals.r, col_truevals.g, col_truevals.b),
width=self.ext_padding.left+1);
}
}
}
fn print_h_divider(&mut self, boxcol: &HexColor, disp_width: &usize){
let box_pieces = map_box_type(&self.type_enum);
let horiz = box_pieces.horizontal.to_string().truecolor(boxcol.r, boxcol.g, boxcol.b);
print!("{:>width$}", box_pieces.left_t.to_string().truecolor(boxcol.r, boxcol.g, boxcol.b), width=self.ext_padding.left+1);
for _ in 0..*disp_width-self.ext_padding.lr() {
print!("{}", horiz);
}
println!("{}", box_pieces.right_t.to_string().truecolor(boxcol.r, boxcol.g, boxcol.b));
}
fn _display_segment_with_ratios(&mut self, seg_index: usize, terminal_size: &usize) {
let col_truevals = HexColor::parse(&self.box_col).unwrap();
let box_pieces = map_box_type(&self.type_enum);
let ratios = if seg_index < self.seg_v_div_ratio.len() {
&self.seg_v_div_ratio[seg_index]
} else {
static EMPTY_VEC: Vec<usize> = Vec::new();
&EMPTY_VEC
};
if ratios.is_empty() {
self.display_segment(seg_index, terminal_size);
return;
}
let total_ratio: usize = ratios.iter().sum();
let printable_width = terminal_size - self.ext_padding.lr();
let segment_widths: Vec<usize> = ratios.iter().map(|r| r * printable_width / total_ratio).collect();
let mut mini_segments: Vec<Vec<String>> = vec![Vec::new(); ratios.len()];
let lines = &self.data[seg_index];
for line in lines.iter() {
let mut processed_data = String::with_capacity(line.len() + 1);
processed_data.push_str(line.trim());
processed_data.push(' ');
let mut ws_indices = Vec::new();
let mut k = 0usize;
while k < processed_data.len() {
if processed_data.as_bytes()[k] == b' ' {
ws_indices.push(k);
}
k += 1;
}
for (j, width) in segment_widths.iter().enumerate() {
let liner = text_wrap_vec(&processed_data, &mut ws_indices, width, &self.ext_padding, &self.int_padding);
mini_segments[j].extend(liner);
}
}
let max_lines = mini_segments.iter().map(|seg| seg.len()).max().unwrap_or(0);
for line_index in 0..max_lines {
print!("{:>width$}", box_pieces.vertical.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b), width = self.ext_padding.left + 1);
for (j, segment) in mini_segments.iter().enumerate() {
if line_index < segment.len() {
print!("{:<pad$}", " ", pad = self.int_padding.left);
print!("{:<width$}", segment[line_index], width = segment_widths[j] - self.int_padding.lr());
print!("{:<pad$}", " ", pad = self.int_padding.right);
} else {
print!("{:<width$}", " ", width = segment_widths[j]);
}
if j < mini_segments.len() - 1 {
print!("{}", box_pieces.vertical.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b));
}
}
println!("{}", box_pieces.vertical.to_string().truecolor(col_truevals.r, col_truevals.g, col_truevals.b));
}
}
}
fn nearest_whitespace(map: &mut Vec<usize>, printable_length: &usize, start_index: usize) -> usize {
let mut next_ws = 0;
for i in map {
if *i > start_index && *i-start_index <= *printable_length {
next_ws = *i;
}
}
if next_ws == 0 {
next_ws = start_index + printable_length;
}
next_ws
}
fn text_wrap_vec(data:&str, map: &mut Vec<usize>, disp_width: &usize, ext_padding: &BoxPad, int_padding: &BoxPad) -> Vec<String> {
let mut liner: Vec<String> = Vec::new();
let mut start_index = 0;
while start_index < data.len() {
let next_ws = nearest_whitespace(map, &(disp_width - (int_padding.lr() + ext_padding.lr()) - 2), start_index);
liner.push(data[start_index..next_ws].to_string());
if next_ws >= data.len()-1 {break;}
start_index = next_ws+1;
}
liner
}
fn iter_line_prnt(liner : &[String], box_pieces:BoxTemplates, box_col: &HexColor, disp_width: &usize, ext_padding: &BoxPad, int_padding: &BoxPad, align: &BoxAlign) {
let printable_area = disp_width - (int_padding.lr() + ext_padding.lr());
let vertical = box_pieces.vertical.to_string().truecolor(box_col.r, box_col.g, box_col.b);
match align {
BoxAlign::Left => {
for i in liner.iter() {
print!("{:>width$}", vertical, width=ext_padding.left+1);
print!("{:<pad$}", " ", pad=int_padding.left);
print!("{:<width$}", i, width=printable_area-2); print!("{:<pad$}", " ", pad=int_padding.right);
println!("{}", vertical);
}
},
BoxAlign::Center => {
for i in liner.iter() {
print!("{:>width$}", vertical, width=ext_padding.left+1);
print!("{:<pad$}", " ", pad=int_padding.left+((printable_area-i.len())/2));
print!("{}", i);
print!("{:<pad$}", " ", pad=int_padding.right+(printable_area-i.len())-((printable_area-i.len())/2));
println!("{}", vertical);
}
},
BoxAlign::Right => {
for i in liner.iter() {
print!("{:>width$}", vertical, width=ext_padding.left+1);
print!("{:<pad$}", " ", pad=int_padding.left);
print!("{:>width$}", i, width=printable_area-2); print!("{:<pad$}", " ", pad=int_padding.right);
println!("{}", vertical);
}
}
}
}
fn map_box_type (boxtype : &BoxType) -> BoxTemplates{
match boxtype {
BoxType::Classic => CLASSIC_TEMPLATE,
BoxType::Single => SINGLE_TEMPLATE,
BoxType::DoubleHorizontal => DOUB_H_TEMPLATE,
BoxType::DoubleVertical => DOUB_V_TEMPLATE,
BoxType::Double => DOUBLE_TEMPLATE,
BoxType::Bold => BOLD_TEMPLATE,
BoxType::Rounded => ROUNDED_TEMPLATE,
BoxType::BoldCorners => BOLD_CORNERS_TEMPLATE,
}
}
pub fn resolve_col(dat : String) -> String {
dat
}
pub fn resolve_pad(dat : String) -> BoxPad {
BoxPad::uniform(dat.parse::<usize>().unwrap_or(0usize))
}
pub fn resolve_align(dat : String) -> BoxAlign {
match &*dat {
"center" => BoxAlign::Center,
"right" => BoxAlign::Right,
"left" => BoxAlign::Left,
_ => BoxAlign::Left,
}
}
pub fn resolve_type(dat : String) -> BoxType{
match &*dat {
"classic" => BoxType::Classic,
"single" => BoxType::Single,
"double_horizontal" => BoxType::DoubleHorizontal,
"double_vertical" => BoxType::DoubleVertical,
"double" => BoxType::Double,
"bold" => BoxType::Bold,
"rounded" => BoxType::Rounded,
"bold_corners" => BoxType::BoldCorners,
_ => BoxType::Single,
}
}
pub fn resolve_segments(dat : String) -> usize {
dat.parse().expect("failed to parse total segment number")
}