From 8ab0c2eb807fc7882d82b18e7c8c8f2669a33950 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Tue, 14 Jan 2025 19:01:16 +0800 Subject: [PATCH] prettier cli --- apps/cli/src/main.rs | 80 +++++++++++++++++++++++++++++++++++------ crates/utils/src/lib.rs | 9 ----- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/apps/cli/src/main.rs b/apps/cli/src/main.rs index d5e60176..67b9ada5 100644 --- a/apps/cli/src/main.rs +++ b/apps/cli/src/main.rs @@ -1,10 +1,10 @@ use std::{path::PathBuf, sync::Arc}; use cap_editor::create_segments; -use cap_media::sources::get_target_fps; +use cap_media::sources::{get_target_fps, ScreenCaptureTarget}; use cap_project::{RecordingMeta, XY}; use cap_rendering::RenderVideoConstants; -use clap::{Parser, Subcommand}; +use clap::{Args, Parser, Subcommand, ValueEnum}; #[derive(Parser)] struct Cli { @@ -14,26 +14,60 @@ struct Cli { #[derive(Subcommand)] enum Commands { + /// Export a '.cap' project to an mp4 file Export { project_path: PathBuf, output_path: Option, }, - Record { - #[command(subcommand)] - command: Option, - }, + /// Start a recording or list available capture targets and devices + Record(RecordArgs), +} + +#[derive(Args)] +#[command(args_conflicts_with_subcommands = true)] +// #[command(flatten_help = true)] +struct RecordArgs { + #[command(subcommand)] + command: Option, + + #[command(flatten)] + args: RecordStartArgs, } #[derive(Subcommand)] enum RecordCommands { + /// List screens available for capturing Screens, + /// List windows available for capturing Windows, - Cameras, - Mics, + // Cameras, + // Mics, +} + +#[derive(Args)] +struct RecordStartArgs { + #[command(flatten)] + target: RecordTargets, + /// ID of the camera to record + #[arg(long)] + camera: Option, + /// ID of the microphone to record + #[arg(long)] + mic: Option, +} + +#[derive(Args)] +struct RecordTargets { + /// ID of the screen to capture + #[arg(long, group = "target")] + screen: Option, + /// ID of the window to capture + #[arg(long, group = "target")] + window: Option, } #[tokio::main] -async fn main() { +async fn main() -> Result<(), String> { let cli = Cli::parse(); match cli.command { @@ -92,7 +126,7 @@ async fn main() { println!("Exported video to '{}'", output_path.display()); } - Commands::Record { command } => match command { + Commands::Record(RecordArgs { command, args }) => match command { Some(RecordCommands::Screens) => { let screens = cap_media::sources::list_screens(); @@ -127,7 +161,33 @@ window {}: ); } } + None => { + let (target_info, scap_target) = args + .target + .screen + .map(|id| { + cap_media::sources::list_screens() + .into_iter() + .find(|s| s.0.id == id) + .map(|(s, t)| (ScreenCaptureTarget::Screen(s), t)) + .ok_or(format!("Screen with id '{id}' not found")) + }) + .or_else(|| { + args.target.window.map(|id| { + cap_media::sources::list_windows() + .into_iter() + .find(|s| s.0.id == id) + .map(|(s, t)| (ScreenCaptureTarget::Window(s), t)) + .ok_or(format!("Window with id '{id}' not found")) + }) + }) + .ok_or("No target specified".to_string())??; + + dbg!(target_info); + } _ => {} }, } + + Ok(()) } diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 188445e9..0e2f7094 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -33,15 +33,6 @@ pub fn format_error_message(error_code: u32) -> String { } } -#[cfg(unix)] -fn create_named_pipe(path: &std::path::Path) -> Result<(), Box> { - use nix::sys::stat; - use nix::unistd; - std::fs::remove_file(path).ok(); - unistd::mkfifo(path, stat::Mode::S_IRWXU)?; - Ok(()) -} - pub fn create_channel_named_pipe( mut rx: Receiver, pipe_path: PathBuf,