Skip to content

Commit

Permalink
Use subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kogai committed May 24, 2018
1 parent a453d00 commit ff082a4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trs"
version = "0.4.5"
version = "0.5.0"
authors = ["shinichi kogai <[email protected]>"]
categories = ["command-line-utilities"]

Expand Down
149 changes: 86 additions & 63 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,71 @@ mod oxford;
mod translate;
mod utils;

use clap::{App, Arg};
use clap::{App, Arg, SubCommand};
use std::io::{self, Write};
use std::process::exit;
use utils::span_of;

fn translate_between(
query_words: Vec<String>,
target_language: String,
namespace: cache::Namespace,
fs_cache: &mut cache::FSCache,
) -> String {
let query_text = query_words.join(" ");
match fs_cache.get(&namespace, &query_text) {
Some(definitions) => definitions,
None => {
let new_def = translate::translate(&target_language, &query_text);
fs_cache.set(&namespace, &query_text, &new_def);
new_def
}
}
}

fn run() {
let matches = App::new(crate_name!())
.version(crate_version!())
.about("CLI for English learners")
.about("CLI tool for English learners")
.arg(
Arg::with_name("languages")
.long("languages")
.short("l")
.help("See the list of languages"),
)
.arg(Arg::with_name("cat").help("Show current cache").short("c"))
.arg(
Arg::with_name("dictionary")
.help("See formal English definition of the words")
.short("d")
.takes_value(true)
.multiple(true),
)
.arg(
Arg::with_name("change-language")
.help("Change the language correspoinding to english")
.short("C")
.takes_value(true),
)
.arg(
Arg::with_name("from-target-language")
.help("Set the words that translate from target language to english")
.short("f")
.takes_value(true)
.multiple(true),
)
.arg(
Arg::with_name("to-target-language")
.help("Set the words that translate to")
.short("t")
.takes_value(true)
.multiple(true),
)
.subcommands(vec![
SubCommand::with_name("d")
.about("See formal English definition of the words")
.arg(
Arg::with_name("dictionary")
.index(1)
.takes_value(true)
.multiple(true),
),
SubCommand::with_name("f")
.about("Set the words that translate from target language to english")
.arg(
Arg::with_name("from-target-language")
.index(1)
.takes_value(true)
.multiple(true),
),
SubCommand::with_name("t")
.about("Set the words that translate to")
.arg(
Arg::with_name("to-target-language")
.index(1)
.takes_value(true)
.multiple(true),
),
])
.get_matches();

let mut fs_cache = cache::FSCache::new();
Expand All @@ -77,54 +100,54 @@ fn run() {
exit(0);
}

if matches.is_present("dictionary") {
let namespace = cache::Namespace::Dictionary;
let query_words = values_t!(matches.values_of("dictionary"), String).unwrap_or(vec![]);
let escaped_query_words = utils::space_to_underscore(&query_words.join(" "));
let definitions = match fs_cache.get(&namespace, &escaped_query_words) {
Some(definitions) => definitions,
None => {
let new_def = oxford::definitions(query_words);
fs_cache.set(&namespace, &escaped_query_words, &new_def);
new_def
}
};
println!("{}", definitions);
let _ = fs_cache.garbage_colloect();
exit(0);
};

if matches.is_present("cat") {
let cache = fs_cache.get_all();
println!("{}", cache);
exit(0);
};

let namespace = cache::Namespace::Translate;
let (query_words, target_language) = if matches.is_present("to-target-language") {
(
values_t!(matches.values_of("to-target-language"), String).unwrap_or(vec![]),
default_language,
)
} else if matches.is_present("from-target-language") {
(
values_t!(matches.values_of("from-target-language"), String).unwrap_or(vec![]),
"en".to_owned(),
)
} else {
unreachable!()
};

let query_text = query_words.join(" ");
let translated = match fs_cache.get(&namespace, &query_text) {
Some(definitions) => definitions,
None => {
let new_def = translate::translate(&target_language, &query_text);
fs_cache.set(&namespace, &query_text, &new_def);
new_def
let result = match matches.subcommand() {
("d", Some(cmd)) => {
let namespace = cache::Namespace::Dictionary;
let query_words = values_t!(cmd.values_of("dictionary"), String).unwrap_or(vec![]);
println!("{:#?}", query_words);
let escaped_query_words = utils::space_to_underscore(&query_words.join(" "));
let definitions = match fs_cache.get(&namespace, &escaped_query_words) {
Some(definitions) => definitions,
None => {
let new_def = oxford::definitions(query_words);
fs_cache.set(&namespace, &escaped_query_words, &new_def);
new_def
}
};
definitions
}
("t", Some(cmd)) => {
let query_words =
values_t!(cmd.values_of("to-target-language"), String).unwrap_or(vec![]);
let target_language = default_language;
translate_between(
query_words,
target_language,
cache::Namespace::Translate,
&mut fs_cache,
)
}
("f", Some(cmd)) => {
let query_words =
values_t!(cmd.values_of("from-target-language"), String).unwrap_or(vec![]);
let target_language = "en".to_owned();
translate_between(
query_words,
target_language,
cache::Namespace::Translate,
&mut fs_cache,
)
}
_ => unreachable!(),
};
println!("{}", translated);

let _ = io::stdout().write(result.as_bytes());
let _ = span_of("garbage_collect", || fs_cache.garbage_colloect());
}

Expand Down

0 comments on commit ff082a4

Please sign in to comment.