Skip to content

Commit

Permalink
Support for stdin #249
Browse files Browse the repository at this point in the history
  • Loading branch information
LuoZijun committed Jan 9, 2019
1 parent 10ca3c8 commit f1c1ff9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
30 changes: 28 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,11 @@ extern crate clap;
#[macro_use]
extern crate unic_cli;

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

use std::time;
use std::thread;
use std::io::{self, Read, Write};
use std::sync::{Arc, Mutex};

use clap::{Arg, ErrorKind};

Expand Down Expand Up @@ -111,6 +115,7 @@ fn run() -> Result<()> {
.arg(
Arg::with_name("STRINGS")
.multiple(true)
.required(false)
.help("Input strings (expected valid Unicode)"),
)
.arg(
Expand All @@ -133,12 +138,33 @@ 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 {
let done = Arc::new(Mutex::new(false));
let done_clone = done.clone();

let handler = thread::spawn(move ||{
let mut input = String::new();
let _ = io::stdin().read_to_string(&mut input);

*done_clone.lock().unwrap() = true;

input
});

thread::sleep(time::Duration::from_millis(300));

if *done.lock().unwrap() {
input = handler.join().unwrap();
}
}


let input_format =
value_t!(matches, "input_format", InputFormat).unwrap_or_else(|err| match err.kind {
ErrorKind::ValueValidation => {
Expand Down
31 changes: 29 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,12 @@ use prettytable::Table;
use unic::char::property::EnumeratedCharProperty;
use unic::ucd::{GeneralCategory, Name};

use std::time;
use std::thread;
use std::io::{self, Read};
use std::sync::{Arc, Mutex};


fn main() {
let app = app_from_crate!()
.about(concat!(
Expand All @@ -31,17 +37,38 @@ 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 {
let done = Arc::new(Mutex::new(false));
let done_clone = done.clone();

let handler = thread::spawn(move ||{
let mut input = String::new();
let _ = io::stdin().read_to_string(&mut input);

*done_clone.lock().unwrap() = true;

input
});

thread::sleep(time::Duration::from_millis(300));

if *done.lock().unwrap() {
input = handler.join().unwrap();
}
}

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

string.chars().for_each(|chr| {
input.chars().for_each(|chr| {
let name = Name::of(chr)
.map(|n| n.to_string())
.unwrap_or_else(|| "<none>".to_owned());
Expand Down

0 comments on commit f1c1ff9

Please sign in to comment.