Browse Source

actually take command-line args

Getty Ritter 2 years ago
parent
commit
b7575389d6
3 changed files with 58 additions and 5 deletions
  1. 21 0
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 36 4
      src/main.rs

+ 21 - 0
Cargo.lock

@@ -90,13 +90,28 @@ checksum = "7a30c3bf9ff12dfe5dae53f0a96e0febcd18420d1c0e7fad77796d9d5c4b5375"
 dependencies = [
 dependencies = [
  "atty",
  "atty",
  "bitflags",
  "bitflags",
+ "clap_derive",
  "indexmap",
  "indexmap",
+ "lazy_static",
  "os_str_bytes",
  "os_str_bytes",
  "strsim",
  "strsim",
  "termcolor",
  "termcolor",
  "textwrap",
  "textwrap",
 ]
 ]
 
 
+[[package]]
+name = "clap_derive"
+version = "3.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "517358c28fcef6607bf6f76108e02afad7e82297d132a6b846dcc1fc3efcd153"
+dependencies = [
+ "heck 0.4.0",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 [[package]]
 name = "crc32fast"
 name = "crc32fast"
 version = "1.3.0"
 version = "1.3.0"
@@ -320,6 +335,12 @@ dependencies = [
  "hashbrown",
  "hashbrown",
 ]
 ]
 
 
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
 [[package]]
 [[package]]
 name = "libc"
 name = "libc"
 version = "0.2.113"
 version = "0.2.113"

+ 1 - 1
Cargo.toml

@@ -12,4 +12,4 @@ pango = "*"
 pangocairo = "*"
 pangocairo = "*"
 serde = { version = "*", features = ["derive"] }
 serde = { version = "*", features = ["derive"] }
 serde_yaml = "*"
 serde_yaml = "*"
-clap = "*"
+clap = { version = "*", features = ["derive"] }

+ 36 - 4
src/main.rs

@@ -1,5 +1,6 @@
 use std::fs::File;
 use std::fs::File;
 
 
+use clap::Parser;
 use serde::Deserialize;
 use serde::Deserialize;
 
 
 type Pixel = (u8, u8, u8);
 type Pixel = (u8, u8, u8);
@@ -126,16 +127,47 @@ impl Default for Config {
     }
     }
 }
 }
 
 
+#[derive(Parser, Debug)]
+#[clap(author, version, about, long_about = None)]
+struct Options {
+    image: String,
+    mapping: String,
+    output: Option<String>,
+
+    #[clap(long, default_value_t = 10)]
+    grid: u32,
+
+    #[clap(long, default_value_t = 1.0)]
+    line_weight: f64,
+
+    #[clap(long, default_value_t = 3.0)]
+    major_line_weight: f64,
+
+    #[clap(long, default_value_t = 24.0)]
+    size: f64,
+
+    #[clap(long, default_value_t = String::from("Fira Sans 12"))]
+    font: String,
+}
+
 fn main() -> Result<(), Box<dyn std::error::Error>> {
 fn main() -> Result<(), Box<dyn std::error::Error>> {
-    let img = Image::load("sample.png")?;
-    let mapping = Mapping::load("colors.yaml")?;
+    let opts = Options::parse();
+
+    let img = Image::load(opts.image)?;
+    let mapping = Mapping::load(opts.mapping)?;
 
 
-    let config = Config::default();
+    let config = Config {
+        grid_every: opts.grid,
+        line_weight: opts.line_weight,
+        major_line_weight: opts.major_line_weight,
+        grid_size: opts.size,
+        font: opts.font.clone(),
+    };
 
 
     let surf = cairo::SvgSurface::new(
     let surf = cairo::SvgSurface::new(
         config.scale(img.width),
         config.scale(img.width),
         config.scale(img.height),
         config.scale(img.height),
-        Some("output.svg"),
+        Some(opts.output.unwrap_or_else(|| "output.svg".to_string())),
     )?;
     )?;
 
 
     let ctx = cairo::Context::new(&surf)?;
     let ctx = cairo::Context::new(&surf)?;