Skip to content

Commit

Permalink
feat: access build information through cli
Browse files Browse the repository at this point in the history
  • Loading branch information
B0ney committed Dec 29, 2023
1 parent 196b9fa commit 3dab004
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 16 deletions.
77 changes: 77 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::path::Path;

static HELP: &str = "\
--help -h Prints help information
--version -V Prints version
";

#[derive(Debug, Default)]
pub enum Mode {
#[default]
None,
Version,
Help,
#[cfg(feature = "built")]
BuildInfo,
#[cfg(windows)]
DragNDrop(Vec<String>),
Unrecognised(String),
}

pub fn parse(args: Vec<String>) -> Mode {
if contains(&args, ["--help", "-h"]) {
return Mode::Help;
}

if contains(&args, ["--version", "-V", "-v"]) {
return Mode::Version;
}

#[cfg(feature = "built")]
if contains(&args, ["--info", "-i"]) {
return Mode::BuildInfo;
}

if let Some(unrecognised) = args
.iter()
.find(|f| f.starts_with('-') && !Path::new(f).exists())
{
return Mode::Unrecognised(unrecognised.to_owned());
}

#[cfg(windows)]
if args.len() > 0 {
return Mode::DragNDrop(args);
}

Mode::None
}

pub fn print_help() {
print!("{}", HELP);

#[cfg(feature = "built")]
println!("--info -i Prints build information");
}

pub fn print_version() {
println!("{}", env!("CARGO_PKG_VERSION"));
}

#[cfg(feature = "built")]
pub fn print_info() {
use crate::screen::build_info::info;

for (label, value) in info(true) {
println!("{label}: {value}");
}
}

pub fn print_unrecognised(option: String) {
println!("Unrecognised option '{option}'");
print_help();
}

fn contains<const T: usize>(args: &[String], flags: [&str; T]) -> bool {
args.iter().any(|f| flags.contains(&f.as_str()))
}
30 changes: 14 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(clippy::needless_lifetimes)]

pub mod app;
mod cli;
pub mod dialog;
pub mod event;
pub mod font;
Expand All @@ -16,6 +17,7 @@ pub mod utils;
pub mod widget;

use app::XMODITS;
use cli::Mode;
use std::env;

#[cfg(all(feature = "jemallocator", not(target_env = "msvc")))]
Expand All @@ -30,21 +32,17 @@ fn main() -> iced::Result {
logger::reattach_windows_terminal();
logger::set_panic_hook();

let version = env::args().skip(1)
.peekable()
.next()
.map(|a| a == "--version" || a == "-V")
.unwrap_or_default();

if version {
println!("{}", env!("CARGO_PKG_VERSION"));
return Ok(());
let mut args: Vec<_> = env::args().collect();
args.remove(0);

match cli::parse(args) {
Mode::None => XMODITS::launch().map(|_| tracing::info!("Bye :)")),
#[cfg(windows)]
Mode::DragNDrop(paths) => XMODITS::launch_simple(paths),
Mode::Version => Ok(cli::print_version()),
Mode::Help => Ok(cli::print_help()),
#[cfg(feature = "built")]
Mode::BuildInfo => Ok(cli::print_info()),
Mode::Unrecognised(option) => Ok(cli::print_unrecognised(option)),
}

#[cfg(windows)]
if env::args().len() > 1 {
return XMODITS::launch_simple(env::args().skip(1));
}

XMODITS::launch().map(|_| tracing::info!("Bye :)"))
}

0 comments on commit 3dab004

Please sign in to comment.