~nicohman/raven

0589a29c0e940c1a3993a0756d25f7f8e8922ceb — nicohman 1 year, 7 months ago fc15062
Restructure args into a struct for better verbose flag usage
2 files changed, 58 insertions(+), 250 deletions(-)

M src/args.rs
M src/main.rs
M src/args.rs => src/args.rs +28 -103
@@ 1,18 1,17 @@
#[derive(StructOpt, Debug)]
#[structopt(name = "raven")]
pub enum Raven {
pub struct Raven {
    #[structopt(flatten)]
    pub verbose: clap_verbosity_flag::Verbosity,
    #[structopt(subcommand)]
    pub command: Command,
}
#[derive(StructOpt, Debug)]
pub enum Command {
    #[structopt(name = "load", about = "Load a complete theme")]
    Load {
        theme: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Load { theme: String },
    #[structopt(name = "new", about = "Create a new theme")]
    New {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    New { name: String },
    #[structopt(
        name = "modify",
        about = "Open the currently edited themes's option in $EDITOR"


@@ 22,68 21,33 @@ pub enum Raven {
        #[structopt(short = "e", long = "editor")]
        editor: Option<String>,
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    #[structopt(name = "delete", about = "Delete a theme")]
    Delete {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Delete { name: String },
    #[structopt(
        name = "info",
        about = "Print info about the theme being currently edited"
    )]
    Info {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Info,
    #[structopt(name = "refresh", about = "Load last loaded theme")]
    Refresh {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Refresh,
    #[structopt(name = "install", about = "Install a theme from ThemeHub repo")]
    Install {
        name: String,
        /// Don't prompt for confirmation
        #[structopt(short = "f", long = "force")]
        force: bool,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    #[structopt(name = "add", about = "Add option to current theme")]
    Add {
        option: String,
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Add { option: String, name: String },
    #[structopt(name = "rm", about = "Remove an option from edited theme")]
    Rm {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Rm { name: String },
    #[structopt(name = "key", about = "Add a key-value option")]
    Key {
        key: String,
        value: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Key { key: String, value: String },
    #[structopt(name = "edit", about = "Edit theme")]
    Edit {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Edit { name: String },
    #[structopt(name = "menu", about = "Show theme menu")]
    Menu {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Menu,
    #[structopt(name = "manage", about = "Manage online themes")]
    ManageO(Manage),
    #[structopt(name = "cycle", about = "Control cycle daemon")]


@@ 92,74 56,35 @@ pub enum Raven {
#[derive(StructOpt, Debug)]
pub enum Manage {
    #[structopt(name = "export", about = "Export a theme to a tarball")]
    Export {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Export { name: String },
    #[structopt(name = "import", about = "Import a theme from a tarball")]
    Import {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Import { name: String },
    #[structopt(name = "publish", about = "Publish an account online")]
    Publish {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Publish { name: String },
    #[structopt(name = "create", about = "Create an account")]
    Create {
        name: String,
        pass1: String,
        pass2: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    #[structopt(
        name = "delete_user",
        about = "Delete an online user's profile and owned themes"
    )]
    DUser {
        pass: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    DUser { pass: String },
    #[structopt(name = "logout", about = "Log out of your user profile")]
    Logout {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Logout,
    #[structopt(name = "unpublish", about = "Delete an online theme")]
    Unpublish {
        name: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Unpublish { name: String },
    #[structopt(name = "login", about = "Log in to an user's account")]
    Login {
        name: String,
        pass: String,
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Login { name: String, pass: String },
}
#[derive(StructOpt, Debug)]
pub enum Cycle {
    #[structopt(name = "start", about = "Start the daemon")]
    Start {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Start,
    #[structopt(name = "check", about = "Check if daemon is running")]
    Check {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Check,
    #[structopt(name = "stop", about = "Stop the daemon")]
    Stop {
        #[structopt(flatten)]
        verbose: clap_verbosity_flag::Verbosity,
    },
    Stop,
}

M src/main.rs => src/main.rs +30 -147
@@ 24,147 24,64 @@ fn main() {
fn interpet_args() {
    //Interpet arguments and check for a need to run init()
    let r = Raven::from_args();
    use args::Command::*;
    use Cycle::*;
    use Manage::*;
    use Raven::*;
    check_themes().unwrap();
    //If a theme may be changing, kill the previous theme's processes. Currently only polybar
    //and lemonbar
    let conf = get_config().unwrap();
    match r {
        Load { theme, verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
    r.verbose
        .setup_env_logger("raven")
        .expect("Couldn't set up logger");
    match r.command {
        Load { theme } => {
            run_theme(&load_theme(theme).unwrap()).unwrap();
        }
        New { name, verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            new_theme(name).unwrap()
        }
        Modify {
            name,
            editor,
            verbose,
        } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            modify_file(conf.editing, name, editor)
        }
        Delete { name, verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            del_theme(name).unwrap()
        }
        Edit { name, verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
        New { name } => new_theme(name).unwrap(),
        Modify { name, editor } => modify_file(conf.editing, name, editor),
        Delete { name } => del_theme(name).unwrap(),
        Edit { name } => {
            edit(name).unwrap();
        }
        Key {
            key,
            value,
            verbose,
        } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            key_value(key, value, conf.editing).unwrap()
        }
        Key { key, value } => key_value(key, value, conf.editing).unwrap(),
        ManageO { .. } => {
            match r {
                ManageO(Export { name, verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
            match r.command {
                ManageO(Export { name }) => {
                    export(name, check_tmp()).unwrap();
                }
                ManageO(Import { name, verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                    import(name).unwrap()
                }
                ManageO(Publish { name, verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                ManageO(Import { name }) => import(name).unwrap(),
                ManageO(Publish { name }) => {
                    upload_theme(name).unwrap();
                }
                ManageO(Create {
                    name,
                    pass1,
                    pass2,
                    verbose,
                }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                ManageO(Create { name, pass1, pass2 }) => {
                    create_user(name, pass1, pass2).unwrap();
                }
                ManageO(Unpublish { name, verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                    unpublish_theme(name).unwrap()
                }
                ManageO(Login {
                    name,
                    pass,
                    verbose,
                }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                    login_user(name, pass).unwrap()
                }
                ManageO(Logout { verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                    logout().unwrap()
                }
                ManageO(DUser { pass, verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                    delete_user(pass).unwrap()
                }
                ManageO(Unpublish { name }) => unpublish_theme(name).unwrap(),
                ManageO(Login { name, pass }) => login_user(name, pass).unwrap(),
                ManageO(Logout) => logout().unwrap(),
                ManageO(DUser { pass }) => delete_user(pass).unwrap(),
                _ => println!("Well, this shouldn't be happening"),
            };
        }
        CycleD { .. } => {
            let running = check_daemon().unwrap();
            match r {
                CycleD(Check { verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
            match r.command {
                CycleD(Check) => {
                    if running {
                        println!("Cycle daemon running.");
                    } else {
                        println!("Cycle daemon not running.");
                    }
                }
                CycleD(Start { verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                CycleD(Start) => {
                    if !running {
                        start_daemon().unwrap();
                    } else {
                        println!("Cycle daemon already running.");
                    }
                }
                CycleD(Stop { verbose }) => {
                    verbose
                        .setup_env_logger("raven")
                        .expect("Couldn't set up logger");
                CycleD(Stop) => {
                    if running {
                        stop_daemon().unwrap();
                    } else {


@@ 176,50 93,16 @@ fn interpet_args() {
                }
            }
        }
        Info { verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            print_info(conf.editing)
        }
        Refresh { verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
        Info => print_info(conf.editing),
        Refresh => {
            refresh_theme(conf.last).unwrap();
        }
        Install {
            name,
            force,
            verbose,
        } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
        Install { name, force } => {
            download_theme(name, force).unwrap();
        }
        Add {
            name,
            option,
            verbose,
        } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            add_to_theme(conf.editing, option, name).unwrap()
        }
        Rm { name, verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            rm_from_theme(conf.editing, name).unwrap()
        }
        Menu { verbose } => {
            verbose
                .setup_env_logger("raven")
                .expect("Couldn't set up logger");
            show_menu(conf.menu_command)
        }
        Add { name, option } => add_to_theme(conf.editing, option, name).unwrap(),
        Rm { name } => rm_from_theme(conf.editing, name).unwrap(),
        Menu => show_menu(conf.menu_command),
    };
}