From 8c8334bc0e0c1b4129541fd1f556a701b131497b Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 9 Nov 2021 23:50:00 +0100 Subject: switch from getopts to clap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In addition to the glaring lack of ergonomics the getopts crate doesn’t even build anymore. --- Cargo.toml | 2 +- src/vtcol.rs | 184 ++++++++++++++++++++++++++++------------------------------- 2 files changed, 88 insertions(+), 98 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4108d5d..1f187f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ test = false doc = false [dependencies] +clap = "2.33" libc = "0.2" -getopts = "0.2" lazy_static = "1.4" diff --git a/src/vtcol.rs b/src/vtcol.rs index 174b62f..665b600 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -1,5 +1,6 @@ -use lazy_static::lazy_static; use std::sync::Mutex; +use std::path::{Path, PathBuf}; +use lazy_static::lazy_static; type Fd = libc::c_int; @@ -95,7 +96,7 @@ enum Scheme Default, SolarizedDark, SolarizedLight, - Custom(Option), + Custom(Option), } impl<'a> std::fmt::Display for Scheme @@ -106,13 +107,8 @@ impl<'a> std::fmt::Display for Scheme Scheme::Default => "default", Scheme::SolarizedDark => "solarized_dark", Scheme::SolarizedLight => "solarized_light", - Scheme::Custom(ref kind) => { - let tmp: &str = match *kind { - Some(ref fname) => fname.as_str(), - None => "", - }; - tmp - }, + Scheme::Custom(None) => "", + Scheme::Custom(Some(fname)) => fname.display(), }; write!(f, "{}", id) } @@ -127,104 +123,106 @@ extern "C" { #[derive(Debug)] struct Job { - this: String, /* argv[0] */ scheme: Scheme, /* The color scheme to switch to. */ } impl<'a> Job { - pub fn new() -> Job + pub fn from_argv() -> Job { - let argv: Vec = std::env::args().collect(); - let this = argv[0].clone(); - let mut opts = getopts::Options::new(); - - opts.optopt("s", "scheme", "predefined color scheme", "NAME"); - opts.optopt("d", "dump", "dump predefined scheme", "NAME"); - opts.optopt("f", "file", "apply scheme from file", "PATH"); - opts.optflag("v", "verbose", "enable verbose messages"); - opts.optflag("l", "list", "list available color schemes"); - opts.optflag("h", "help", "print this message"); - - let matches = match opts.parse(&argv[1..]) { - Ok(m) => m, - Err(f) => panic!(f.to_string()), - }; - - if matches.opt_present("h") { - Job::usage(&this, opts); - unsafe { exit(0) }; - }; - - if matches.opt_present("v") { + use clap::{App, Arg}; + + let matches = App::new(clap::crate_name!()) + .version(clap::crate_version!()) + .author(clap::crate_authors!()) + .about(clap::crate_description!()) + .arg( + Arg::with_name("scheme") + .short("s") + .long("scheme") + .value_name("NAME") + .help("predefined color scheme") + .takes_value(true), + ) + .arg( + Arg::with_name("dump") + .short("d") + .long("dump") + .value_name("NAME") + .help("dump predefined scheme") + .takes_value(true), + ) + .arg( + Arg::with_name("file") + .short("f") + .long("file") + .value_name("PATH") + .help("apply scheme from file") + .takes_value(true), + ) + .arg( + Arg::with_name("verbose") + .short("v") + .long("verbose") + .help("enable extra diagnostics") + .takes_value(false), + ) + .arg( + Arg::with_name("list") + .short("l") + .long("list") + .help("list available color schemes") + .takes_value(false), + ) + .arg( + Arg::with_name("help") + .short("h") + .long("help") + .help("print this message") + .takes_value(false), + ) + .get_matches(); + + if matches.is_present("v") { VERBOSITY.lock().unwrap() = true; } - if matches.opt_present("l") { + if matches.is_present("l") { Job::schemes(); - unsafe { exit(0) }; + std::process::exit(0); }; - if matches.opt_present("d") { - match matches.opt_str("d") { - None => { - Job::usage(&this, opts); - panic!("no color scheme given, aborting") - }, - Some(name) => { - let scm = Job::pick_scheme(&name); - Job::dump(scm); - unsafe { exit(0) }; - }, - }; + if let Some(name) = matches.value_of("dump") { + let scm = Job::pick_scheme(name); + Job::dump(scm); + std::process::exit(0); } - let scheme = if matches.opt_present("f") { - match matches.opt_str("f") { - None => { - Job::usage(&this, opts); - panic!("no file name specified, aborting") + let scheme = match matches.value_of("file") { + Some("-") => Job::scheme_from_stdin(), + Some(fname) => Scheme::Custom(Some(PathBuf::from(fname))), + None => + match matches.value_of("scheme") { + Some("-") | None => Job::scheme_from_stdin(), + Some(name) => Job::pick_scheme(name), }, - Some(fname) => - if fname == "-" { - Job::scheme_from_stdin() - } else { - Scheme::Custom(Some(fname.clone())) - }, - } - } else { - match matches.opt_str("s") { - None => Job::scheme_from_stdin(), - Some(name) => - if name == "-" { - Job::scheme_from_stdin() - } else { - Job::pick_scheme(&name) - }, - } - }; /* [let scheme] */ + }; - Job { this, scheme } + Job { scheme } } - fn pick_scheme<'b>(name: &String) -> Scheme + fn pick_scheme<'b>(name: &str) -> Scheme { - match name.as_str() { + match name { "solarized" | "solarized_dark" | "sd" => Scheme::SolarizedDark, "solarized_light" | "sl" => Scheme::SolarizedLight, "default" | "normal" => Scheme::Default, - _any => Scheme::Custom(Some(name.clone())), + _any => Scheme::Custom(Some(PathBuf::from(name))), } } fn scheme_from_stdin() -> Scheme { Scheme::Custom(None) } - fn usage(this: &String, opts: getopts::Options) - { - let brief = format!("usage: {} [options]", this); - print!("{}", opts.usage(&brief)); - } - fn schemes() { println!("Available color schemes:"); @@ -240,12 +238,9 @@ impl<'a> Job Scheme::Default => Job::dump_scheme(&DEFAULT_COLORS), Scheme::SolarizedDark => Job::dump_scheme(&SOLARIZED_COLORS_DARK), Scheme::SolarizedLight => Job::dump_scheme(&SOLARIZED_COLORS_LIGHT), - Scheme::Custom(kind) => - match kind { - Some(fname) => + Scheme::Custom(None) => Job::dump_palette(Palette::from_stdin()), + Scheme::Custom(Some(fname)) => Job::dump_palette(Palette::from_file(&fname)), - None => Job::dump_palette(Palette::from_stdin()), - }, } } @@ -431,12 +426,11 @@ impl Palette /* [Palette::from_buffered_reader] */ - pub fn from_file(fname: &String) -> Palette + pub fn from_file(fname: &Path) -> Palette { /* Check if file exists */ - let path = std::path::Path::new(fname); - let file = match std::fs::File::open(&path) { + let file = match std::fs::File::open(&fname) { Err(e) => panic!("failed to open {} as file ({})", fname, e), Ok(f) => f, }; @@ -583,20 +577,16 @@ fn clear_term(fd: Fd) fn main() { - let job = Job::new(); + let job = Job::from_argv(); vrb!("job parms: {:?}", job); - let mut pal: Palette = { + let mut pal: Palette = match job.scheme { Scheme::Default => Palette::new(&DEFAULT_COLORS), Scheme::SolarizedDark => Palette::new(&SOLARIZED_COLORS_DARK), Scheme::SolarizedLight => Palette::new(&SOLARIZED_COLORS_LIGHT), - Scheme::Custom(ref kind) => - match *kind { - Some(ref fname) => Palette::from_file(fname), - None => Palette::from_stdin(), - }, - } - }; + Scheme::Custom(None) => Palette::from_stdin(), + Scheme::Custom(Some(fname)) => Palette::from_file(&fname), + }; vrb!("Using palette:"); vrb!("{}", pal); let fd = get_console_fd(None).unwrap(); -- cgit v1.2.3