Initial Commit
This commit is contained in:
commit
4e9d1c91ed
5 changed files with 10000 additions and 0 deletions
41
src/main.rs
Normal file
41
src/main.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use std::fs;
|
||||
use rand::prelude::*;
|
||||
use clap::{Arg, App};
|
||||
|
||||
fn main() {
|
||||
// specify app
|
||||
let app = App::new("Fortune.rs")
|
||||
.version("0.1")
|
||||
.author("Daan Boerlage <runebaas@gmail.com>")
|
||||
.about("When fortune is run with no arguments it prints out a random epigram. Epigrams are divided into several categories.")
|
||||
.arg(Arg::with_name("file")
|
||||
.short("F")
|
||||
.long("file")
|
||||
.default_value("fortunes")
|
||||
.help("The fortune file")
|
||||
.takes_value(true)
|
||||
.value_name("file"))
|
||||
.arg(Arg::with_name("length")
|
||||
.short("n")
|
||||
.default_value("160")
|
||||
.help("Set the longest fortune length (in characters) considered to be 'short'. All fortunes longer than this are considered 'long'. Be careful! If you set the length too short and ask for short fortunes, or too long and ask for long ones, fortune goes into a never-ending thrash loop.")
|
||||
.takes_value(true)
|
||||
.value_name("length"));
|
||||
let matches = app.get_matches();
|
||||
|
||||
// get fortunes
|
||||
let filename = matches.value_of("file").unwrap().to_owned();
|
||||
let fortune_file = fs::read_to_string(filename).unwrap();
|
||||
let fortunes: Vec<&str> = fortune_file.split('%').collect();
|
||||
|
||||
// filter by max length
|
||||
let max_length = matches.value_of("length").unwrap().parse::<usize>().unwrap();
|
||||
let filtered = fortunes.into_iter().filter(|x| x.len() < max_length).collect::<Vec<&str>>();
|
||||
|
||||
// get a random one
|
||||
let total_fortunes = filtered.len();
|
||||
let random_fortune = rand::thread_rng().gen_range(0, total_fortunes);
|
||||
|
||||
// print
|
||||
println!("{}", filtered.get(random_fortune).unwrap());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue