Skip to content

Commit

Permalink
refactor: configuration and args structs
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Jan 25, 2024
1 parent 3633a30 commit 3297a2f
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 404 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 17 additions & 144 deletions crates/sss_cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
use clap::Parser;
use merge2::{bool::overwrite_false, Merge};
use serde::{Deserialize, Serialize};
use sss_lib::font::parse_font_str;
use sss_lib::font::FontCollection;
use sss_lib::{Background, GenerationSettings, Shadow, ToRgba};
use sss_lib::{default_bool, swap_option};

use crate::{str_to_area, Area};

const fn default_bool() -> bool {
false
}

#[inline]
fn swap_option<T>(left: &mut Option<T>, right: &mut Option<T>) {
if left.is_none() || right.is_some() {
core::mem::swap(left, right);
}
#[derive(Clone, Debug, Deserialize, Merge, Parser, Serialize)]
#[clap(version, author)]
struct ClapConfig {
#[clap(flatten)]
pub cli: Option<CliConfig>,
// lib configs
#[clap(flatten)]
#[serde(rename = "general")]
pub lib_config: sss_lib::GenerationSettingsArgs,
}

#[derive(Clone, Debug, Default, Deserialize, Merge, Parser, Serialize)]
#[clap(version, author)]
#[derive(Clone, Debug, Deserialize, Merge, Parser, Serialize)]
pub struct CliConfig {
#[clap(
long,
Expand All @@ -42,103 +39,9 @@ pub struct CliConfig {
#[clap(long, help = "Captures an area of the screen", value_parser = str_to_area)]
#[merge(strategy = swap_option)]
pub area: Option<Area>,
// Screenshot Section
#[clap(
long,
help = "[default: Hack=12.0;] The font used to render, format: Font Name=size;Other Font Name=12.0",
value_parser = parse_font_str
)]
#[merge(strategy = swap_option)]
pub fonts: Option<FontCollection>,
#[clap(
long,
short,
help = "[default: #323232] Support: '#RRGGBBAA' 'h;#RRGGBBAA;#RRGGBBAA' 'v;#RRGGBBAA;#RRGGBBAA' or file path"
)]
#[merge(strategy = swap_option)]
pub background: Option<String>,
#[clap(long, short, help = "[default: 15] ")]
#[merge(strategy = swap_option)]
pub radius: Option<u32>,
#[clap(long, help = "Author Name of screenshot")]
#[merge(strategy = swap_option)]
pub author: Option<String>,
#[clap(long, help = "[default: #FFFFFF] Title bar text color")]
#[merge(strategy = swap_option)]
pub author_color: Option<String>,
#[clap(long, help = "[default: Hack] Font to render Author")]
#[merge(strategy = swap_option)]
pub author_font: Option<String>,
// Window Bar
#[clap(long, help = "Whether show the window controls")]
#[merge(strategy = overwrite_false)]
#[serde(default = "default_bool")]
pub window_controls: bool,
#[clap(long, help = "Window title")]
#[merge(strategy = swap_option)]
pub window_title: Option<String>,
#[clap(long, help = "[default: #4287f5] Window bar background")]
#[merge(strategy = swap_option)]
pub window_background: Option<String>,
#[clap(long, help = "[default: #FFFFFF] Title bar text color")]
#[merge(strategy = swap_option)]
pub window_title_color: Option<String>,
#[clap(long, help = "[default 120] Width of window controls")]
#[merge(strategy = swap_option)]
pub window_controls_width: Option<u32>,
#[clap(long, help = "[default: 40] Height of window title/controls bar")]
#[merge(strategy = swap_option)]
pub window_controls_height: Option<u32>,
#[clap(long, help = "[default: 10] Padding of title on window bar")]
#[merge(strategy = swap_option)]
pub titlebar_padding: Option<u32>,
// Padding Section
#[clap(long, help = "[default: 80]")]
#[merge(strategy = swap_option)]
pub padding_x: Option<u32>,
#[clap(long, help = "[default: 100]")]
#[merge(strategy = swap_option)]
pub padding_y: Option<u32>,
// Shadow Section
#[clap(long, help = "Enable shadow")]
#[merge(strategy = overwrite_false)]
#[serde(default = "default_bool")]
pub shadow: bool,
#[clap(long, help = "Generate shadow from inner image")]
#[merge(strategy = overwrite_false)]
#[serde(default = "default_bool")]
pub shadow_image: bool,
#[clap(
long,
help = "[default: #707070] Support: '#RRGGBBAA' 'h;#RRGGBBAA;#RRGGBBAA' 'v;#RRGGBBAA;#RRGGBBAA' or file path"
)]
#[merge(strategy = swap_option)]
pub shadow_color: Option<String>,
#[clap(long, help = "[default: 50] Shadow blur")]
#[merge(strategy = swap_option)]
pub shadow_blur: Option<f32>,
// Saving options
#[clap(long, short, help = "Send the result to your clipboard")]
#[merge(strategy = overwrite_false)]
#[serde(default = "default_bool")]
pub copy: bool,
#[clap(
long,
short,
help = "If it is set then the result will be saved here, otherwise it will not be saved."
)]
#[serde(skip)]
pub output: String,
#[clap(
long,
short = 'f',
help = "[default: png] The format in which the image will be saved"
)]
#[merge(strategy = swap_option)]
pub save_format: Option<String>,
}

pub fn get_config() -> CliConfig {
pub fn get_config() -> (CliConfig, sss_lib::GenerationSettings) {
let config_path = directories::BaseDirs::new()
.unwrap()
.config_dir()
Expand All @@ -151,42 +54,12 @@ pub fn get_config() -> CliConfig {

if let Ok(cfg_content) = std::fs::read_to_string(config_path) {
println!("Merging from config file");
let mut config: CliConfig = toml::from_str(&cfg_content).unwrap();
let mut args = CliConfig::parse();
let mut config: ClapConfig = toml::from_str(&cfg_content).unwrap();
let mut args = ClapConfig::parse();

config.merge(&mut args);
return config;
}
CliConfig::parse()
}

impl From<CliConfig> for GenerationSettings {
fn from(val: CliConfig) -> Self {
let background = Background::try_from(val.background.unwrap().clone()).unwrap();
let windows_background =
Background::try_from(val.window_background.unwrap().clone()).unwrap();
let shadow_color = Background::try_from(val.shadow_color.unwrap().clone()).unwrap();

GenerationSettings {
windows_background,
background,
padding: (val.padding_x.unwrap(), val.padding_y.unwrap()),
round_corner: val.radius,
shadow: val.shadow.then_some(Shadow {
shadow_color,
use_inner_image: val.shadow_image,
blur_radius: val.shadow_blur.unwrap(),
}),
fonts: val.fonts.unwrap_or_default(),
author: val.author.clone(),
author_font: val.author_font.clone().unwrap(),
author_color: val.author_color.unwrap().to_rgba().unwrap(),
window_controls: val.window_controls,
windows_title: val.window_title.clone(),
windows_title_color: val.window_title_color.unwrap().to_rgba().unwrap(),
window_controls_width: val.window_controls_width.unwrap(),
window_controls_height: val.window_controls_height.unwrap(),
titlebar_padding: val.titlebar_padding.unwrap(),
}
return (config.cli.unwrap(), config.lib_config.into());
}
let config = ClapConfig::parse();
(config.cli.unwrap(), config.lib_config.into())
}
28 changes: 2 additions & 26 deletions crates/sss_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use config::get_config;
use img::Screenshot;
use screenshots::image::error::{ImageFormatHint, UnsupportedError, UnsupportedErrorKind};
use screenshots::image::{ImageError, ImageFormat};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sss_lib::generate_image;

Expand All @@ -18,31 +16,9 @@ pub struct Area {
}

fn main() {
let config = get_config();
let (config, g_config) = get_config();

let img = generate_image(
config.copy,
config.clone().into(),
Screenshot {
config: config.clone(),
},
);

img.save_with_format(
&config.output,
str_to_format(config.save_format.unwrap_or("png".to_string())).unwrap(),
)
.unwrap();
println!("Saved!");
}

fn str_to_format(s: String) -> Result<ImageFormat, ImageError> {
ImageFormat::from_extension(s.clone()).ok_or(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormatHint::Name(s.to_string()),
UnsupportedErrorKind::Format(ImageFormatHint::Name(s.to_string())),
),
))
generate_image(g_config, Screenshot { config });
}

fn str_to_area(s: &str) -> Result<Area, String> {
Expand Down
Loading

0 comments on commit 3297a2f

Please sign in to comment.