use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Shell};
const KEYBINDINGS_HELP: &str = "\
Keybindings (inside an attached session):
Ctrl+a d Detach from the session (leave it running)
Ctrl+a k Kill the session
Ctrl+a Ctrl+a Send a literal Ctrl+a to the program";
const TOP_LEVEL_HELP: &str = "\
Shortcuts:
keep-running list running sessions
keep-running <name> attach to a session (prefix match works)
Keybindings (inside an attached session):
Ctrl+a d Detach from the session (leave it running)
Ctrl+a k Kill the session
Ctrl+a Ctrl+a Send a literal Ctrl+a to the program";
#[derive(Parser)]
#[command(name = "keep-running")]
#[command(about = "Human-friendly terminal session manager with dtach-style detach")]
#[command(after_help = TOP_LEVEL_HELP)]
#[command(after_long_help = TOP_LEVEL_HELP)]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
#[arg(value_name = "SESSION")]
pub session: Option<String>,
}
#[derive(Subcommand)]
pub enum Commands {
#[command(trailing_var_arg = true)]
#[command(after_help = KEYBINDINGS_HELP)]
#[command(after_long_help = KEYBINDINGS_HELP)]
Run {
#[arg(short, long)]
name: Option<String>,
#[arg(required = true)]
command: Vec<String>,
},
Shell {
#[arg(short, long)]
name: Option<String>,
},
Attach {
session: String,
},
#[command(alias = "ls")]
List,
Kill {
session: String,
},
Start {
#[arg(short, long)]
name: Option<String>,
#[arg(required = true, trailing_var_arg = true)]
command: Vec<String>,
},
Completions {
#[arg(value_enum)]
shell: Shell,
},
}
pub fn parse() -> Cli {
Cli::parse()
}
pub fn print_completions(shell: Shell) {
let mut cmd = Cli::command();
generate(shell, &mut cmd, "keep-running", &mut std::io::stdout());
}