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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::collections::HashSet;
use std::io::{BufRead, BufReader, LineWriter, Read, Write};
use std::{env::current_dir, fs::File, process::Stdio};
use std::{path::PathBuf, process::Command};
mod git_cmd;

#[derive(Debug, PartialEq)]
pub enum Op {
    Status(bool),    // bool: { true: normal, false: short }
    Number(PathBuf), // PathBuf contains command
}

pub struct Opts {
    pub op: Op,
    pub cwd: PathBuf,
    pub gcs: bool, // git-command set? ('add'/'status'/...)
}

impl Opts {
    pub fn cache(&self, create: bool) -> Option<File> {
        let p = PathBuf::from(
            String::from_utf8_lossy(
                &Command::new("git")
                    .args(["rev-parse", "--git-dir"])
                    .current_dir(&self.cwd)
                    .output()
                    .ok()?
                    .stdout,
            )
            .trim(),
        )
        .join("gitnu.txt");
        if create { File::create(p) } else { File::open(p) }.ok()
    }

    pub fn set_op(&mut self, op: Op) {
        match (&self.gcs, &self.op, &op) {
            (false, _, _) => (self.op, self.gcs) = (op, true),
            (_, Op::Status(true), Op::Status(false)) => self.op = op,
            _ => (),
        }
    }
}

pub fn parse(args: Vec<String>) -> (Vec<String>, Opts) {
    // load git commands
    let mut git_cmd = HashSet::with_capacity(git_cmd::GIT_CMD.len());
    git_cmd.extend(git_cmd::GIT_CMD.iter().map(|v| v.to_string()));
    let mut iter = args.iter().skip(1);
    let mut res = Vec::new();
    let mut opts = Opts {
        cwd: current_dir().unwrap_or(".".into()),
        op: Op::Number("git".into()),
        gcs: false,
    };
    while let Some(arg) = iter.next() {
        if !opts.gcs && git_cmd.contains(arg) {
            match arg.as_str() {
                "status" => opts.set_op(Op::Status(true)),
                _ => opts.set_op(Op::Number("git".into())),
            }
        }
        match arg.as_str() {
            "status" => opts.set_op(Op::Status(true)),
            "--short" | "-s" | "--porcelain" => opts.set_op(Op::Status(false)),
            "-x" | "-C" => match opts.gcs {
                false => {
                    if let Some(v) = iter.next() {
                        match arg.as_str() {
                            "-x" => opts.set_op(Op::Number(v.into())),
                            _ => opts.cwd = v.into(),
                        }
                        continue;
                    }
                }
                true => (),
            },
            _ => (),
        }
        res.push(arg.to_string());
    }
    (res, opts)
}

pub fn load(args: Vec<String>, opts: &Opts) -> Vec<PathBuf> {
    fn get_range(arg: &str) -> Option<[usize; 2]> {
        arg.parse().map(|v| Some([v, v])).unwrap_or_else(|_| {
            let (a, b) = arg.split_once("-")?;
            let a = a.parse().ok()?;
            let b = b.parse().unwrap_or(a);
            Some(if a < b { [a, b] } else { [b, a] })
        })
    }
    let mut skip = false;
    let c: Vec<String> =
        opts.cache(false).map(|v| lines(v).collect()).unwrap_or_default();
    args.iter().fold(Vec::new(), |mut r, a| {
        let isf = a.starts_with('-') && !a.starts_with("--"); // is short flag
        match (!skip && !isf, get_range(a)) {
            (true, Some([s, e])) => (s..e + 1)
                .map(|n| (n.checked_sub(1).map(|v| c.get(v)), n.to_string()))
                .for_each(|(o, s)| r.push(o.flatten().unwrap_or(&s).into())),
            _ => r.push(PathBuf::from(a)),
        }
        skip = isf;
        r
    })
}

fn uncolor(f: &str) -> String {
    f.replace("\x1b[31m", "").replace("\x1b[32m", "").replace("\x1b[m", "")
}

fn lines<R: Read>(src: R) -> impl Iterator<Item = String> {
    return BufReader::new(src).lines().filter_map(|v| v.ok());
}

pub fn status(args: &Vec<PathBuf>, o: Opts, is_normal: bool) -> Option<()> {
    let count = &mut 1;
    let mut su = false;
    let mut git = Command::new("git");
    git.args(["-c", "color.status=always"])
        .current_dir(&o.cwd)
        .args(args)
        .stdout(Stdio::piped());
    let mut writer = o.cache(true).map(LineWriter::new);
    let mut git = git.spawn().ok()?;
    let b = lines(git.stdout.as_mut()?);
    b.filter_map(|line| {
        su |= line.contains("Untracked files:");
        match (is_normal, line.starts_with('\t')) {
            (true, false) => println!("{}", line),
            (true, true) => {
                println!("{}{}", *count, line);
                *count += 1;
                let f = uncolor(&line);
                let mut f = f.split_once('\t')?.1;
                f = if su { f } else { f.split_once(':')?.1.trim_start() };
                return Some(o.cwd.join(f));
            }
            _ => {
                println!("{: <3}{}", *count, line);
                *count += 1;
                return Some(o.cwd.join(&uncolor(&line)[3..]));
            }
        };
        return None;
    })
    .for_each(|v| {
        writer.as_mut().map(|lw| writeln!(lw, "{}", v.display()));
    });
    git.wait().ok();
    writer.map(|mut v| v.flush().ok()).flatten()
}

pub fn core(args: Vec<String>) -> (Vec<PathBuf>, Opts) {
    let (args, opts) = parse(args);
    (load(args, &opts), opts)
}

pub fn run(a: Vec<PathBuf>, opts: Opts) -> Option<()> {
    let sp = |c| Command::new(c).args(&a).spawn().ok()?.wait().map(|_| ()).ok();
    match opts.op {
        Op::Status(normal) => status(&a, opts, normal),
        Op::Number(cwd) => sp(cwd),
    }
}

#[cfg(test)]
mod tests;