-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathmain.rs
More file actions
134 lines (112 loc) · 3.92 KB
/
main.rs
File metadata and controls
134 lines (112 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#![allow(clippy::inconsistent_struct_constructor)]
use rx::execution::{DigestMode, ExecutionMode, GifMode};
use rx::logger;
use std::io;
use std::path::PathBuf;
use std::process;
const HEADER: &str = r#"
Alexis Sellier <self@cloudhead.io>
A Modern & Minimalist Pixel Editor
"#;
const HELP: &str = r#"
USAGE
rx [OPTIONS] [<path>..]
OPTIONS
-h, --help Prints help
-V, --version Prints version
-v Verbose mode
-u <script> Use the commands in <script> for initialization
--fullscreen Start application in fullscreen mode
--record <dir> Record user input to a directory
--replay <dir> Replay user input from a directory
--width <width> Set the window width
--height <height> Set the window height
--debug Set debug mode
"#;
fn main() {
if let Err(e) = self::execute(pico_args::Arguments::from_env()) {
eprintln!("rx: {}", e);
process::exit(1);
}
}
fn execute(mut args: pico_args::Arguments) -> Result<(), Box<dyn std::error::Error>> {
rx::ALLOCATOR.reset();
let default = rx::Options::default();
if args.contains(["-h", "--help"]) {
println!("rx v{}{}{}", rx::VERSION, HEADER, HELP);
return Ok(());
}
if args.contains(["-V", "--version"]) {
println!("rx v{}", rx::VERSION);
return Ok(());
}
let verbose = args.contains("-v");
let debug = args.contains("--debug");
let fullscreen = args.contains("--fullscreen");
let width = args.opt_value_from_str("--width")?;
let height = args.opt_value_from_str("--height")?;
let record_digests = args.contains("--record-digests");
let record_gif = args.contains("--record-gif");
let verify_digests = args.contains("--verify-digests");
let headless = args.contains("--headless");
let source = args.opt_value_from_str::<_, PathBuf>("-u")?;
let replay = args.opt_value_from_str::<_, PathBuf>("--replay")?;
let record = args.opt_value_from_str::<_, PathBuf>("--record")?;
let resizable = width.is_none() && height.is_none() && replay.is_none() && record.is_none();
if replay.is_some() && record.is_some() {
return Err("'--replay' and '--record' can't both be specified".into());
}
let digest_mode = if record_digests && !verify_digests {
DigestMode::Record
} else if verify_digests && !record_digests {
DigestMode::Verify
} else if !verify_digests && !record_digests {
DigestMode::Ignore
} else {
return Err("'--record-digests' and '--verify-digests' can't both be specified".into());
};
let gif_mode = if record_gif {
GifMode::Record
} else {
GifMode::Ignore
};
if record_gif && record.is_none() && replay.is_none() {
return Err("'--record-gif' has no effect without '--record' or '--replay'".into());
}
if record_digests && record.is_none() && replay.is_none() {
return Err("'--record-digests' has no effect without '--record' or '--replay'".into());
}
let log_lvl = if verbose {
log::Level::Debug
} else {
log::Level::Info
};
logger::init(log_lvl)?;
let width = width.unwrap_or(default.width);
let height = height.unwrap_or(default.height);
let exec = if let Some(path) = replay {
ExecutionMode::Replay(path, digest_mode)
} else if let Some(path) = record {
ExecutionMode::Record(path, digest_mode, gif_mode)
} else {
ExecutionMode::Normal
};
let glyphs = rx::data::GLYPHS;
let options = rx::Options {
width,
height,
headless,
resizable,
source,
exec,
glyphs,
debug,
fullscreen,
};
match args.free() {
Ok(paths) => rx::init(&paths, options).map_err(|e| e.into()),
Err(e) => {
Err(io::Error::new(io::ErrorKind::InvalidInput, format!("{}\n{}", e, HELP)).into())
}
}
}