Adds a custom config and finishes the graph arc

This commit is contained in:
Dandaedre 2024-06-25 05:26:19 +02:00
parent 0f0ec8c8f0
commit 53d8ab77dd
3 changed files with 151 additions and 35 deletions

View File

@ -21,7 +21,9 @@ logos = "0.14.0"
phf = { version = "0.11.2", features = ["macros"] }
regex = "1.10.5"
regex_static = "0.1.1"
ron = "0.8.1"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
tokio = { version = "1.38.0", features = ["full"] }
toml = "0.8.14"

View File

@ -1,8 +1,10 @@
//! Export graphs
use charts_rs::{get_or_try_init_fonts, RadarChart, RadarIndicator, Series};
use charts_rs::{Box, Color, THEME_ANT};
use charts_rs::{Box, Color};
use glob::glob;
use serde::{Deserialize, Serialize};
use std::io::Read;
use std::process::Command;
use std::{f32, fs::File, io::Write, iter, sync::LazyLock};
@ -26,6 +28,10 @@ pub(super) struct GraphConfig {
/// Alternative destination
#[arg(short, long, default_value = "Images/Radar")]
destination: String,
/// Alternative config to load (generate a new one with init)
#[arg(short, long)]
config: Option<String>,
}
#[derive(Debug, Clone)]
@ -35,6 +41,8 @@ pub(super) enum GraphTargetType {
Generate,
/// Clean all graphs
Remove,
/// Initialize the connfig
Init,
}
/// Main action dispatch for the graph make
@ -43,9 +51,36 @@ pub(super) fn make_graphs(args: &GraphConfig, _config: &Config) -> Result<()> {
match args.target {
GraphTargetType::Generate => gen_graph(args),
GraphTargetType::Remove => del_graphs(args),
GraphTargetType::Init => init_graph_config(args),
}
}
/// Initialize the config on the main directory
fn init_graph_config(_args: &GraphConfig) -> Result<()> {
let mut f = File::create("graph_config.ron")?;
let theme = MiniTheme::new();
f.write_all(
ron::ser::to_string_pretty(&theme, ron::ser::PrettyConfig::default())
.context("Converting to toml")?
.as_bytes(),
)
.context("Writing to file")?;
Ok(())
}
/// Load the config if given of the default config if none
fn load_graph_config(args: &GraphConfig) -> Result<MiniTheme> {
if let Some(c) = &args.config {
println!("[INFO] Reading config {}", &c);
let mut f = File::open(c).context("Opening config")?;
let mut buf = String::new();
f.read_to_string(&mut buf).context("Reading the config")?;
Ok(ron::from_str(buf.as_str()).context("Deserializing config")?)
} else {
println!("[INFO] Loading default config");
Ok(MiniTheme::new())
}
}
/// Delete the existing graphs
fn del_graphs(_args: &GraphConfig) -> Result<()> {
let mut cmd = Command::new("rm");
@ -72,7 +107,6 @@ fn del_graphs(_args: &GraphConfig) -> Result<()> {
/// Generate the graph and save it
fn gen_graph(args: &GraphConfig) -> Result<()> {
// Import CSV
let data = include_bytes!("../data/lmroman8-regular.ttf") as &[u8];
get_or_try_init_fonts(Some(vec![data])).unwrap();
@ -80,6 +114,8 @@ fn gen_graph(args: &GraphConfig) -> Result<()> {
let maxv = &csv.max;
let cfg = load_graph_config(args)?;
for graph in csv.values {
// println!("{:?} {:#?}", maxv, graph);
// let path = format!("{}/{}.png", args.destination, &graph.name);
@ -88,31 +124,9 @@ fn gen_graph(args: &GraphConfig) -> Result<()> {
println!("[INFO] Processing {}", path_svg);
let mut f = File::create(&path_svg)?;
let mut r = RadarChart::new_with_theme([graph].to_vec(), maxv.to_vec(), THEME_ANT);
r.legend_show = Some(false);
r.margin = Box {
left: -90.0,
top: -20.0,
right: -90.0,
bottom: -30.0,
};
r.width = 800.0;
r.height = 600.0;
r.series_label_font_size = 20.0;
let mut r = RadarChart::new([graph].to_vec(), maxv.to_vec());
cfg.apply_to(&mut r);
// r.grid_stroke_color = vec![Color{ r: 75,g: 114, b: 110, a: 0 }];
r.series_colors = vec![Color {
r: 0,
g: 127,
b: 142,
a: 255,
}];
// r.series_colors = vec![Color{ r: 140,g:170, b:161, a: 255}];
// r.series_colors = vec![Color{ r:150,g:251, b:199, a: 255 }];
r.font_family = "LMRoman8".to_string();
// f.write_all(&charts_rs::svg_to_png(r.svg().context("Converting to svg")?.as_str())?)?;
f.write_all(r.svg().context("Converting to SVG")?.as_bytes())?;
let mut cmd = Command::new("convert");
@ -218,6 +232,7 @@ static STR_TO_GRAPH_TARGET_TYPE: phf::Map<&'static str, GraphTargetType> = phf_m
"gen" => GraphTargetType::Generate,
"remove" => GraphTargetType::Remove,
"del" => GraphTargetType::Remove,
"init" => GraphTargetType::Init,
};
/// Help listing all the actions availables
@ -241,3 +256,102 @@ impl From<&str> for GraphTargetType {
}
}
}
impl MiniTheme {
/// The default theme for out tool
pub fn new() -> Self {
Self {
font_family: "LMRoman8".to_string(),
margin: Box {
left: -10.0,
top: 20.0,
right: 70.0,
bottom: 0.0,
},
width: 720.0,
height: 440.0,
grid_stroke_color: Color {
r: 77,
g: 69,
b: 57,
a: 255,
},
grid_stroke_width: 2.5,
series_stroke_width: 5.0,
series_label_font_size: 60.0,
series_label_font_color: Color {
r: 75,
g: 61,
b: 68,
a: 255,
},
series_colors: [Color {
r: 179,
g: 165,
b: 85,
a: 255,
}]
.to_vec(),
legend_show: Some(false),
}
}
/// Apply our super custom theme to the graph
pub(super) fn apply_to(&self, r: &mut RadarChart) {
r.font_family = self.font_family.clone();
r.margin = self.margin.clone();
r.width = self.width;
r.height = self.height;
r.grid_stroke_color = self.grid_stroke_color;
r.grid_stroke_width = self.grid_stroke_width;
r.series_stroke_width = self.series_stroke_width;
r.series_label_font_size = self.series_label_font_size;
r.series_label_font_color = self.series_label_font_color;
r.series_colors = self.series_colors.clone();
r.legend_show = self.legend_show;
}
}
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
#[derive(Serialize, Deserialize)]
pub struct MiniTheme {
pub font_family: String,
pub margin: Box,
pub width: f32,
pub height: f32,
pub legend_show: Option<bool>,
// pub background_color: Color,
pub grid_stroke_color: Color,
pub grid_stroke_width: f32,
pub series_stroke_width: f32,
pub series_label_font_size: f32,
pub series_label_font_color: Color,
pub series_colors: Vec<Color>,
// pub title_font_size: f32,
// pub title_font_color: Color,
// pub title_font_weight: Option<String>,
// pub title_margin: Option<Box>,
// pub title_align: Align,
// pub title_height: f32,
// pub sub_title_font_size: f32,
// pub sub_title_font_color: Color,
// pub sub_title_margin: Option<Box>,
// pub sub_title_align: Align,
// pub sub_title_height: f32,
// pub legend_font_size: f32,
// pub legend_font_color: Color,
// pub legend_align: Align,
// pub legend_margin: Option<Box>,
// pub x_axis_font_size: f32,
// pub x_axis_stroke_color: Color,
// pub x_axis_font_color: Color,
// pub x_axis_name_gap: f32,
// pub x_axis_height: f32,
// pub y_axis_font_size: f32,
// pub y_axis_font_color: Color,
// pub y_axis_stroke_color: Color,
// pub y_axis_split_number: usize,
// pub y_axis_name_gap: f32,
// pub table_header_color: Color,
// pub table_body_colors: Vec<Color>,
// pub table_border_color: Color,
}

View File

@ -2,7 +2,7 @@
use std::time::{Duration, Instant};
// use std::str::pattern::Pattern;
use std::{cmp::min, fmt::Write};
use std::fmt::Write;
use dynfmt::FormatArgs;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
@ -61,9 +61,9 @@ pub(super) struct MakeConfig {
/// Wether to add the initial and final context
#[arg(short = 'C', long, action = clap::ArgAction::SetFalse)]
pub(super) no_context: bool,
/// Activate shell escape
#[arg(short , long)]
#[arg(short, long)]
pub(super) shell_escape: bool,
}
@ -167,16 +167,16 @@ async fn launch_subprocess(
loop {
let res = reader.next_segment().await;
// let new = min(built + 1, to_build);
let new = if built < to_build{
built+1
} else{
to_build +=1;
let new = if built < to_build {
built + 1
} else {
to_build += 1;
pb.set_length(to_build);
built+1
built + 1
};
built = new;
pb.set_position(new);
current_run.lines_parsed += 1;
if let Err(e) = res {
return Err(anyhow!("[CORE] Runtime error : {}", e));