Skip to content

Commit f1c1ff9

Browse files
committed
Support for stdin #249
1 parent 10ca3c8 commit f1c1ff9

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

apps/cli/src/bin/unic-echo.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ extern crate clap;
1414
#[macro_use]
1515
extern crate unic_cli;
1616

17-
use std::io::{self, Write};
17+
18+
use std::time;
19+
use std::thread;
20+
use std::io::{self, Read, Write};
21+
use std::sync::{Arc, Mutex};
1822

1923
use clap::{Arg, ErrorKind};
2024

@@ -111,6 +115,7 @@ fn run() -> Result<()> {
111115
.arg(
112116
Arg::with_name("STRINGS")
113117
.multiple(true)
118+
.required(false)
114119
.help("Input strings (expected valid Unicode)"),
115120
)
116121
.arg(
@@ -133,12 +138,33 @@ fn run() -> Result<()> {
133138
let matches = app.get_matches();
134139

135140
// == Read input ==
136-
let input: String = matches
141+
let mut input: String = matches
137142
.values_of("STRINGS")
138143
.unwrap_or_default()
139144
.collect::<Vec<&str>>()
140145
.join(" ");
141146

147+
if input.len() == 0 {
148+
let done = Arc::new(Mutex::new(false));
149+
let done_clone = done.clone();
150+
151+
let handler = thread::spawn(move ||{
152+
let mut input = String::new();
153+
let _ = io::stdin().read_to_string(&mut input);
154+
155+
*done_clone.lock().unwrap() = true;
156+
157+
input
158+
});
159+
160+
thread::sleep(time::Duration::from_millis(300));
161+
162+
if *done.lock().unwrap() {
163+
input = handler.join().unwrap();
164+
}
165+
}
166+
167+
142168
let input_format =
143169
value_t!(matches, "input_format", InputFormat).unwrap_or_else(|err| match err.kind {
144170
ErrorKind::ValueValidation => {

apps/cli/src/bin/unic-inspector.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ use prettytable::Table;
2121
use unic::char::property::EnumeratedCharProperty;
2222
use unic::ucd::{GeneralCategory, Name};
2323

24+
use std::time;
25+
use std::thread;
26+
use std::io::{self, Read};
27+
use std::sync::{Arc, Mutex};
28+
29+
2430
fn main() {
2531
let app = app_from_crate!()
2632
.about(concat!(
@@ -31,17 +37,38 @@ fn main() {
3137
.arg(
3238
Arg::with_name("STRINGS")
3339
.help("Input strings (expected valid Unicode)")
40+
.required(false)
3441
.multiple(true),
3542
);
3643
let matches = app.get_matches();
3744

3845
// == Read input ==
39-
let string: String = matches
46+
let mut input: String = matches
4047
.values_of("STRINGS")
4148
.unwrap_or_default()
4249
.collect::<Vec<&str>>()
4350
.join(" ");
4451

52+
if input.len() == 0 {
53+
let done = Arc::new(Mutex::new(false));
54+
let done_clone = done.clone();
55+
56+
let handler = thread::spawn(move ||{
57+
let mut input = String::new();
58+
let _ = io::stdin().read_to_string(&mut input);
59+
60+
*done_clone.lock().unwrap() = true;
61+
62+
input
63+
});
64+
65+
thread::sleep(time::Duration::from_millis(300));
66+
67+
if *done.lock().unwrap() {
68+
input = handler.join().unwrap();
69+
}
70+
}
71+
4572
// == Write output ==
4673
let mut table = Table::new();
4774
let mut table_format = TableFormat::new();
@@ -58,7 +85,7 @@ fn main() {
5885
]);
5986
*/
6087

61-
string.chars().for_each(|chr| {
88+
input.chars().for_each(|chr| {
6289
let name = Name::of(chr)
6390
.map(|n| n.to_string())
6491
.unwrap_or_else(|| "<none>".to_owned());

0 commit comments

Comments
 (0)