Skip to content

Commit

Permalink
Merge pull request #18 from donovanglover/feat/man-pages-shell-comple…
Browse files Browse the repository at this point in the history
…tions

feat: add man pages and shell completions
  • Loading branch information
chaosprint authored Jul 30, 2024
2 parents 566e247 + cf4747c commit 2b15066
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 68 deletions.
49 changes: 41 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ rand = "0.8.5"
ratatui = "0.26.1"
smallvec = "1.13.1"

[build-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
clap_complete = "4.5.2"
clap_mangen = "0.2.20"

[features]
default = ["cpal"]
jack = ["cpal/jack"]
Expand Down
49 changes: 49 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
include!("src/cli.rs");

use clap::Command;
use clap::CommandFactory;
use clap_complete::generate_to;
use clap_complete::Shell::{Bash, Fish, Zsh};
use clap_mangen::Man;
use std::fs;
use std::path::PathBuf;

static NAME: &str = "asak";

fn generate_man_pages(cmd: Command) {
let man_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/man");
let mut buffer = Vec::default();

Man::new(cmd.clone()).render(&mut buffer).unwrap();
fs::create_dir_all(&man_dir).unwrap();
fs::write(man_dir.join(NAME.to_owned() + ".1"), buffer).unwrap();

for subcommand in cmd.get_subcommands() {
let mut buffer = Vec::default();

Man::new(subcommand.clone()).render(&mut buffer).unwrap();
fs::write(
man_dir.join(NAME.to_owned() + "-" + subcommand.get_name() + ".1"),
buffer,
)
.unwrap();
}
}

fn generate_shell_completions(mut cmd: Command) {
let comp_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/completions");

fs::create_dir_all(&comp_dir).unwrap();

for shell in [Bash, Fish, Zsh] {
generate_to(shell, &mut cmd, NAME, &comp_dir).unwrap();
}
}

fn main() {
let mut cmd = Cli::command();
cmd.set_bin_name(NAME);

generate_man_pages(cmd.clone());
generate_shell_completions(cmd);
}
61 changes: 61 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use clap::{Args, Parser, Subcommand};

/// Audio Swiss Army knife written in Rust. Like Sox but interactive with TUI.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,

/// The audio device to use
#[arg(short, long, default_value_t = String::from("default"))]
pub device: String,

/// Use the JACK host
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
))]
#[arg(short, long)]
#[allow(dead_code)]
pub jack: bool,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
/// Record an audio file
Rec(RecArgs),
/// Play an audio file
Play(PlayArgs),
/// Monitor audio input with scopes
Monitor(MonitorArgs),
}

/// Arguments used for the `rec` command
#[derive(Args, Debug)]
pub struct RecArgs {
/// Path for the output audio file, e.g. `output`
#[arg(required = false)]
pub output: Option<String>,
}

/// Arguments used for the `play` command
#[derive(Args, Debug)]
pub struct PlayArgs {
/// Path to the audio file to play; must be wav format for now, e.g. `input.wav`
#[arg(required = false)]
pub input: Option<String>,
}

/// Arguments used for the `monitor` command
#[derive(Args, Debug)]
pub struct MonitorArgs {
/// Buffer size for the audio input monitoring, defaults to 1024, the higher the value the more latency
#[arg(required = false, short, long)]
pub buffer_size: Option<usize>,
}
63 changes: 3 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Args, Parser, Subcommand};
use clap::Parser;

mod record;
use inquire::{InquireError, Select, Text};
Expand All @@ -10,65 +10,8 @@ use playback::play_audio;
mod monitor;
use monitor::start_monitoring;

/// Audio Swiss Army knife written in Rust. Like Sox but interactive with TUI.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,

/// The audio device to use
#[arg(short, long, default_value_t = String::from("default"))]
device: String,

/// Use the JACK host
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
))]
#[arg(short, long)]
#[allow(dead_code)]
jack: bool,
}

#[derive(Debug, Subcommand)]
enum Commands {
/// Record an audio file
Rec(RecArgs),
/// Play an audio file
Play(PlayArgs),
/// Monitor audio input with scopes
Monitor(MonitorArgs),
}

/// Arguments used for the `rec` command
#[derive(Args, Debug)]
struct RecArgs {
/// Path for the output audio file, e.g. `output`
#[arg(required = false)]
output: Option<String>,
}

/// Arguments used for the `play` command
#[derive(Args, Debug)]
struct PlayArgs {
/// Path to the audio file to play; must be wav format for now, e.g. `input.wav`
#[arg(required = false)]
input: Option<String>,
}

/// Arguments used for the `monitor` command
#[derive(Args, Debug)]
struct MonitorArgs {
/// Buffer size for the audio input monitoring, defaults to 1024, the higher the value the more latency
#[arg(required = false, short, long)]
buffer_size: Option<usize>,
}
mod cli;
use cli::{Cli, Commands};

fn main() {
let cli = Cli::parse();
Expand Down

0 comments on commit 2b15066

Please sign in to comment.