Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ use std::{
path::PathBuf,
};

const BUG_REPORT_FLAG_ID: &str = "bugreport";
const LOG_FILE_FLAG_ID: &str = "logfile";
const LOGGING_FLAG_ID: &str = "logging";
const THEME_FLAG_ID: &str = "theme";
const WORKDIR_FLAG_ID: &str = "workdir";
const GIT_DIR_FLAG_ID: &str = "directory";
const WATCHER_FLAG_ID: &str = "watcher";
const DEFAULT_THEME: &str = "theme.ron";
const DEFAULT_GIT_DIR: &str = ".";

pub struct CliArgs {
pub theme: PathBuf,
pub repo_path: RepoPath,
Expand All @@ -23,20 +33,23 @@ pub fn process_cmdline() -> Result<CliArgs> {

let arg_matches = app.get_matches();

if arg_matches.get_flag("bugreport") {
if arg_matches.get_flag(BUG_REPORT_FLAG_ID) {
bug_report::generate_bugreport();
std::process::exit(0);
}
if arg_matches.get_flag("logging") {
let logfile = arg_matches.get_one::<String>("logfile");
if arg_matches.get_flag(LOGGING_FLAG_ID) {
let logfile = arg_matches.get_one::<String>(LOG_FILE_FLAG_ID);
setup_logging(logfile.map(PathBuf::from))?;
}

let workdir =
arg_matches.get_one::<String>("workdir").map(PathBuf::from);
let gitdir = arg_matches
.get_one::<String>("directory")
.map_or_else(|| PathBuf::from("."), PathBuf::from);
let workdir = arg_matches
.get_one::<String>(WORKDIR_FLAG_ID)
.map(PathBuf::from);
let gitdir =
arg_matches.get_one::<String>(GIT_DIR_FLAG_ID).map_or_else(
|| PathBuf::from(DEFAULT_GIT_DIR),
PathBuf::from,
);

let repo_path = if let Some(w) = workdir {
RepoPath::Workdir { gitdir, workdir: w }
Expand All @@ -45,8 +58,8 @@ pub fn process_cmdline() -> Result<CliArgs> {
};

let arg_theme = arg_matches
.get_one::<String>("theme")
.map_or_else(|| PathBuf::from("theme.ron"), PathBuf::from);
.get_one::<String>(THEME_FLAG_ID)
.map_or_else(|| PathBuf::from(DEFAULT_THEME), PathBuf::from);

let confpath = get_app_config_path()?;
fs::create_dir_all(&confpath).with_context(|| {
Expand All @@ -58,7 +71,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
let theme = confpath.join(arg_theme);

let notify_watcher: bool =
*arg_matches.get_one("watcher").unwrap_or(&false);
*arg_matches.get_one(WATCHER_FLAG_ID).unwrap_or(&false);

Ok(CliArgs {
theme,
Expand All @@ -84,48 +97,48 @@ fn app() -> ClapApp {
",
)
.arg(
Arg::new("theme")
Arg::new(THEME_FLAG_ID)
.help("Set color theme filename loaded from config directory")
.short('t')
.long("theme")
.value_name("THEME_FILE")
.default_value("theme.ron")
.default_value(DEFAULT_THEME)
.num_args(1),
)
.arg(
Arg::new("logging")
Arg::new(LOGGING_FLAG_ID)
.help("Store logging output into a file (in the cache directory by default)")
.short('l')
.long("logging")
.default_value_if("logfile", ArgPredicate::IsPresent, "true")
.action(clap::ArgAction::SetTrue),
)
.arg(Arg::new("logfile")
.arg(Arg::new(LOG_FILE_FLAG_ID)
.help("Store logging output into the specified file (implies --logging)")
.long("logfile")
.value_name("LOG_FILE"))
.arg(
Arg::new("watcher")
Arg::new(WATCHER_FLAG_ID)
.help("Use notify-based file system watcher instead of tick-based update. This is more performant, but can cause issues on some platforms. See https://github.com/gitui-org/gitui/blob/master/FAQ.md#watcher for details.")
.long("watcher")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("bugreport")
Arg::new(BUG_REPORT_FLAG_ID)
.help("Generate a bug report")
.long("bugreport")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("directory")
Arg::new(GIT_DIR_FLAG_ID)
.help("Set the git directory")
.short('d')
.long("directory")
.env("GIT_DIR")
.num_args(1),
)
.arg(
Arg::new("workdir")
Arg::new(WORKDIR_FLAG_ID)
.help("Set the working directory")
.short('w')
.long("workdir")
Expand Down
Loading