Skip to content

Commit

Permalink
prettier cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Jan 14, 2025
1 parent 0c80279 commit 8ab0c2e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
80 changes: 70 additions & 10 deletions apps/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<PathBuf>,
},
Record {
#[command(subcommand)]
command: Option<RecordCommands>,
},
/// 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<RecordCommands>,

#[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<u32>,
/// ID of the microphone to record
#[arg(long)]
mic: Option<u32>,
}

#[derive(Args)]
struct RecordTargets {
/// ID of the screen to capture
#[arg(long, group = "target")]
screen: Option<u32>,
/// ID of the window to capture
#[arg(long, group = "target")]
window: Option<u32>,
}

#[tokio::main]
async fn main() {
async fn main() -> Result<(), String> {
let cli = Cli::parse();

match cli.command {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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(())
}
9 changes: 0 additions & 9 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
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<T, F>(
mut rx: Receiver<T>,
pipe_path: PathBuf,
Expand Down

1 comment on commit 8ab0c2e

@vercel
Copy link

@vercel vercel bot commented on 8ab0c2e Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.