-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.rs
217 lines (180 loc) · 5.76 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#![allow(clippy::too_many_arguments)]
extern crate pretty_env_logger;
#[macro_use]
extern crate paris_log;
use std::path::PathBuf;
use material_colors::theme::ThemeBuilder;
mod helpers;
pub mod template;
mod util;
mod wallpaper;
use helpers::{set_wallpaper, setup_logging};
use matugen::{
color::color::{get_source_color, Source},
scheme::{get_custom_color_schemes, get_schemes, SchemeTypes},
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};
use clap::Parser;
use color_eyre::Report;
use matugen::scheme::{Schemes, SchemesEnum};
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,
}
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 update_themes(&mut self) {
let source_color = get_source_color(&self.args.source).unwrap();
let _theme = ThemeBuilder::with_source(source_color).build();
let (scheme_dark, scheme_light) =
get_schemes(source_color, &self.args.r#type, &self.args.contrast);
self.schemes = get_custom_color_schemes(
source_color,
scheme_dark,
scheme_light,
&self.config_file.config.custom_colors,
&self.args.r#type,
&self.args.contrast,
);
}
#[cfg(feature = "ui")]
fn run_gui(self) -> Result<(), Report> {
setup_logging(&self.args)?;
eframe::run_native(
"Matugen",
eframe::NativeOptions::default(),
Box::new(|ctx| Ok(Box::new(MyApp::new(ctx, Box::new(self.args))))),
)
.unwrap();
Ok(())
}
fn init_in_term(&self) -> Result<(), Report> {
setup_logging(&self.args)?;
#[cfg(feature = "update-informer")]
if self.config_file.config.version_check == Some(true) {
use crate::helpers::check_version;
check_version();
}
Ok(())
}
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,
);
}
if self.args.dry_run == Some(true) {
return Ok(());
}
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()?;
if let Some(_wallpaper_cfg) = &self.config_file.config.wallpaper {
set_wallpaper(&self.args.source, _wallpaper_cfg)?;
}
Ok(())
}
fn init_engine(&self) -> upon::Engine {
let syntax = build_engine_syntax(self);
upon::Engine::with_syntax(syntax)
}
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,
)
}
}
#[cfg(feature = "ui")]
mod gui;
#[cfg(feature = "ui")]
use gui::init::MyApp;
#[allow(unreachable_code)]
fn main() -> Result<(), Report> {
color_eyre::install()?;
let args_unparsed: Vec<String> = std::env::args().collect();
let default_args = Cli {
source: crate::Source::Color(matugen::color::color::ColorFormat::Hex {
string: String::from("#ffffff"),
}),
r#type: Some(SchemeTypes::SchemeContent),
config: None,
prefix: None,
contrast: Some(0.0),
verbose: Some(true),
quiet: None,
debug: Some(true),
mode: Some(SchemesEnum::Dark),
dry_run: None,
show_colors: None,
json: None,
};
if args_unparsed.len() > 1 && args_unparsed[1] == "ui" {
#[cfg(feature = "ui")]
{
let prog = State::new(default_args);
prog.run_gui()?;
return Ok(());
}
error!("Tried to run gui mode without the <red>--ui</> compilation flag")
} else {
let args = Cli::parse();
let prog = State::new(args.clone());
prog.run_in_term()?;
}
Ok(())
}