From cbbea4dcd8a8d5445461c2723019fae099936c4a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 9 Dec 2024 12:28:30 +0100 Subject: [PATCH] More cli work --- .gitignore | 2 ++ Cargo.toml | 1 + src/cli.rs | 16 +++++++--------- src/dispatch.rs | 19 +++++++++++++------ src/projector/mod.rs | 10 ++++++---- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 679f4b7..8e2aae8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ tarpaulin-report.html test/ .ignore *.json +config/ +vp/ diff --git a/Cargo.toml b/Cargo.toml index fc2092c..d5ea2ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ enum-map = { version = "2.7.3", features = ["serde"] } colog = "1.3.0" env_logger = "0.11.5" log = { version = "0.4.22", features = ["serde", "std", "release_max_level_debug", "max_level_debug"] } +rand = { version = "0.8.5", features = ["small_rng"] } [[bin]] name = "rusty_midicommander" diff --git a/src/cli.rs b/src/cli.rs index 1c4c6c9..07c094a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -48,9 +48,9 @@ pub struct CLIArgs { /// Create a config file from all the given arguments #[arg(short, long)] pub save: Option, - // // /// Connect to a VP - // pub vp: Option>, + // #[command(subcommand)] + // pub vp: Vec, } /// All arguments for creating a VP configuration @@ -58,14 +58,12 @@ pub struct CLIArgs { pub struct CreateArgs { /// Create a projector config file of a specific type #[command(subcommand)] - pub create: Option, -} + pub create: VPType, -// impl From for VPType { -// fn from(value: OsStr) -> Self { -// todo!() -// } -// } + /// The name to save it under + #[arg(short, long)] + pub name: Option, +} /// Get a parsed Args from the Cli, this function will also perform the basic (save/load) and printing needed pub fn get_arguments_from_cli() -> Result { diff --git a/src/dispatch.rs b/src/dispatch.rs index 5ac1194..a04e128 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -3,10 +3,13 @@ //! Contains all functionnalities the CLI expects use std::{ + fs::create_dir, process::exit, sync::{Arc, Mutex}, }; +use rand::{rngs::SmallRng, Rng, SeedableRng}; + use crate::{ cli::{self, CreateArgs}, midi::{self, cleanup, connect_loop_with_callback, create_output_interface}, @@ -16,24 +19,28 @@ use crate::{ /// Execute all config creation action pub fn create_config(create_args: CreateArgs) -> Result<(), RustyMidiError> { - create_args.create.into_iter().for_each(|vp| { - let _ = new_default_projector_of_type(&vp) - .inspect_err(|e| println!("Couldn't save the projector {:?} {}", vp, e)); - }); + let mut small_rng = SmallRng::from_entropy(); + let _ = create_dir("vp"); + let vp = create_args.create; + let name = create_args.name.unwrap_or(format!( + "vp/{:?}_new_{}.json", + vp, + small_rng.gen_range(255..2048), + )); + let _ = new_default_projector_of_type(&vp, &name) + .inspect_err(|e| println!("Couldn't save the projector {:?} {}", vp, e)); Ok(()) } // TODO: Upgrade /// Launch the program as a cli pub fn cli_launch(cliargs: cli::CLIArgs) -> Result<(), RustyMidiError> { - println!("{:?}", cliargs); // TODO: Cleanup all the following if cliargs.midi_interface.is_none() { midi::print_midi_interfaces(); exit(1); } - println!("{:?}", cliargs); let proj = Arc::new(Mutex::new(projector::panasonic::PanasonicProjector::new( "127.0.0.1:8080", "admin1", diff --git a/src/projector/mod.rs b/src/projector/mod.rs index 103a4db..1005df6 100644 --- a/src/projector/mod.rs +++ b/src/projector/mod.rs @@ -11,16 +11,18 @@ use serde::{Deserialize, Serialize}; /// Enumerate all the VP types #[derive(Parser, Serialize, Deserialize, Clone, Debug)] pub enum VPType { + /// A panasonic VideoProjector Panasonic, } /// Create a new empty projector of the given type /// /// While inelegant, this is the most straightforward way of having a generic projector -pub fn new_default_projector_of_type(vptype: &VPType) -> Result<(), crate::RustyMidiError> { +pub fn new_default_projector_of_type( + vptype: &VPType, + name: &str, +) -> Result<(), crate::RustyMidiError> { match vptype { - VPType::Panasonic => { - PanasonicProjector::default().create_default_config_file("new_panasonic.json") - } + VPType::Panasonic => PanasonicProjector::default().create_default_config_file(name), } } -- 2.45.1