|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
use clap::{App, Arg}; |
|
|
|
|
use std::fs; |
|
|
|
|
use std::path::Path; |
|
|
|
|
use clap::{Arg, App};
|
|
|
|
|
|
|
|
|
|
/// Indicator of where the message should be in fumofile
|
|
|
|
|
const MESSAGE_INDICATOR: &str = "!message"; |
|
|
|
@ -12,7 +12,7 @@ const DEFAULT_FUMOFILES_DIR: &str = "/usr/local/share/fumosay/fumofiles/";
|
|
|
|
|
/// Returns a resulting string with `MESSAGE_INDICATOR` replaced with given
|
|
|
|
|
/// `message`. If `MESSAGE_INDICATOR` is not present in fumofile - the
|
|
|
|
|
/// `message` will be added on the new line at the end of the fumofile.
|
|
|
|
|
fn process_message(fumofile_contents: &mut String, message: &str) -> String { |
|
|
|
|
fn process_message(fumofile_contents: &mut String, message: &str) -> String { |
|
|
|
|
if fumofile_contents.contains(MESSAGE_INDICATOR) { |
|
|
|
|
return fumofile_contents.replace(MESSAGE_INDICATOR, message); |
|
|
|
|
} |
|
|
|
@ -27,46 +27,48 @@ fn main() {
|
|
|
|
|
.about("cowsay, but with soft friends") |
|
|
|
|
.arg( |
|
|
|
|
Arg::with_name("fumo") |
|
|
|
|
.short("f") |
|
|
|
|
.long("fumo") |
|
|
|
|
.value_name("fumofile") |
|
|
|
|
.help("Choose another fumofumo to print") |
|
|
|
|
.takes_value(true) |
|
|
|
|
.required(false) |
|
|
|
|
.default_value(FUMO_DEFAULT) |
|
|
|
|
.short("f") |
|
|
|
|
.long("fumo") |
|
|
|
|
.value_name("fumofile") |
|
|
|
|
.help("Choose another fumofumo to print") |
|
|
|
|
.takes_value(true) |
|
|
|
|
.required(false) |
|
|
|
|
.default_value(FUMO_DEFAULT), |
|
|
|
|
) |
|
|
|
|
.arg( |
|
|
|
|
Arg::with_name("fumofiles_directory") |
|
|
|
|
.short("d") |
|
|
|
|
.long("fumofiles_directory") |
|
|
|
|
.value_name("fumofiles_directory_path") |
|
|
|
|
.help("Look for fumofiles in given directory") |
|
|
|
|
.takes_value(true) |
|
|
|
|
.required(false) |
|
|
|
|
.default_value(DEFAULT_FUMOFILES_DIR) |
|
|
|
|
.short("d") |
|
|
|
|
.long("fumofiles_directory") |
|
|
|
|
.value_name("fumofiles_directory_path") |
|
|
|
|
.help("Look for fumofiles in given directory") |
|
|
|
|
.takes_value(true) |
|
|
|
|
.required(false) |
|
|
|
|
.default_value(DEFAULT_FUMOFILES_DIR), |
|
|
|
|
) |
|
|
|
|
.arg( |
|
|
|
|
Arg::with_name("list_fumos") |
|
|
|
|
.short("l") |
|
|
|
|
.long("list_fumos") |
|
|
|
|
.help("Lists all fumofiles in the default directory instead") |
|
|
|
|
.takes_value(false) |
|
|
|
|
.required(false) |
|
|
|
|
.short("l") |
|
|
|
|
.long("list_fumos") |
|
|
|
|
.help("Lists all fumofiles in the default directory instead") |
|
|
|
|
.takes_value(false) |
|
|
|
|
.required(false), |
|
|
|
|
) |
|
|
|
|
.arg( |
|
|
|
|
Arg::with_name("message") |
|
|
|
|
.short("m") |
|
|
|
|
.long("message") |
|
|
|
|
.help("Message to print") |
|
|
|
|
.takes_value(true) |
|
|
|
|
.index(1) |
|
|
|
|
.multiple(true) |
|
|
|
|
).get_matches(); |
|
|
|
|
.short("m") |
|
|
|
|
.long("message") |
|
|
|
|
.help("Message to print") |
|
|
|
|
.takes_value(true) |
|
|
|
|
.index(1) |
|
|
|
|
.multiple(true), |
|
|
|
|
) |
|
|
|
|
.get_matches(); |
|
|
|
|
|
|
|
|
|
// check for list_fumos flag.
|
|
|
|
|
if matches.is_present("list_fumos") { |
|
|
|
|
// list fumofiles and exit
|
|
|
|
|
let fumofiles = fs::read_dir(DEFAULT_FUMOFILES_DIR).expect("Could not read default fumofiles directory"); |
|
|
|
|
let fumofiles = fs::read_dir(DEFAULT_FUMOFILES_DIR) |
|
|
|
|
.expect("Could not read default fumofiles directory"); |
|
|
|
|
for entry in fumofiles { |
|
|
|
|
let file = entry.unwrap(); |
|
|
|
|
let filename = file.file_name(); |
|
|
|
@ -85,7 +87,8 @@ fn main() {
|
|
|
|
|
let fumofile_path = Path::new(&new_fumofiles_dir).join(fumofile_name); |
|
|
|
|
|
|
|
|
|
// read fumofile
|
|
|
|
|
let mut fumofile_contents: String = fs::read_to_string(fumofile_path).expect("Could not find a fumofile!"); |
|
|
|
|
let mut fumofile_contents: String = |
|
|
|
|
fs::read_to_string(fumofile_path).expect("Could not find a fumofile!"); |
|
|
|
|
|
|
|
|
|
// process the message
|
|
|
|
|
let message_values = matches.values_of("message").expect("No message provided"); |
|
|
|
@ -97,5 +100,3 @@ fn main() {
|
|
|
|
|
|
|
|
|
|
println!("{}", fumosay); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|