use std::string::ToString;
use crate::color::AnsiColor;
#[derive(Debug, Clone)]
pub struct CharSet {
pub horizontal: char,
pub vertical_line: char,
pub arc_down_right: char,
pub arc_down_left: char,
pub arc_up_right: char,
pub arc_up_left: char,
pub end_cap: char,
pub start_cap: char,
pub up_right: char,
pub down_horizontal: char,
}
impl Default for CharSet {
fn default() -> Self {
CharSet {
horizontal: DEFAULT_CHAR_SET.horizontal,
vertical_line: DEFAULT_CHAR_SET.vertical_line,
arc_down_right: DEFAULT_CHAR_SET.arc_down_right,
arc_down_left: DEFAULT_CHAR_SET.arc_down_left,
arc_up_right: DEFAULT_CHAR_SET.arc_up_right,
arc_up_left: DEFAULT_CHAR_SET.arc_up_left,
end_cap: DEFAULT_CHAR_SET.end_cap,
start_cap: DEFAULT_CHAR_SET.start_cap,
up_right: DEFAULT_CHAR_SET.up_right,
down_horizontal: DEFAULT_CHAR_SET.down_horizontal,
}
}
}
pub const DEFAULT_CHAR_SET: CharSet = CharSet {
horizontal: '─',
vertical_line: '│',
arc_down_right: '╭',
arc_down_left: '╮',
arc_up_right: '╰',
arc_up_left: '╯',
end_cap: '╴',
start_cap: '╶',
up_right: '└',
down_horizontal: '┬',
};
pub fn create_char_set(character: char) -> CharSet {
CharSet {
horizontal: character,
vertical_line: character,
arc_down_right: character,
arc_down_left: character,
arc_up_right: character,
arc_up_left: character,
end_cap: character,
start_cap: character,
up_right: character,
down_horizontal: character,
}
}
pub struct Config {
pub width: usize,
pub height: usize,
pub lower_bound: Option<f64>,
pub upper_bound: Option<f64>,
pub offset: usize,
pub caption: String,
pub precision: Option<usize>,
pub caption_color: AnsiColor,
pub axis_color: AnsiColor,
pub label_color: AnsiColor,
pub series_colors: Vec<AnsiColor>,
pub series_legends: Vec<String>,
pub line_ending: String,
pub series_chars: Vec<CharSet>,
pub x_axis_tick_count: usize,
pub x_axis_range: Option<[f64; 2]>,
pub x_axis_value_formatter: Option<Box<dyn Fn(f64) -> String>>,
pub y_axis_value_formatter: Option<Box<dyn Fn(f64) -> String>>,
}
impl Default for Config {
fn default() -> Self {
Config {
width: 0,
height: 0,
lower_bound: None,
upper_bound: None,
offset: 3,
caption: String::new(),
precision: None,
caption_color: AnsiColor::DEFAULT,
axis_color: AnsiColor::DEFAULT,
label_color: AnsiColor::DEFAULT,
series_colors: Vec::new(),
series_legends: Vec::new(),
line_ending: String::from("\n"),
series_chars: Vec::new(),
x_axis_tick_count: 0,
x_axis_range: None,
x_axis_value_formatter: None,
y_axis_value_formatter: None,
}
}
}
impl Config {
pub fn width(mut self, w: usize) -> Self {
self.width = w;
self
}
pub fn height(mut self, h: usize) -> Self {
self.height = h;
self
}
pub fn lower_bound(mut self, min: f64) -> Self {
self.lower_bound = Some(min);
self
}
pub fn upper_bound(mut self, max: f64) -> Self {
self.upper_bound = Some(max);
self
}
pub fn offset(mut self, off: usize) -> Self {
self.offset = off;
self
}
pub fn precision(mut self, p: usize) -> Self {
self.precision = Some(p);
self
}
pub fn caption(mut self, c: &str) -> Self {
self.caption = c.trim().to_string();
self
}
pub fn caption_color(mut self, color: AnsiColor) -> Self {
self.caption_color = color;
self
}
pub fn axis_color(mut self, color: AnsiColor) -> Self {
self.axis_color = color;
self
}
pub fn label_color(mut self, color: AnsiColor) -> Self {
self.label_color = color;
self
}
pub fn series_colors(mut self, colors: &[AnsiColor]) -> Self {
self.series_colors = colors.to_vec();
self
}
pub fn series_legends(mut self, text: &[&str]) -> Self {
self.series_legends = text.iter().map(|s| s.to_string()).collect();
self
}
pub fn line_ending(mut self, ending: &str) -> Self {
self.line_ending = ending.to_string();
self
}
pub fn series_chars(mut self, cs: &[CharSet]) -> Self {
self.series_chars = cs.to_vec();
self
}
pub fn x_axis_tick_count(mut self, count: usize) -> Self {
if count >= 2 {
self.x_axis_tick_count = count;
}
self
}
pub fn x_axis_range(mut self, min: f64, max: f64) -> Self {
self.x_axis_range = Some([min, max]);
self
}
pub fn x_axis_value_formatter(mut self, formatter: Box<dyn Fn(f64) -> String>) -> Self {
self.x_axis_value_formatter = Some(formatter);
self
}
pub fn y_axis_value_formatter(mut self, formatter: Box<dyn Fn(f64) -> String>) -> Self {
self.y_axis_value_formatter = Some(formatter);
self
}
}