mirror of
https://github.com/gitui-org/gitui
synced 2026-05-24 09:28:21 +00:00
move args stuff into separate file
This commit is contained in:
parent
fc5deec424
commit
d4455a2657
4 changed files with 107 additions and 111 deletions
100
src/args.rs
Normal file
100
src/args.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
use crate::bug_report;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use clap::{
|
||||||
|
crate_authors, crate_description, crate_name, crate_version,
|
||||||
|
App as ClapApp, Arg,
|
||||||
|
};
|
||||||
|
use simplelog::{Config, LevelFilter, WriteLogger};
|
||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
fs::{self, File},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct CliArgs {
|
||||||
|
pub theme: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_cmdline() -> Result<CliArgs> {
|
||||||
|
let app = ClapApp::new(crate_name!())
|
||||||
|
.author(crate_authors!())
|
||||||
|
.version(crate_version!())
|
||||||
|
.about(crate_description!())
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("theme")
|
||||||
|
.help("Set the color theme (defaults to theme.ron)")
|
||||||
|
.short("t")
|
||||||
|
.long("theme")
|
||||||
|
.value_name("THEME")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("logging")
|
||||||
|
.help("Stores logging output into a cache directory")
|
||||||
|
.short("l")
|
||||||
|
.long("logging"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("bugreport")
|
||||||
|
.help("Generate a bug report")
|
||||||
|
.long("bugreport"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("directory")
|
||||||
|
.help("Set the working directory")
|
||||||
|
.short("d")
|
||||||
|
.long("directory")
|
||||||
|
.takes_value(true),
|
||||||
|
);
|
||||||
|
|
||||||
|
let arg_matches = app.get_matches();
|
||||||
|
if arg_matches.is_present("bugreport") {
|
||||||
|
bug_report::generate_bugreport()?;
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
if arg_matches.is_present("logging") {
|
||||||
|
setup_logging()?;
|
||||||
|
}
|
||||||
|
if arg_matches.is_present("directory") {
|
||||||
|
let directory =
|
||||||
|
arg_matches.value_of("directory").unwrap_or(".");
|
||||||
|
env::set_current_dir(directory)?;
|
||||||
|
}
|
||||||
|
let arg_theme =
|
||||||
|
arg_matches.value_of("theme").unwrap_or("theme.ron");
|
||||||
|
if get_app_config_path()?.join(arg_theme).is_file() {
|
||||||
|
Ok(CliArgs {
|
||||||
|
theme: get_app_config_path()?.join(arg_theme),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(CliArgs {
|
||||||
|
theme: get_app_config_path()?.join("theme.ron"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_logging() -> Result<()> {
|
||||||
|
let mut path = get_app_config_path()?;
|
||||||
|
path.push("gitui.log");
|
||||||
|
|
||||||
|
let _ = WriteLogger::init(
|
||||||
|
LevelFilter::Trace,
|
||||||
|
Config::default(),
|
||||||
|
File::create(path)?,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_app_config_path() -> Result<PathBuf> {
|
||||||
|
let mut path = if cfg!(target_os = "macos") {
|
||||||
|
dirs_next::home_dir().map(|h| h.join(".config"))
|
||||||
|
} else {
|
||||||
|
dirs_next::config_dir()
|
||||||
|
}
|
||||||
|
.ok_or_else(|| anyhow!("failed to find os config dir."))?;
|
||||||
|
|
||||||
|
path.push("gitui");
|
||||||
|
fs::create_dir_all(&path)?;
|
||||||
|
Ok(path)
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,7 @@ use super::{
|
||||||
EventState, ExternalEditorComponent,
|
EventState, ExternalEditorComponent,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
get_app_config_path,
|
args::get_app_config_path,
|
||||||
keys::SharedKeyConfig,
|
keys::SharedKeyConfig,
|
||||||
queue::{InternalEvent, NeedsUpdate, Queue},
|
queue::{InternalEvent, NeedsUpdate, Queue},
|
||||||
strings,
|
strings,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
//TODO: remove once fixed https://github.com/rust-lang/rust-clippy/issues/6818
|
//TODO: remove once fixed https://github.com/rust-lang/rust-clippy/issues/6818
|
||||||
#![allow(clippy::use_self)]
|
#![allow(clippy::use_self)]
|
||||||
|
|
||||||
use crate::get_app_config_path;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
use ron::{
|
use ron::{
|
||||||
|
|
@ -16,6 +15,8 @@ use std::{
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::args::get_app_config_path;
|
||||||
|
|
||||||
pub type SharedKeyConfig = Rc<KeyConfig>;
|
pub type SharedKeyConfig = Rc<KeyConfig>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
|
|
||||||
113
src/main.rs
113
src/main.rs
|
|
@ -15,6 +15,7 @@
|
||||||
// #![deny(clippy::expect_used)]
|
// #![deny(clippy::expect_used)]
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
|
mod args;
|
||||||
mod bug_report;
|
mod bug_report;
|
||||||
mod clipboard;
|
mod clipboard;
|
||||||
mod cmdbar;
|
mod cmdbar;
|
||||||
|
|
@ -30,14 +31,10 @@ mod tabs;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod version;
|
mod version;
|
||||||
|
|
||||||
use crate::app::App;
|
use crate::{app::App, args::process_cmdline};
|
||||||
use anyhow::{anyhow, bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use asyncgit::AsyncNotification;
|
use asyncgit::AsyncNotification;
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use clap::{
|
|
||||||
crate_authors, crate_description, crate_name, crate_version,
|
|
||||||
App as ClapApp, Arg,
|
|
||||||
};
|
|
||||||
use crossbeam_channel::{tick, unbounded, Receiver, Select};
|
use crossbeam_channel::{tick, unbounded, Receiver, Select};
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
terminal::{
|
terminal::{
|
||||||
|
|
@ -51,15 +48,10 @@ use keys::KeyConfig;
|
||||||
use profiler::Profiler;
|
use profiler::Profiler;
|
||||||
use scopeguard::defer;
|
use scopeguard::defer;
|
||||||
use scopetime::scope_time;
|
use scopetime::scope_time;
|
||||||
use simplelog::{Config, LevelFilter, WriteLogger};
|
|
||||||
use spinner::Spinner;
|
use spinner::Spinner;
|
||||||
use std::{
|
use std::{
|
||||||
env, fs,
|
|
||||||
fs::File,
|
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
panic,
|
panic, process,
|
||||||
path::PathBuf,
|
|
||||||
process,
|
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
use tui::{
|
use tui::{
|
||||||
|
|
@ -80,10 +72,6 @@ pub enum QueueEvent {
|
||||||
InputEvent(InputEvent),
|
InputEvent(InputEvent),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CliArgs {
|
|
||||||
theme: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let cliargs = process_cmdline()?;
|
let cliargs = process_cmdline()?;
|
||||||
|
|
||||||
|
|
@ -259,99 +247,6 @@ fn start_terminal<W: Write>(
|
||||||
Ok(terminal)
|
Ok(terminal)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_app_cache_path() -> Result<PathBuf> {
|
|
||||||
let mut path = dirs_next::cache_dir()
|
|
||||||
.ok_or_else(|| anyhow!("failed to find os cache dir."))?;
|
|
||||||
|
|
||||||
path.push("gitui");
|
|
||||||
fs::create_dir_all(&path)?;
|
|
||||||
Ok(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_app_config_path() -> Result<PathBuf> {
|
|
||||||
let mut path = if cfg!(target_os = "macos") {
|
|
||||||
dirs_next::home_dir().map(|h| h.join(".config"))
|
|
||||||
} else {
|
|
||||||
dirs_next::config_dir()
|
|
||||||
}
|
|
||||||
.ok_or_else(|| anyhow!("failed to find os config dir."))?;
|
|
||||||
|
|
||||||
path.push("gitui");
|
|
||||||
fs::create_dir_all(&path)?;
|
|
||||||
Ok(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup_logging() -> Result<()> {
|
|
||||||
let mut path = get_app_cache_path()?;
|
|
||||||
path.push("gitui.log");
|
|
||||||
|
|
||||||
let _ = WriteLogger::init(
|
|
||||||
LevelFilter::Trace,
|
|
||||||
Config::default(),
|
|
||||||
File::create(path)?,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_cmdline() -> Result<CliArgs> {
|
|
||||||
let app = ClapApp::new(crate_name!())
|
|
||||||
.author(crate_authors!())
|
|
||||||
.version(crate_version!())
|
|
||||||
.about(crate_description!())
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("theme")
|
|
||||||
.help("Set the color theme (defaults to theme.ron)")
|
|
||||||
.short("t")
|
|
||||||
.long("theme")
|
|
||||||
.value_name("THEME")
|
|
||||||
.takes_value(true),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("logging")
|
|
||||||
.help("Stores logging output into a cache directory")
|
|
||||||
.short("l")
|
|
||||||
.long("logging"),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("bugreport")
|
|
||||||
.help("Generate a bug report")
|
|
||||||
.long("bugreport"),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::with_name("directory")
|
|
||||||
.help("Set the working directory")
|
|
||||||
.short("d")
|
|
||||||
.long("directory")
|
|
||||||
.takes_value(true),
|
|
||||||
);
|
|
||||||
|
|
||||||
let arg_matches = app.get_matches();
|
|
||||||
if arg_matches.is_present("bugreport") {
|
|
||||||
bug_report::generate_bugreport()?;
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
if arg_matches.is_present("logging") {
|
|
||||||
setup_logging()?;
|
|
||||||
}
|
|
||||||
if arg_matches.is_present("directory") {
|
|
||||||
let directory =
|
|
||||||
arg_matches.value_of("directory").unwrap_or(".");
|
|
||||||
env::set_current_dir(directory)?;
|
|
||||||
}
|
|
||||||
let arg_theme =
|
|
||||||
arg_matches.value_of("theme").unwrap_or("theme.ron");
|
|
||||||
if get_app_config_path()?.join(arg_theme).is_file() {
|
|
||||||
Ok(CliArgs {
|
|
||||||
theme: get_app_config_path()?.join(arg_theme),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Ok(CliArgs {
|
|
||||||
theme: get_app_config_path()?.join("theme.ron"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_panic_handlers() -> Result<()> {
|
fn set_panic_handlers() -> Result<()> {
|
||||||
// regular panic handler
|
// regular panic handler
|
||||||
panic::set_hook(Box::new(|e| {
|
panic::set_hook(Box::new(|e| {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue