Skip to content

Commit

Permalink
lib/loop: Add command-line args
Browse files Browse the repository at this point in the history
  • Loading branch information
zphixon committed Sep 18, 2022
1 parent b83100b commit a940b24
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 26 deletions.
53 changes: 53 additions & 0 deletions Cargo.lock

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

36 changes: 31 additions & 5 deletions derive-loop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ pub fn pmb_loop(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

quote::quote! {
fn #loop_name() {
use powdermilk_biscuits::gumdrop::Options;
let args = powdermilk_biscuits::Args::parse_args_default_or_exit();

if args.version {
println!(
"Powdermilk Biscuits ({} {}, file format version {})",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
powdermilk_biscuits::migrate::Version::CURRENT,
);
return;
}

if cfg!(unix) {
let var = std::env::var("WINIT_UNIX_BACKEND");
match var.as_ref().map(|s| s.as_str()) {
Expand All @@ -201,7 +214,20 @@ pub fn pmb_loop(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
}
}

let mut config = Config::from_disk();
let config_path = if let Some(config_path) = args.config {
config_path
} else {
if cfg!(feature = "pmb-release") {
let mut dir = powdermilk_biscuits::dirs::config_dir().unwrap();
dir.push("powdermilk-biscuits");
dir.push("config.ron");
dir
} else {
std::path::PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../config.ron"))
}
};

let mut config = Config::from_disk(&config_path);
let mut builder = WindowBuilder::new()
.with_maximized(config.window_maximized)
.with_title(powdermilk_biscuits::TITLE_UNMODIFIED);
Expand All @@ -223,7 +249,7 @@ pub fn pmb_loop(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Ui::<#backend_crate_name::#coords_name>::new(width, height)
};
let mut sketch: Sketch<#backend_crate_name::#stroke_backend_name> =
if let Some(filename) = std::env::args().nth(1) {
if let Some(filename) = args.file {
Sketch::with_filename(&mut ui, std::path::PathBuf::from(filename))
} else {
Sketch::default()
Expand Down Expand Up @@ -269,11 +295,11 @@ pub fn pmb_loop(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
.unwrap_or(false)
{
flow.set_exit();
config.save();
config.save(&config_path);
}
} else {
flow.set_exit();
config.save();
config.save(&config_path);
}
}

Expand All @@ -291,7 +317,7 @@ pub fn pmb_loop(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
..
} => {
flow.set_exit();
config.save();
config.save(&config_path);
}

#event_enum_name::WindowEvent {
Expand Down
2 changes: 2 additions & 0 deletions pmb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ lyon = { git = 'https://github.com/zphixon/lyon', branch = 'new-euclid' }
serde = { version = '1.0.144', features = ['derive'] }
ron = '0.8.0'
slotmap = '1.0.6'
gumdrop = '0.8.1'
dirs = '4.0.0'
49 changes: 28 additions & 21 deletions pmb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ use crate::{
graphics::{Color, ColorExt, PixelPos, StrokePoint, StrokePos},
stroke::{Stroke, StrokeElement},
};
pub use dirs;
use event::Combination;
pub use gumdrop;
use lyon::lyon_tessellation::{StrokeOptions, StrokeTessellator};
use slotmap::{DefaultKey, SlotMap};
use std::path::{Path, PathBuf};

pub const TITLE_UNMODIFIED: &str = "hi! <3";
pub const TITLE_MODIFIED: &str = "hi! <3 (modified)";
Expand Down Expand Up @@ -82,6 +85,18 @@ pub trait StrokeBackend: std::fmt::Debug {
fn is_dirty(&self) -> bool;
}

#[derive(gumdrop::Options, Debug)]
pub struct Args {
#[options(help = "Show this message")]
help: bool,
#[options(help = "Print the version", short = "V")]
pub version: bool,
#[options(help = "Config file location")]
pub config: Option<PathBuf>,
#[options(free, help = "File to open")]
pub file: Option<PathBuf>,
}

#[derive(Default, PartialEq, Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub enum Tool {
#[default]
Expand Down Expand Up @@ -138,14 +153,12 @@ pub struct Config {
}

impl Default for Config {
#[cfg(feature = "pmb-release")]
fn default() -> Self {
Self::new()
}

#[cfg(not(feature = "pmb-release"))]
fn default() -> Self {
Self::debug()
if cfg!(feature = "pmb-release") {
Self::new()
} else {
Self::debug()
}
}
}

Expand Down Expand Up @@ -211,19 +224,10 @@ impl Config {
}
}

#[cfg(not(feature = "pmb-release"))]
fn path() -> &'static str {
concat!(env!("CARGO_MANIFEST_DIR"), "/../config.ron")
}

#[cfg(feature = "pmb-release")]
fn path() -> &'static str {
todo!()
}

// TODO registry/gsettings or something, this is dumb
pub fn from_disk() -> Config {
let file = match std::fs::read_to_string(Self::path()) {
pub fn from_disk(path: &Path) -> Config {
log::info!("load config from {}", path.display());
let file = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
return Config::default();
Expand All @@ -243,9 +247,12 @@ impl Config {
}
}

pub fn save(&self) {
pub fn save(&self, path: &Path) {
log::info!("save config to {}", path.display());

if self.had_error_parsing {
// don't overwrite broken configs
log::error!("had error");
return;
}

Expand All @@ -261,7 +268,7 @@ impl Config {
let contents =
format!("// this file generated automatically.\n// do not edit while pmb is running!!\n{contents}");

match std::fs::write(Self::path(), contents) {
match std::fs::write(path, contents) {
Err(err) => {
PmbError::from(err).display_with(String::from("Couldn't read config file"));
}
Expand Down

0 comments on commit a940b24

Please sign in to comment.