-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.rs
99 lines (84 loc) · 2.88 KB
/
main.rs
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
mod airline;
mod alacritty;
mod colorscheme;
mod palette;
use airline::AirlineTheme;
use alacritty::AlacrittyTheme;
use colorscheme::Colorscheme;
use palette::Palette;
use anyhow::{Context, Result};
use std::env;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::PathBuf;
fn write_to_files(dir: &str) -> Result<()> {
let palette = Palette::default();
fn join(entries: &[&str]) -> PathBuf {
let mut entries = entries.iter();
let mut path = PathBuf::from(entries.next().unwrap());
for entry in entries {
path.push(entry);
}
path
}
let path = join(&[dir, "colors", "spring-night.vim"]);
let file = File::create(&path)
.with_context(|| format!("Could not create colorscheme file: {:?}", &path))?;
Colorscheme::new(&palette)
.write_to(&mut BufWriter::new(file))
.with_context(|| format!("Could not write to colorscheme file {:?}", &path))?;
let path = join(&[dir, "autoload", "airline", "themes", "spring_night.vim"]);
let file = File::create(&path)
.with_context(|| format!("Could not create airline theme file {:?}", &path))?;
AirlineTheme::new(&palette)
.write_to(&mut BufWriter::new(file))
.with_context(|| format!("Could not write to airline theme file {:?}", &path))?;
let path = join(&[dir, "alacritty", "spring_night.toml"]);
let file = File::create(&path)
.with_context(|| format!("Could not create alacritty theme file {:?}", &path))?;
AlacrittyTheme::new(&palette)
.write_to(&mut BufWriter::new(file))
.with_context(|| format!("Could not write to alacritty theme file {:?}", &path))
}
fn write_to(w: &mut impl Write) -> Result<()> {
let palette = Palette::default();
Colorscheme::new(&palette).write_to(w)?;
writeln!(w)?;
AirlineTheme::new(&palette).write_to(w)?;
writeln!(w)?;
AlacrittyTheme::new(&palette).write_to(w)?;
Ok(())
}
fn main() -> Result<()> {
let (program, args) = {
let mut argv = env::args();
(argv.next().unwrap(), argv)
};
let mut opts = getopts::Options::new();
opts.optopt("d", "dir", "repository root directory", "PATH");
opts.optflag("h", "help", "print this help");
let opts = opts;
let matches = opts
.parse(args)
.context("Please try --help for more detail")?;
if matches.opt_present("h") {
let brief = &format!("Usage: {} [options]", program);
eprintln!("{}", opts.usage(brief));
return Ok(());
}
if let Some(dir) = matches.opt_str("d") {
write_to_files(&dir)
} else {
write_to(&mut io::stdout().lock()).context("Could not write to stdout")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_write_to_stdout_successfully() {
let mut stdout = vec![];
write_to(&mut stdout).unwrap();
assert!(!stdout.is_empty());
}
}