From 4a8a00a545f456494f06f24b98fef4aab8faf238 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 13 Apr 2025 13:23:31 -0400 Subject: [PATCH 1/2] Argument support with clap_lex --- Cargo.lock | 7 +++++++ Cargo.toml | 2 ++ src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 7b73688..200aa8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -949,6 +949,12 @@ dependencies = [ "inout", ] +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + [[package]] name = "clipboard-win" version = "5.4.0" @@ -1179,6 +1185,7 @@ dependencies = [ name = "cosmic-edit" version = "0.1.0" dependencies = [ + "clap_lex", "cosmic-files", "cosmic-syntax-theme", "cosmic-text", diff --git a/Cargo.toml b/Cargo.toml index ded49df..fc70b38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,8 @@ tokio = { version = "1", features = ["process", "time"] } # Extra syntax highlighting syntect = "5.2.0" two-face = "0.4.3" +# CLI arguments +clap_lex = "0.7" # Internationalization icu_collator = "1.5" icu_provider = { version = "1.5", features = ["sync"] } diff --git a/src/main.rs b/src/main.rs index 3eba325..a4fea7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,6 +69,10 @@ mod tab; use self::text_box::text_box; mod text_box; +use clap_lex::RawArgs; + +use std::error::Error; + static ICON_CACHE: OnceLock> = OnceLock::new(); static LINE_NUMBER_CACHE: OnceLock> = OnceLock::new(); static SWASH_CACHE: OnceLock> = OnceLock::new(); @@ -79,7 +83,29 @@ pub fn icon_cache_get(name: &'static str, size: u16) -> icon::Icon { icon_cache.get(name, size) } -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { + let raw_args = RawArgs::from_args(); + let mut cursor = raw_args.cursor(); + + // Parse the arguments + while let Some(arg) = raw_args.next_os(&mut cursor) { + match arg.to_str() { + Some("--help") | Some("-h") => { + print_help(env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA")); + return Ok(()); + } + Some("--version") | Some("-V") => { + println!( + "cosmic-edit {} (git commit {})", + env!("CARGO_PKG_VERSION"), + env!("VERGEN_GIT_SHA") + ); + return Ok(()); + } + _ => {} + } + } + #[cfg(all(unix, not(target_os = "redox")))] match fork::daemon(true, true) { Ok(fork::Fork::Child) => (), @@ -182,6 +208,21 @@ fn main() -> Result<(), Box> { Ok(()) } +fn print_help(version: &str, git_rev: &str) { + println!( + r#"cosmic-edit {version} (git commit {git_rev}) +System76 + +Designed for the COSMICâ„¢ desktop environment, cosmic-edit is a libcosmic-based text editor. + +Project home page: https://github.com/pop-os/cosmic-edit + +Options: + -h, --help Show this message + -v, --version Show the version of cosmic-edit"# + ); +} + #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] pub enum Action { Todo, From 3a8353ce19d99429d7539b84a43f2b0f93e3ad80 Mon Sep 17 00:00:00 2001 From: LinuxBoy-96 <128245725+LinuxBoy-96@users.noreply.github.com> Date: Sun, 29 Jun 2025 12:06:34 -0400 Subject: [PATCH 2/2] Update main.rs Uniformized with cosmic-term --- src/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index a4fea7b..6506c73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,7 +91,7 @@ fn main() -> Result<(), Box> { while let Some(arg) = raw_args.next_os(&mut cursor) { match arg.to_str() { Some("--help") | Some("-h") => { - print_help(env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA")); + print_help(env!(); return Ok(()); } Some("--version") | Some("-V") => { @@ -208,18 +208,16 @@ fn main() -> Result<(), Box> { Ok(()) } -fn print_help(version: &str, git_rev: &str) { - println!( - r#"cosmic-edit {version} (git commit {git_rev}) -System76 - +fn print_help() { + println!( + r#"COSMIC Text Editor Designed for the COSMICâ„¢ desktop environment, cosmic-edit is a libcosmic-based text editor. Project home page: https://github.com/pop-os/cosmic-edit Options: - -h, --help Show this message - -v, --version Show the version of cosmic-edit"# + -h, --help Show this message + -v, --version Show the version of cosmic-edit"# ); }