Skip to content

Commit d4455a2

Browse files
author
Stephan Dilly
committed
move args stuff into separate file
1 parent fc5deec commit d4455a2

File tree

4 files changed

+107
-111
lines changed

4 files changed

+107
-111
lines changed

src/args.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use crate::bug_report;
2+
use anyhow::{anyhow, Result};
3+
use clap::{
4+
crate_authors, crate_description, crate_name, crate_version,
5+
App as ClapApp, Arg,
6+
};
7+
use simplelog::{Config, LevelFilter, WriteLogger};
8+
use std::{
9+
env,
10+
fs::{self, File},
11+
path::PathBuf,
12+
};
13+
14+
pub struct CliArgs {
15+
pub theme: PathBuf,
16+
}
17+
18+
pub fn process_cmdline() -> Result<CliArgs> {
19+
let app = ClapApp::new(crate_name!())
20+
.author(crate_authors!())
21+
.version(crate_version!())
22+
.about(crate_description!())
23+
.arg(
24+
Arg::with_name("theme")
25+
.help("Set the color theme (defaults to theme.ron)")
26+
.short("t")
27+
.long("theme")
28+
.value_name("THEME")
29+
.takes_value(true),
30+
)
31+
.arg(
32+
Arg::with_name("logging")
33+
.help("Stores logging output into a cache directory")
34+
.short("l")
35+
.long("logging"),
36+
)
37+
.arg(
38+
Arg::with_name("bugreport")
39+
.help("Generate a bug report")
40+
.long("bugreport"),
41+
)
42+
.arg(
43+
Arg::with_name("directory")
44+
.help("Set the working directory")
45+
.short("d")
46+
.long("directory")
47+
.takes_value(true),
48+
);
49+
50+
let arg_matches = app.get_matches();
51+
if arg_matches.is_present("bugreport") {
52+
bug_report::generate_bugreport()?;
53+
std::process::exit(0);
54+
}
55+
if arg_matches.is_present("logging") {
56+
setup_logging()?;
57+
}
58+
if arg_matches.is_present("directory") {
59+
let directory =
60+
arg_matches.value_of("directory").unwrap_or(".");
61+
env::set_current_dir(directory)?;
62+
}
63+
let arg_theme =
64+
arg_matches.value_of("theme").unwrap_or("theme.ron");
65+
if get_app_config_path()?.join(arg_theme).is_file() {
66+
Ok(CliArgs {
67+
theme: get_app_config_path()?.join(arg_theme),
68+
})
69+
} else {
70+
Ok(CliArgs {
71+
theme: get_app_config_path()?.join("theme.ron"),
72+
})
73+
}
74+
}
75+
76+
fn setup_logging() -> Result<()> {
77+
let mut path = get_app_config_path()?;
78+
path.push("gitui.log");
79+
80+
let _ = WriteLogger::init(
81+
LevelFilter::Trace,
82+
Config::default(),
83+
File::create(path)?,
84+
);
85+
86+
Ok(())
87+
}
88+
89+
pub fn get_app_config_path() -> Result<PathBuf> {
90+
let mut path = if cfg!(target_os = "macos") {
91+
dirs_next::home_dir().map(|h| h.join(".config"))
92+
} else {
93+
dirs_next::config_dir()
94+
}
95+
.ok_or_else(|| anyhow!("failed to find os config dir."))?;
96+
97+
path.push("gitui");
98+
fs::create_dir_all(&path)?;
99+
Ok(path)
100+
}

src/components/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
EventState, ExternalEditorComponent,
55
};
66
use crate::{
7-
get_app_config_path,
7+
args::get_app_config_path,
88
keys::SharedKeyConfig,
99
queue::{InternalEvent, NeedsUpdate, Queue},
1010
strings,

src/keys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//TODO: remove once fixed https://github.com/rust-lang/rust-clippy/issues/6818
22
#![allow(clippy::use_self)]
33

4-
use crate::get_app_config_path;
54
use anyhow::Result;
65
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
76
use ron::{
@@ -16,6 +15,8 @@ use std::{
1615
rc::Rc,
1716
};
1817

18+
use crate::args::get_app_config_path;
19+
1920
pub type SharedKeyConfig = Rc<KeyConfig>;
2021

2122
#[derive(Serialize, Deserialize, Debug)]

src/main.rs

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// #![deny(clippy::expect_used)]
1616

1717
mod app;
18+
mod args;
1819
mod bug_report;
1920
mod clipboard;
2021
mod cmdbar;
@@ -30,14 +31,10 @@ mod tabs;
3031
mod ui;
3132
mod version;
3233

33-
use crate::app::App;
34-
use anyhow::{anyhow, bail, Result};
34+
use crate::{app::App, args::process_cmdline};
35+
use anyhow::{bail, Result};
3536
use asyncgit::AsyncNotification;
3637
use backtrace::Backtrace;
37-
use clap::{
38-
crate_authors, crate_description, crate_name, crate_version,
39-
App as ClapApp, Arg,
40-
};
4138
use crossbeam_channel::{tick, unbounded, Receiver, Select};
4239
use crossterm::{
4340
terminal::{
@@ -51,15 +48,10 @@ use keys::KeyConfig;
5148
use profiler::Profiler;
5249
use scopeguard::defer;
5350
use scopetime::scope_time;
54-
use simplelog::{Config, LevelFilter, WriteLogger};
5551
use spinner::Spinner;
5652
use std::{
57-
env, fs,
58-
fs::File,
5953
io::{self, Write},
60-
panic,
61-
path::PathBuf,
62-
process,
54+
panic, process,
6355
time::{Duration, Instant},
6456
};
6557
use tui::{
@@ -80,10 +72,6 @@ pub enum QueueEvent {
8072
InputEvent(InputEvent),
8173
}
8274

83-
struct CliArgs {
84-
theme: PathBuf,
85-
}
86-
8775
fn main() -> Result<()> {
8876
let cliargs = process_cmdline()?;
8977

@@ -259,99 +247,6 @@ fn start_terminal<W: Write>(
259247
Ok(terminal)
260248
}
261249

262-
fn get_app_cache_path() -> Result<PathBuf> {
263-
let mut path = dirs_next::cache_dir()
264-
.ok_or_else(|| anyhow!("failed to find os cache dir."))?;
265-
266-
path.push("gitui");
267-
fs::create_dir_all(&path)?;
268-
Ok(path)
269-
}
270-
271-
fn get_app_config_path() -> Result<PathBuf> {
272-
let mut path = if cfg!(target_os = "macos") {
273-
dirs_next::home_dir().map(|h| h.join(".config"))
274-
} else {
275-
dirs_next::config_dir()
276-
}
277-
.ok_or_else(|| anyhow!("failed to find os config dir."))?;
278-
279-
path.push("gitui");
280-
fs::create_dir_all(&path)?;
281-
Ok(path)
282-
}
283-
284-
fn setup_logging() -> Result<()> {
285-
let mut path = get_app_cache_path()?;
286-
path.push("gitui.log");
287-
288-
let _ = WriteLogger::init(
289-
LevelFilter::Trace,
290-
Config::default(),
291-
File::create(path)?,
292-
);
293-
294-
Ok(())
295-
}
296-
297-
fn process_cmdline() -> Result<CliArgs> {
298-
let app = ClapApp::new(crate_name!())
299-
.author(crate_authors!())
300-
.version(crate_version!())
301-
.about(crate_description!())
302-
.arg(
303-
Arg::with_name("theme")
304-
.help("Set the color theme (defaults to theme.ron)")
305-
.short("t")
306-
.long("theme")
307-
.value_name("THEME")
308-
.takes_value(true),
309-
)
310-
.arg(
311-
Arg::with_name("logging")
312-
.help("Stores logging output into a cache directory")
313-
.short("l")
314-
.long("logging"),
315-
)
316-
.arg(
317-
Arg::with_name("bugreport")
318-
.help("Generate a bug report")
319-
.long("bugreport"),
320-
)
321-
.arg(
322-
Arg::with_name("directory")
323-
.help("Set the working directory")
324-
.short("d")
325-
.long("directory")
326-
.takes_value(true),
327-
);
328-
329-
let arg_matches = app.get_matches();
330-
if arg_matches.is_present("bugreport") {
331-
bug_report::generate_bugreport()?;
332-
std::process::exit(0);
333-
}
334-
if arg_matches.is_present("logging") {
335-
setup_logging()?;
336-
}
337-
if arg_matches.is_present("directory") {
338-
let directory =
339-
arg_matches.value_of("directory").unwrap_or(".");
340-
env::set_current_dir(directory)?;
341-
}
342-
let arg_theme =
343-
arg_matches.value_of("theme").unwrap_or("theme.ron");
344-
if get_app_config_path()?.join(arg_theme).is_file() {
345-
Ok(CliArgs {
346-
theme: get_app_config_path()?.join(arg_theme),
347-
})
348-
} else {
349-
Ok(CliArgs {
350-
theme: get_app_config_path()?.join("theme.ron"),
351-
})
352-
}
353-
}
354-
355250
fn set_panic_handlers() -> Result<()> {
356251
// regular panic handler
357252
panic::set_hook(Box::new(|e| {

0 commit comments

Comments
 (0)