Support ClI argument for Version and Help
This commit is contained in:
parent
d1dc537c7b
commit
4a41aa1d86
3 changed files with 69 additions and 13 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -973,6 +973,12 @@ dependencies = [
|
||||||
"inout",
|
"inout",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "0.7.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clipboard-win"
|
name = "clipboard-win"
|
||||||
version = "5.4.0"
|
version = "5.4.0"
|
||||||
|
|
@ -1497,6 +1503,7 @@ name = "cosmic-term"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"alacritty_terminal",
|
"alacritty_terminal",
|
||||||
|
"clap_lex",
|
||||||
"cosmic-files",
|
"cosmic-files",
|
||||||
"cosmic-text",
|
"cosmic-text",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ ron = "0.8"
|
||||||
serde = { version = "1", features = ["serde_derive"] }
|
serde = { version = "1", features = ["serde_derive"] }
|
||||||
shlex = "1"
|
shlex = "1"
|
||||||
tokio = { version = "1", features = ["sync"] }
|
tokio = { version = "1", features = ["sync"] }
|
||||||
|
# CLI arguments
|
||||||
|
clap_lex = "0.7"
|
||||||
# Internationalization
|
# Internationalization
|
||||||
i18n-embed = { version = "0.15", features = [
|
i18n-embed = { version = "0.15", features = [
|
||||||
"fluent-system",
|
"fluent-system",
|
||||||
|
|
|
||||||
73
src/main.rs
73
src/main.rs
|
|
@ -69,6 +69,10 @@ mod terminal_theme;
|
||||||
|
|
||||||
mod dnd;
|
mod dnd;
|
||||||
|
|
||||||
|
use clap_lex::RawArgs;
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
static ref ICON_CACHE: Mutex<IconCache> = Mutex::new(IconCache::new());
|
static ref ICON_CACHE: Mutex<IconCache> = Mutex::new(IconCache::new());
|
||||||
}
|
}
|
||||||
|
|
@ -79,21 +83,39 @@ pub fn icon_cache_get(name: &'static str, size: u16) -> widget::icon::Icon {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs application with these settings
|
/// Runs application with these settings
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[rustfmt::skip]
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let raw_args = RawArgs::from_args();
|
||||||
|
let mut cursor = raw_args.cursor();
|
||||||
|
|
||||||
|
let mut shell_program_opt = None;
|
||||||
|
let mut shell_args = Vec::new();
|
||||||
let mut daemonize = true;
|
let mut daemonize = true;
|
||||||
let mut args_iter = env::args().fuse();
|
// Parse the arguments using clap_lex
|
||||||
// more performant than an iterator adapter
|
while let Some(arg) = raw_args.next_os(&mut cursor) {
|
||||||
_ = args_iter.next();
|
match arg.to_str() {
|
||||||
for arg in args_iter.by_ref() {
|
Some("--help") | Some("-h") => {
|
||||||
match arg.as_str() {
|
print_help();
|
||||||
// These flags indicate the end of parsing flags
|
return Ok(());
|
||||||
"-e" | "--command" | "--" => {
|
}
|
||||||
|
Some("--version") | Some("-V") => {
|
||||||
|
println!(
|
||||||
|
"cosmic-term {} (git commit {})",
|
||||||
|
env!("CARGO_PKG_VERSION"),
|
||||||
|
env!("VERGEN_GIT_SHA")
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Some("--no-daemon") => {
|
||||||
|
daemonize = false;
|
||||||
|
}
|
||||||
|
Some("-e") | Some("--command") => {
|
||||||
|
// Handle the '--command' or '-e' flag
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
"--no-daemon" => {
|
Some("--") => {
|
||||||
daemonize = false;
|
// End of flags, the next args are shell-related
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
//TODO: should this throw an error?
|
//TODO: should this throw an error?
|
||||||
|
|
@ -101,8 +123,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let shell_program_opt = args_iter.next();
|
// After flags, process remaining shell program and args
|
||||||
let shell_args = Vec::from_iter(args_iter);
|
while let Some(arg) = raw_args.next_os(&mut cursor) {
|
||||||
|
if let Some(program) = shell_program_opt.take() {
|
||||||
|
shell_args.push(arg.to_string_lossy().to_string());
|
||||||
|
} else {
|
||||||
|
shell_program_opt = Some(arg.to_string_lossy().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Platform-specific daemonization logic
|
||||||
|
|
||||||
#[cfg(all(unix, not(target_os = "redox")))]
|
#[cfg(all(unix, not(target_os = "redox")))]
|
||||||
if daemonize {
|
if daemonize {
|
||||||
|
|
@ -145,27 +175,44 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Terminal config setup
|
||||||
let term_config = term::Config::default();
|
let term_config = term::Config::default();
|
||||||
// Set up environmental variables for terminal
|
// Set up environmental variables for terminal
|
||||||
tty::setup_env();
|
tty::setup_env();
|
||||||
// Override TERM for better compatibility
|
// Override TERM for better compatibility
|
||||||
env::set_var("TERM", "xterm-256color");
|
env::set_var("TERM", "xterm-256color");
|
||||||
|
|
||||||
|
// Set settings
|
||||||
let mut settings = Settings::default();
|
let mut settings = Settings::default();
|
||||||
settings = settings.theme(config.app_theme.theme());
|
settings = settings.theme(config.app_theme.theme());
|
||||||
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));
|
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));
|
||||||
|
|
||||||
|
// Flags
|
||||||
let flags = Flags {
|
let flags = Flags {
|
||||||
config_handler,
|
config_handler,
|
||||||
config,
|
config,
|
||||||
startup_options,
|
startup_options,
|
||||||
term_config,
|
term_config,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Run the cosmic app
|
||||||
cosmic::app::run::<App>(settings, flags)?;
|
cosmic::app::run::<App>(settings, flags)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_help() {
|
||||||
|
println!(
|
||||||
|
r#"COSMIC Terminal
|
||||||
|
Designed for the COSMIC™ desktop environment, cosmic-term is a libcosmic-based terminal emulator.
|
||||||
|
|
||||||
|
Project home page: https://github.com/pop-os/cosmic-term
|
||||||
|
Options:
|
||||||
|
--help Show this message
|
||||||
|
--version Show the version of cosmic-term"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Flags {
|
pub struct Flags {
|
||||||
config_handler: Option<cosmic_config::Config>,
|
config_handler: Option<cosmic_config::Config>,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue