Skip to content

Commit

Permalink
wip: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Dec 10, 2024
1 parent 8384ba0 commit 05e7cea
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 190 deletions.
8 changes: 4 additions & 4 deletions src/color/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use std::str::FromStr;

use crate::{color::math::get_color_distance_lab, scheme::SchemeTypes};

#[derive(clap::Parser, Debug)]
#[derive(clap::Parser, Debug, Clone)]
pub enum ColorFormat {
Hex { string: String },
Rgb { string: String },
Hsl { string: String },
}

#[derive(clap::Subcommand, Debug)]
#[derive(clap::Subcommand, Debug, Clone)]
pub enum Source {
/// The image to use for generating a color scheme
Image { path: String },
Expand All @@ -37,13 +37,13 @@ pub enum Source {
Color(crate::color::color::ColorFormat),
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct ColorDefinition {
pub name: String,
pub color: String,
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum OwnCustomColor {
Color(String),
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn setup_logging(args: &Cli) -> Result<(), Report> {
Ok(())
}

pub fn set_wallpaper(source: &Source, wallpaper_cfg: wallpaper::Wallpaper) -> Result<(), Report> {
pub fn set_wallpaper(source: &Source) -> Result<(), Report> {
let path = match &source {
Source::Image { path } => path,
Source::Color { .. } => return Ok(()),
Expand Down
170 changes: 122 additions & 48 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
extern crate pretty_env_logger;
#[macro_use]
extern crate paris_log;
use std::path::PathBuf;

use material_colors::theme::ThemeBuilder;

mod helpers;
Expand All @@ -12,9 +14,11 @@ mod wallpaper;

use helpers::{set_wallpaper, setup_logging};
use matugen::{
color::color::get_source_color,
color::color::{get_source_color, Source},
scheme::{get_custom_color_schemes, get_schemes},
template_util::template::get_render_data,
};
use template::{build_engine_syntax, TemplateFile};

use crate::template::Template;
use crate::util::{arguments::Cli, color::show_color, config::ConfigFile};
Expand All @@ -24,66 +28,136 @@ use color_eyre::{eyre::Result, Report};

use matugen::scheme::{Schemes, SchemesEnum};

fn main() -> Result<(), Report> {
color_eyre::install()?;
let args = Cli::parse();
use material_colors::{color::Argb, theme::Theme};
use upon::Value;
pub struct State {
pub args: Cli,
pub config_file: ConfigFile,
pub config_path: Option<PathBuf>,
pub source_color: Argb,
pub theme: Theme,
pub schemes: Schemes,
pub default_scheme: SchemesEnum,
}

setup_logging(&args)?;
impl State {
pub fn new(args: Cli) -> Self {
let (config_file, config_path) = ConfigFile::read(&args).unwrap();

let source_color = get_source_color(&args.source).unwrap();
let theme = ThemeBuilder::with_source(source_color).build();
let (scheme_dark, scheme_light) = get_schemes(source_color, &args.r#type, &args.contrast);

let default_scheme = args
.mode
.expect("Something went wrong while parsing the mode");

let schemes = get_custom_color_schemes(
source_color,
scheme_dark,
scheme_light,
&config_file.config.custom_colors,
&args.r#type,
&args.contrast,
);

Self {
args,
config_file,
config_path,
source_color,
theme,
schemes,
default_scheme,
}
}

fn init_in_term(&self) -> Result<(), Report> {
color_eyre::install()?;
setup_logging(&self.args)?;

let (config, config_path) = ConfigFile::read(&args)?;
#[cfg(feature = "update-informer")]
if self.config_file.config.version_check == Some(true) {
use crate::helpers::check_version;
check_version();
}

#[cfg(feature = "update-informer")]
if config.config.version_check == Some(true) {
use crate::helpers::check_version;
check_version();
Ok(())
}


let source_color = get_source_color(&args.source).unwrap();

let theme = ThemeBuilder::with_source(source_color).build();
pub fn run_in_term(&self) -> Result<(), Report> {
self.init_in_term()?;

if self.args.show_colors == Some(true) {
show_color(&self.schemes, &self.source_color);
}

#[cfg(feature = "dump-json")]
if let Some(ref format) = self.args.json {
use crate::util::color::dump_json;
dump_json(
&self.schemes,
&self.source_color,
format,
&self.theme.palettes,
);
}

let (scheme_dark, scheme_light) = get_schemes(source_color, &args.r#type, &args.contrast);
if self.args.dry_run == Some(true) {
return Ok(());
}

let default_scheme = args
.mode
.expect("Something went wrong while parsing the mode");
let mut engine = self.init_engine();
let mut render_data = self.init_render_data()?;
let mut template = TemplateFile::new(self, &mut engine, &mut render_data);

template.generate()?;

// Template::generate(
// &self.schemes,
// &self.config_file.templates,
// &self.args.source,
// &self.source_color,
// &self.default_scheme,
// &self.config_file.config.custom_keywords,
// &self.args.prefix,
// &self.config_path,
// )?;

if let Some(_wallpaper_cfg) = &self.config_file.config.wallpaper {
set_wallpaper(&self.args.source)?;
}

let schemes = get_custom_color_schemes(
source_color,
scheme_dark,
scheme_light,
&config.config.custom_colors,
&args.r#type,
&args.contrast,
);
Ok(())
}

if args.show_colors == Some(true) {
show_color(&schemes, &source_color);
fn init_engine(&self) -> upon::Engine {
let syntax = build_engine_syntax(self);
upon::Engine::with_syntax(syntax)
}

#[cfg(feature = "dump-json")]
if let Some(ref format) = args.json {
use crate::util::color::dump_json;
dump_json(&schemes, &source_color, format, theme.palettes);
fn init_render_data(&self) -> Result<Value, Report> {
let image = match &self.args.source {
Source::Image { path } => Some(path),
#[cfg(feature = "web-image")]
Source::WebImage { .. } => None,
Source::Color { .. } => None,
};

get_render_data(
&self.schemes,
&self.source_color,
&self.default_scheme,
&self.config_file.config.custom_keywords,
image,
)
}
}

if args.dry_run == Some(false) {
Template::generate(
&schemes,
&config.templates,
&args.source,
&source_color,
&default_scheme,
&config.config.custom_keywords,
&args.prefix,
config_path,
)?;
fn main() -> Result<(), Report> {
let args = Cli::parse();

if let Some(wallpaper_cfg) = config.config.wallpaper {
set_wallpaper(&args.source, wallpaper_cfg)?;
}
}
let prog = State::new(args.clone());

Ok(())
prog.run_in_term()
}
Loading

0 comments on commit 05e7cea

Please sign in to comment.