mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 19:26:43 +01:00
added -p -c -l -o options to uw8 run command
This commit is contained in:
62
src/main.rs
62
src/main.rs
@@ -1,22 +1,54 @@
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::process;
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
process::exit,
|
||||
};
|
||||
use std::io::prelude::*;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use uw8::MicroW8;
|
||||
use notify::{DebouncedEvent, Watcher};
|
||||
use pico_args::Arguments;
|
||||
use uw8::MicroW8;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut args = Arguments::from_env();
|
||||
|
||||
match args.subcommand()?.as_ref().map(|s| s.as_str()) {
|
||||
Some("run") => run(args),
|
||||
Some(other) => {
|
||||
eprintln!("Unknown command '{}'", other);
|
||||
process::exit(1);
|
||||
}
|
||||
None => {
|
||||
println!("Usage:");
|
||||
println!(" uw8 run [-w] [-p] [-c] [-l] [-o <out-file>] <file>");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run(mut args: Arguments) -> Result<()> {
|
||||
let watch_mode = args.contains(["-w", "--watch"]);
|
||||
|
||||
let mut config = Config::default();
|
||||
config.pack = args.contains(["-p", "--pack"]);
|
||||
if args.contains(["-c", "--compress"]) {
|
||||
config.compression = Some(2);
|
||||
}
|
||||
|
||||
if let Some(level) = args.opt_value_from_str(["-l", "--level"])? {
|
||||
config.compression = Some(level);
|
||||
}
|
||||
|
||||
if let Some(path) =
|
||||
args.opt_value_from_os_str::<_, _, bool>(["-o", "--output"], |s| Ok(s.into()))?
|
||||
{
|
||||
config.output_path = Some(path);
|
||||
}
|
||||
|
||||
let filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
||||
|
||||
let mut uw8 = MicroW8::new()?;
|
||||
@@ -28,7 +60,7 @@ fn main() -> Result<()> {
|
||||
watcher.watch(&filename, notify::RecursiveMode::NonRecursive)?;
|
||||
}
|
||||
|
||||
if let Err(err) = load_cart(&filename, &mut uw8) {
|
||||
if let Err(err) = load_cart(&filename, &mut uw8, &config) {
|
||||
eprintln!("Load error: {}", err);
|
||||
if !watch_mode {
|
||||
exit(1);
|
||||
@@ -38,7 +70,7 @@ fn main() -> Result<()> {
|
||||
while uw8.is_open() {
|
||||
match rx.try_recv() {
|
||||
Ok(DebouncedEvent::Create(_) | DebouncedEvent::Write(_)) => {
|
||||
if let Err(err) = load_cart(&filename, &mut uw8) {
|
||||
if let Err(err) = load_cart(&filename, &mut uw8, &config) {
|
||||
eprintln!("Load error: {}", err);
|
||||
}
|
||||
}
|
||||
@@ -52,7 +84,14 @@ fn main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_cart(filename: &Path, uw8: &mut MicroW8) -> Result<()> {
|
||||
#[derive(Default)]
|
||||
struct Config {
|
||||
pack: bool,
|
||||
compression: Option<u8>,
|
||||
output_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn load_cart(filename: &Path, uw8: &mut MicroW8, config: &Config) -> Result<()> {
|
||||
let mut cart = vec![];
|
||||
File::open(filename)?.read_to_end(&mut cart)?;
|
||||
|
||||
@@ -61,6 +100,19 @@ fn load_cart(filename: &Path, uw8: &mut MicroW8) -> Result<()> {
|
||||
cart = curlywas::compile_str(&src)?;
|
||||
}
|
||||
|
||||
if config.pack {
|
||||
let mut pack_config = uw8_tool::PackConfig::default();
|
||||
if let Some(level) = config.compression {
|
||||
pack_config = pack_config.with_compression_level(level);
|
||||
}
|
||||
cart = uw8_tool::pack(&cart, pack_config)?;
|
||||
println!("packed size: {} bytes", cart.len());
|
||||
}
|
||||
|
||||
if let Some(ref path) = config.output_path {
|
||||
File::create(path)?.write_all(&cart)?;
|
||||
}
|
||||
|
||||
if let Err(err) = uw8.load_from_memory(&cart) {
|
||||
eprintln!("Load error: {}", err);
|
||||
Err(err)
|
||||
|
||||
Reference in New Issue
Block a user