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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extern crate ansi_term;
extern crate docopt;
extern crate regex;
extern crate rustc_serialize;

use std::fs;
use std::io;
use std::thread;

use std::io::Write;
use std::process::Command;
use std::time::Duration;

use ansi_term::Style;
use ansi_term::Colour::Red;
use docopt::{Docopt, Error};
use regex::Regex;


const USAGE: &'static str = "
blerp filters local or remote files or resources.

Usage:
    blerp [options] [<path>...]
    blerp (--help | --version)

Options:
    -a    Attack mode
    -b    Suppress bees
    -—    Flags use em dashes
    -c    Count number of arguments
    -d    Pipes output to debug.exe
    -D    Deprecated
    -e    Execute something
    -f    Fun mode
    -g    Use Google
    -h    Check whether input halts
    -i    Ignore case (lower)
    -I    Ignore case (upper)
    -jk   Kidding
    -n    Behavior not defined
    -o    Overwrite
    -O    Opposite day
    -p    Set true Pope; accepts \"Rome\" or \"Avignon\"
    -q    Quiet mode; output is printed to stdout instead of being spoken aloud
    -r    Randomize arguments
    -R    Run recursively on http://*
    -s    Follow symbolic links symbolically
    -S    Stealth mode
    -t    Tumble dry
    -u    UTF-8 mode; otherwise defaults to ANSEL
    -U    Update (default: Facebook)
    -v    Verbose; alias to find / -exec cat {}
    -V    Set version number
    -y    Yikes
";

#[derive(Debug)]
pub struct Blerp {
    arguments: Vec<String>,
    suppress_bees: bool,
    count_args: bool,
    deprecated: bool,
    fun_mode: bool,
    use_google: bool,
    check_if_halts: bool,
    opposite_day: bool,
    quiet_mode: bool,
    stealth_mode: bool,
}

impl Blerp {
    pub fn new<S>(argv: Vec<S>) -> Result<Self, Error> where S: AsRef<str> {
        let version = Some(format!("blerp {}", env!("CARGO_PKG_VERSION")));

        let docopt = try!(Docopt::new(USAGE));
        let argvmap = try!(docopt.argv(argv).options_first(true).version(version).parse());

        let blerp = Blerp {
            arguments:      argvmap.get_vec("<path>").iter().map(|a| a.to_string()).collect::<Vec<String>>(),
            suppress_bees:  argvmap.get_bool("-b"),
            count_args:     argvmap.get_bool("-c"),
            deprecated:     argvmap.get_bool("-D"),
            fun_mode:       argvmap.get_bool("-f"),
            use_google:     argvmap.get_bool("-g"),
            check_if_halts: argvmap.get_bool("-h"),
            opposite_day:   argvmap.get_bool("-O"),
            quiet_mode:     argvmap.get_bool("-q"),
            stealth_mode:   argvmap.get_bool("-S"),
        };

        return Ok(blerp);
    }

    pub fn run(&self) -> Result<(), &str> {
        // Use Google
        if self.use_google {
            let engine = if self.opposite_day {
                "https://duckduckgo.com/"
            } else {
                "https://www.google.com/search"
            };

            let url = format!("{}?q={}", engine, self.arguments.join("+"));
            if Command::new("open").arg(url).output().is_err() {
                return Err("Failed to open URL in browser.");
            }
        }

        // Count number of arguments
        if self.count_args {
            println!("Number of arguments: {}", self.arguments.len());
        }

        // Deprecated feature
        if self.deprecated {
            println!("{}", Red.paint("Use of the `-D` option is deprecated."));
        }

        // Fun mode
        if self.fun_mode {
            println!("{}", Style::new().blink().bold().paint("Fun mode!"));
        }

        let mut files: Vec<String> = Vec::new();

        // Get all files in the current directory
        for path in fs::read_dir("./").unwrap() {
            if path.as_ref().unwrap().metadata().unwrap().is_file() {
                files.push(path.unwrap().file_name().into_string().unwrap());
            }
        }

        // Filter files using arguments as regexes
        for arg in self.arguments.clone() {
            let re = Regex::new(&arg).unwrap();
            files.retain(|f| re.is_match(f));
        }

        // Stealth mode
        let mut style = Style::new();

        if self.stealth_mode {
            if self.opposite_day {
                style = style.bold();
            } else {
                style = style.dimmed();
            }
        }

        // Determine if `say` or `espeak` are available
        let say_cmd: Option<&str>;

        if Command::new("say").spawn().is_ok() {
            say_cmd = Some("say");
        } else if Command::new("espeak").spawn().is_ok() {
            say_cmd = Some("espeak");
        } else {
            say_cmd = None;

            if self.quiet_mode && self.opposite_day {
                println!("`say` and `espeak` are unavailable. Defaulting to quiet mode.");
            }
        }

        for mut file in files {
            // Suppress bees
            if self.suppress_bees {
                file = file.replace("B", "Ƀ").replace("b", "ƀ");
            }

            // Quiet mode, opposite day
            if self.quiet_mode && self.opposite_day && say_cmd.is_some() {
                Command::new(say_cmd.unwrap()).arg(file).output().unwrap();
            } else {
                println!("{}", style.paint(file));
            }
        }

        // Check whether input halts
        if self.check_if_halts {
            if self.opposite_day {
                println!("Input halts.");
            } else {
                // TODO: Solve Halting problem
                print!("Checking whether input halts");

                let mut delay = Duration::from_secs(1);
                let mut next = delay;

                loop {
                    io::stdout().flush().expect("Couldn't flush stdout");
                    print!(".");
                    thread::sleep(delay);

                    let tmp = delay + next;
                    delay = next;
                    next = tmp;
                }
            }
        }

        return Ok(());
    }
}