Skip to content

Commit

Permalink
[apps/cli] Support reading from stdin #249
Browse files Browse the repository at this point in the history
  • Loading branch information
LuoZijun committed Jan 31, 2019
1 parent 6c6702a commit 70494b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 13 additions & 2 deletions apps/cli/src/bin/unic-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ extern crate clap;
#[macro_use]
extern crate unic_cli;

use std::io::{self, Write};
use std::env;
use std::io::{self, Read, Write};

use clap::{Arg, ErrorKind};

Expand Down Expand Up @@ -111,6 +112,7 @@ fn run() -> Result<()> {
.arg(
Arg::with_name("STRINGS")
.multiple(true)
.required(false)
.help("Input strings (expected valid Unicode)"),
)
.arg(
Expand All @@ -133,12 +135,21 @@ fn run() -> Result<()> {
let matches = app.get_matches();

// == Read input ==
let input: String = matches
let mut input: String = matches
.values_of("STRINGS")
.unwrap_or_default()
.collect::<Vec<&str>>()
.join(" ");

if input.len() == 0 {
if let Some(last_arg) = env::args().last() {
if last_arg == "--" {
input.clear();
io::stdin().read_to_string(&mut input).unwrap();
}
}
}

let input_format =
value_t!(matches, "input_format", InputFormat).unwrap_or_else(|err| match err.kind {
ErrorKind::ValueValidation => {
Expand Down
18 changes: 16 additions & 2 deletions apps/cli/src/bin/unic-inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ use prettytable::Table;
use unic::char::property::EnumeratedCharProperty;
use unic::ucd::{name_aliases_of, GeneralCategory, Name, NameAliasType};

use std::env;
use std::io::{self, Read};


fn main() {
let app = app_from_crate!()
.about(concat!(
Expand All @@ -31,17 +35,27 @@ fn main() {
.arg(
Arg::with_name("STRINGS")
.help("Input strings (expected valid Unicode)")
.required(false)
.multiple(true),
);
let matches = app.get_matches();

// == Read input ==
let string: String = matches
let mut input: String = matches
.values_of("STRINGS")
.unwrap_or_default()
.collect::<Vec<&str>>()
.join(" ");

if input.len() == 0 {
if let Some(last_arg) = env::args().last() {
if last_arg == "--" {
input.clear();
io::stdin().read_to_string(&mut input).unwrap();
}
}
}

// == Write output ==
let mut table = Table::new();
let mut table_format = TableFormat::new();
Expand All @@ -58,7 +72,7 @@ fn main() {
]);
*/

string.chars().for_each(|chr| {
input.chars().for_each(|chr| {
let display_name = Name::of(chr).map(|n| n.to_string()).unwrap_or_else(|| {
match name_aliases_of(chr, NameAliasType::NameAbbreviations) {
Some(abbrs) => abbrs[0].to_owned(),
Expand Down

0 comments on commit 70494b6

Please sign in to comment.