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
135
136
use std::io::{BufRead, BufReader, Read};
use std::{fs::File, path::PathBuf, process::Command};
mod git;
mod git_cmd;
mod parser;
mod status;
pub use parser::parse;
use Subcommand::*;
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");
#[derive(Debug, PartialEq)]
pub enum Subcommand {
Status(bool),
Number,
Version,
Unset,
}
#[derive(Debug)]
pub struct App {
subcommand: Subcommand,
cwd: PathBuf,
cmd: Command,
pargs: Vec<String>,
cache: Vec<String>,
}
impl App {
fn cache_path(&self) -> Option<PathBuf> {
git::git_dir(&self.pargs).map(|v| self.cwd.join(v).join("gitnu.txt"))
}
pub fn cache(&self, create: bool) -> Option<File> {
self.cache_path().and_then(|v| match create {
true => File::create(v).ok(),
false => File::open(v).ok(),
})
}
pub fn read_cache(&mut self) {
std::mem::swap(&mut self.cache, &mut vec!["0".to_string()]);
self.cache(false).map(|f| lines(f).for_each(|v| self.cache.push(v)));
}
pub fn set_once(&mut self, sc: Subcommand) {
match (&self.subcommand, &sc) {
(Unset, _) => self.subcommand = sc,
(Status(true), Status(false)) => self.subcommand = sc,
_ => (),
}
}
pub fn push_arg(&mut self, arg: &str) {
self.cmd.arg(arg);
if self.subcommand == Unset {
self.pargs.push(arg.to_string());
}
}
pub fn new(cwd: PathBuf) -> Self {
let mut cmd = Command::new("git");
if atty::is(atty::Stream::Stdout) {
cmd.args(["-c", "color.ui=always"]);
}
cmd.current_dir(&cwd);
Self { cwd, subcommand: Unset, pargs: vec![], cache: vec![], cmd }
}
}
fn lines<R: Read>(src: R) -> impl Iterator<Item = String> {
BufReader::new(src).lines().filter_map(|v| v.ok())
}
pub fn spawn(mut c: Command) {
c.spawn().and_then(|mut v| v.wait().map(|_| ())).ok();
}
pub fn run(app: App) {
match app.subcommand {
Status(is_normal) => status::status(app, is_normal),
Version => {
spawn(app.cmd);
println!("gitnu version {}", VERSION.unwrap_or("unknown"));
}
_ => spawn(app.cmd),
};
}
#[cfg(test)]
mod tests;