-
Notifications
You must be signed in to change notification settings - Fork 24
/
unic-echo.rs
225 lines (190 loc) · 6.19 KB
/
unic-echo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2017 The UNIC Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[macro_use]
extern crate clap;
#[macro_use]
extern crate unic_cli;
use std::time;
use std::thread;
use std::io::{self, Read, Write};
use std::sync::{Arc, Mutex};
use clap::{Arg, ErrorKind};
use unic_cli::{parsers, writers, Result};
unic_arg_enum! {
#[derive(Debug)]
enum InputFormat {
Plain,
// Unicode + UTF
Codepoint,
Codepoints,
Utf8Hex,
Utf16Hex
}
}
macro_rules! input_formats_help {
() => {
"INPUT FORMATS:
plain [default] Plain Unicode characters
codepoints Unicode codepoints (hex)
utf8-hex UTF-8 bytes (hex)
utf16-hex UTF-16 words (hex)
"
};
}
unic_arg_enum! {
#[derive(Debug)]
enum OutputFormat {
Plain,
// Unicode + UTF
Codepoint,
Codepoints,
Utf8Hex,
Utf16Hex,
// Braces Escapes
BracesEscape,
Js6Escape,
RustEscape,
// Braces Escape All
BracesEscapeAll,
Js6EscapeAll,
RustEscapeAll,
// Braces Escape Control
BracesEscapeControl,
Js6EscapeControl,
RustEscapeControl
}
}
macro_rules! output_formats_help {
() => {
"OUTPUT FORMATS:
plain [default] Plain Unicode characters
codepoints Unicode codepoints (hex)
utf8-hex UTF-8 bytes (hex)
utf16-hex UTF-16 words (hex)
braces-escape String literal with \\u{...} escapes for
| js6-escape control and non-ASCII characters
| rust-escape
braces-escape-all String literal with \\u{...} escapes for
| js6-escape-all all characters
| rust-escape-all
braces-escape-control String literal with \\u{...} escapes for
| js6-escape-control control characters
| rust-escape-control
"
};
}
fn main() {
run().expect("IO Error");
}
fn run() -> Result<()> {
let app = app_from_crate!()
.about(concat!(
env!("CARGO_PKG_DESCRIPTION"),
"\n\n",
"Write arguments to the standard output",
))
.arg(
Arg::with_name("no_newline")
.short("n")
.long("no-newline")
.help("No trailing newline"),
)
.arg(
Arg::with_name("STRINGS")
.multiple(true)
.required(false)
.help("Input strings (expected valid Unicode)"),
)
.arg(
Arg::with_name("input_format")
.short("i")
.long("input")
.takes_value(true)
.value_name("FORMAT")
.help("Specify input format (see list below)"),
)
.arg(
Arg::with_name("output_format")
.short("o")
.long("output")
.takes_value(true)
.value_name("FORMAT")
.help("Specify output format (see list below)"),
)
.after_help(concat!(input_formats_help!(), "\n", output_formats_help!()));
let matches = app.get_matches();
// == Read input ==
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 => {
eprintln!("{}", matches.usage());
err.exit();
}
_ => InputFormat::Plain,
});
let string: String = match input_format {
InputFormat::Plain => input,
InputFormat::Codepoint | InputFormat::Codepoints => parsers::codepoints(&input),
InputFormat::Utf8Hex => parsers::utf8_hex(&input),
InputFormat::Utf16Hex => parsers::utf16_hex(&input),
};
let chars = string.chars();
// == Write output ==
let mut output = io::stdout();
let output_format =
value_t!(matches, "output_format", OutputFormat).unwrap_or_else(|err| match err.kind {
ErrorKind::ValueValidation => err.exit(),
_ => OutputFormat::Plain,
});
match output_format {
OutputFormat::Plain => write!(output, "{}", string)?,
// Unicode + UTF
OutputFormat::Codepoint | OutputFormat::Codepoints => {
writers::write_as_codepoints(&mut output, chars)?
}
OutputFormat::Utf8Hex => writers::write_as_utf8_hex(&mut output, chars)?,
OutputFormat::Utf16Hex => writers::write_as_utf16_hex(&mut output, chars)?,
// Escapes
OutputFormat::BracesEscape | OutputFormat::Js6Escape | OutputFormat::RustEscape => {
writers::write_with_control_n_unicode_braces_escape(&mut output, chars)?
}
OutputFormat::BracesEscapeAll
| OutputFormat::Js6EscapeAll
| OutputFormat::RustEscapeAll => writers::write_with_all_braces_escape(&mut output, chars)?,
OutputFormat::BracesEscapeControl
| OutputFormat::Js6EscapeControl
| OutputFormat::RustEscapeControl => {
writers::write_with_control_braces_escape(&mut output, chars)?
}
};
if !matches.is_present("no_newline") {
writeln!(output)?;
}
Ok(())
}