-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathinteractive.rs
334 lines (301 loc) · 11.6 KB
/
interactive.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// The example only works on Linux since Termion does not yet support
// Windows: https://gitlab.redox-os.org/redox-os/termion/-/issues/103
// The precise library doesn't matter much, so feel free to send a PR
// if there is a library with good Windows support.
fn main() -> Result<(), std::io::Error> {
#[cfg(not(unix))]
panic!("Sorry, this example currently only works on Unix!");
#[cfg(unix)]
unix_only::main()
}
#[cfg(unix)]
mod unix_only {
use std::io::{self, Write};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
use termion::screen::IntoAlternateScreen;
use termion::{color, cursor, style};
use textwrap::{wrap, Options, WordSeparator, WordSplitter, WrapAlgorithm};
#[cfg(feature = "hyphenation")]
use hyphenation::{Language, Load, Standard};
fn draw_margins(
row: u16,
col: u16,
line_width: u16,
left: char,
right: char,
stdout: &mut RawTerminal<io::Stdout>,
) -> Result<(), io::Error> {
write!(
stdout,
"{}{}{}{}",
cursor::Goto(col - 1, row),
color::Fg(color::Red),
left,
color::Fg(color::Reset),
)?;
write!(
stdout,
"{}{}{}{}",
cursor::Goto(col + line_width, row),
color::Fg(color::Red),
right,
color::Fg(color::Reset),
)?;
Ok(())
}
fn draw_text<'a>(
text: &str,
options: &Options<'a>,
word_splitter_label: &str,
stdout: &mut RawTerminal<io::Stdout>,
) -> Result<(), io::Error> {
let mut left_row: u16 = 1;
let left_col: u16 = 3;
write!(stdout, "{}", termion::clear::All)?;
write!(
stdout,
"{}{}Options:{}",
cursor::Goto(left_col, left_row),
style::Bold,
style::Reset,
)?;
left_row += 1;
write!(
stdout,
"{}- width: {}{}{} (use ← and → to change)",
cursor::Goto(left_col, left_row),
style::Bold,
options.width,
style::Reset,
)?;
left_row += 1;
write!(
stdout,
"{}- break_words: {}{:?}{} (toggle with Ctrl-b)",
cursor::Goto(left_col, left_row),
style::Bold,
options.break_words,
style::Reset,
)?;
left_row += 1;
write!(
stdout,
"{}- splitter: {}{}{} (cycle with Ctrl-s)",
cursor::Goto(left_col, left_row),
style::Bold,
word_splitter_label,
style::Reset,
)?;
left_row += 1;
#[cfg(feature = "smawk")]
{
// The OptimalFit struct formats itself with a ton of
// parameters. This removes the parameters, leaving only
// the struct name behind.
let wrap_algorithm_label = format!("{:?}", options.wrap_algorithm)
.split('(')
.next()
.unwrap()
.to_string();
write!(
stdout,
"{}- algorithm: {}{}{} (toggle with Ctrl-o)",
cursor::Goto(left_col, left_row),
style::Bold,
wrap_algorithm_label,
style::Reset,
)?;
left_row += 1;
}
let now = std::time::Instant::now();
let mut lines = wrap(text, options);
let elapsed = now.elapsed();
let right_col: u16 = 55;
let mut right_row: u16 = 1;
write!(
stdout,
"{}{}Performance:{}",
cursor::Goto(right_col, right_row),
style::Bold,
style::Reset,
)?;
right_row += 1;
write!(
stdout,
"{}- build: {}{}{}",
cursor::Goto(right_col, right_row),
style::Bold,
if cfg!(debug_assertions) {
"debug"
} else {
"release"
},
style::Reset,
)?;
right_row += 1;
write!(
stdout,
"{}- words: {}{}{}",
cursor::Goto(right_col, right_row),
style::Bold,
text.split_whitespace().count(),
style::Reset,
)?;
right_row += 1;
write!(
stdout,
"{}- characters: {}{}{}",
cursor::Goto(right_col, right_row),
style::Bold,
text.chars().count(),
style::Reset,
)?;
right_row += 1;
write!(
stdout,
"{}- latency: {}{} usec{}",
cursor::Goto(right_col, right_row),
style::Bold,
elapsed.as_micros(),
style::Reset,
)?;
// Empty line.
left_row += 1;
if let Some(line) = lines.last_mut() {
let trailing_whitespace = &text[text.trim_end_matches(' ').len()..];
if !trailing_whitespace.is_empty() {
// Trailing whitespace is discarded by
// `textwrap::wrap`. We reinsert it here. If multiple
// spaces are added, this can overflow the margins
// which look a bit odd. Handling this would require
// some more tinkering...
*line = format!("{}{}", line, trailing_whitespace).into();
} else if line.ends_with('\n') {
// If `text` ends with a newline, the final wrapped line
// contains this newline. This will in turn leave the
// cursor hanging in the middle of the line. Pushing an
// extra empty line fixes this.
lines.push("".into());
}
} else {
// No lines -> we add an empty line so we have a place
// where we can display the cursor.
lines.push("".into());
}
// Draw margins above and below the wrapped text. We draw the
// margin before the text so that 1) the text can overwrite
// the margin if `break_words` is `false` and `width` is very
// small and 2) so the cursor remains at the end of the last
// line of text.
draw_margins(left_row, left_col, options.width as u16, '┌', '┐', stdout)?;
left_row += 1;
let final_row = left_row + lines.len() as u16;
draw_margins(final_row, left_col, options.width as u16, '└', '┘', stdout)?;
let (_, rows) = termion::terminal_size()?;
write!(stdout, "{}", cursor::Show)?;
for line in lines {
if left_row > rows {
// The text does not fits on the terminal -- we hide
// the cursor since it's supposed to be "below" the
// bottom of the terminal.
write!(stdout, "{}", cursor::Hide)?;
break;
}
draw_margins(left_row, left_col, options.width as u16, '│', '│', stdout)?;
write!(stdout, "{}{}", cursor::Goto(left_col, left_row), line)?;
left_row += 1;
}
stdout.flush()
}
pub fn main() -> Result<(), io::Error> {
let mut wrap_algorithms = Vec::new();
#[cfg(feature = "smawk")]
wrap_algorithms.push(WrapAlgorithm::OptimalFit(
textwrap::wrap_algorithms::Penalties::new(),
));
wrap_algorithms.push(WrapAlgorithm::FirstFit);
let mut word_splitters: Vec<WordSplitter> =
vec![WordSplitter::HyphenSplitter, WordSplitter::NoHyphenation];
let mut word_splitter_labels: Vec<String> =
word_splitters.iter().map(|s| format!("{:?}", s)).collect();
// If you like, you can download more dictionaries from
// https://github.com/tapeinosyne/hyphenation/tree/master/dictionaries
// Place the dictionaries in the examples/ directory. Here we
// just load the embedded en-us dictionary.
#[cfg(feature = "hyphenation")]
for lang in &[Language::EnglishUS] {
let dictionary = Standard::from_embedded(*lang).or_else(|_| {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples")
.join(format!("{}.standard.bincode", lang.code()));
Standard::from_path(*lang, &path)
});
if let Ok(dict) = dictionary {
word_splitters.insert(0, WordSplitter::Hyphenation(dict));
word_splitter_labels.insert(0, format!("{} hyphenation", lang.code()));
}
}
let mut options = Options::new(35)
.break_words(false)
.wrap_algorithm(wrap_algorithms.remove(0))
.word_splitter(word_splitters.remove(0))
.word_separator(WordSeparator::AsciiSpace);
let mut word_splitter_label = word_splitter_labels.remove(0);
let args = std::env::args().collect::<Vec<_>>();
let mut text = if args.len() > 1 {
args[1..].join(" ")
} else {
String::from(
"Welcome to the interactive demo! The following is from The \
Emperor’s New Clothes by Hans Christian Andersen. You can edit the \
text!\n\n\
Many years ago there was an Emperor, who was so excessively fond \
of new clothes that he spent all his money on them. He cared \
nothing about his soldiers, nor for the theatre, nor for driving \
in the woods except for the sake of showing off his new clothes. \
He had a costume for every hour in the day, and instead of saying, \
as one does about any other king or emperor, ‘He is in his council \
chamber,’ here one always said, ‘The Emperor is in his \
dressing-room.’",
)
};
let stdin = io::stdin();
let mut screen = io::stdout().into_raw_mode()?.into_alternate_screen()?;
write!(screen, "{}", cursor::BlinkingUnderline)?;
draw_text(&text, &options, &word_splitter_label, &mut screen)?;
for c in stdin.keys() {
match c? {
Key::Esc | Key::Ctrl('c') => break,
Key::Left => options.width = options.width.saturating_sub(1),
Key::Right => options.width = options.width.saturating_add(1),
Key::Ctrl('b') => options.break_words = !options.break_words,
#[cfg(feature = "smawk")]
Key::Ctrl('o') => {
std::mem::swap(&mut options.wrap_algorithm, &mut wrap_algorithms[0]);
wrap_algorithms.rotate_left(1);
}
Key::Ctrl('s') => {
// We always keep the next splitter at position 0.
std::mem::swap(&mut options.word_splitter, &mut word_splitters[0]);
word_splitters.rotate_left(1);
std::mem::swap(&mut word_splitter_label, &mut word_splitter_labels[0]);
word_splitter_labels.rotate_left(1);
}
Key::Char(c) => text.push(c),
Key::Backspace => {
text.pop();
}
// Also known as Ctrl-Backspace
Key::Ctrl('h') => text.truncate(text.rfind(' ').unwrap_or(0)),
_ => {}
}
draw_text(&text, &options, &word_splitter_label, &mut screen)?;
}
// TODO: change to cursor::DefaultStyle if
// https://github.com/redox-os/termion/pull/157 is merged.
screen.write_all(b"\x1b[0 q")?;
screen.flush()
}
}