Skip to content

Commit cf046e7

Browse files
authored
Add more examples. (#747)
Add a simple `termios` example, and add more output to the stdio and time examples.
1 parent 5b3578e commit cf046e7

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

examples/stdio.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
285285
term.special_codes[SpecialCodeIndex::VTIME],
286286
term.special_codes[SpecialCodeIndex::VMIN]
287287
);
288+
#[cfg(not(any(
289+
bsd,
290+
solarish,
291+
target_os = "aix",
292+
target_os = "haiku",
293+
target_os = "nto",
294+
)))]
295+
println!(
296+
" VSWTC={}",
297+
term.special_codes[SpecialCodeIndex::VSWTC]
298+
);
288299
println!(
289300
" START={} STOP={} SUSP={} EOL={}",
290301
key(term.special_codes[SpecialCodeIndex::VSTART]),

examples/termios.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! A command which demonstrates using `tcgetattr` and `tcsetattr` including
2+
//! enabling raw (press any key) input.
3+
4+
#[cfg(all(not(windows), feature = "termios"))]
5+
fn main() -> std::io::Result<()> {
6+
use rustix::termios::{tcgetattr, tcsetattr, OptionalActions};
7+
use std::io::{Read, Write};
8+
9+
let tty = std::io::stdin();
10+
let termios = tcgetattr(&tty)?;
11+
println!("Original termios: {:?}", termios);
12+
13+
print!("Original settings; enter some text: ");
14+
std::io::stdout().flush()?;
15+
let mut buffer = String::new();
16+
let _input = std::io::stdin().read_line(&mut buffer)?;
17+
18+
tcsetattr(&tty, OptionalActions::Flush, &termios)?;
19+
20+
print!("Reset original settings; enter some text: ");
21+
std::io::stdout().flush()?;
22+
let _input = std::io::stdin().read_line(&mut buffer)?;
23+
24+
let mut raw = termios.clone();
25+
raw.make_raw();
26+
27+
println!("Raw termios: {:?}", raw);
28+
29+
tcsetattr(&tty, OptionalActions::Flush, &raw)?;
30+
31+
print!("Raw settings; press any key...");
32+
std::io::stdout().flush()?;
33+
let mut buf = [0_u8];
34+
let _input = std::io::stdin().read(&mut buf)?;
35+
36+
tcsetattr(&tty, OptionalActions::Flush, &termios)?;
37+
println!();
38+
39+
print!("Reset original settings again; enter some text: ");
40+
std::io::stdout().flush()?;
41+
let _input = std::io::stdin().read_line(&mut buffer)?;
42+
43+
Ok(())
44+
}
45+
46+
#[cfg(any(windows, not(feature = "termios")))]
47+
fn main() {
48+
unimplemented!()
49+
}

examples/time.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,42 @@
44
#[cfg(not(any(windows, target_os = "espidf")))]
55
#[cfg(feature = "time")]
66
fn main() {
7+
use rustix::time::{clock_gettime, ClockId};
8+
9+
println!("Real time: {:?}", clock_gettime(ClockId::Realtime));
10+
println!("Monotonic time: {:?}", clock_gettime(ClockId::Monotonic));
11+
12+
#[cfg(any(freebsdlike, target_os = "openbsd"))]
13+
println!("Uptime: {:?}", clock_gettime(ClockId::Uptime));
14+
15+
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
716
println!(
8-
"Real time: {:?}",
9-
rustix::time::clock_gettime(rustix::time::ClockId::Realtime)
17+
"Process CPU time: {:?}",
18+
clock_gettime(ClockId::ProcessCPUTime)
1019
);
20+
21+
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
22+
println!(
23+
"Thread CPU time: {:?}",
24+
clock_gettime(ClockId::ThreadCPUTime)
25+
);
26+
27+
#[cfg(any(linux_kernel, target_os = "freebsd"))]
28+
println!(
29+
"Realtime (coarse): {:?}",
30+
clock_gettime(ClockId::RealtimeCoarse)
31+
);
32+
33+
#[cfg(any(linux_kernel, target_os = "freebsd"))]
34+
println!(
35+
"Monotonic (coarse): {:?}",
36+
clock_gettime(ClockId::MonotonicCoarse)
37+
);
38+
39+
#[cfg(linux_kernel)]
1140
println!(
12-
"Monotonic time: {:?}",
13-
rustix::time::clock_gettime(rustix::time::ClockId::Monotonic)
41+
"Monotonic (raw): {:?}",
42+
clock_gettime(ClockId::MonotonicRaw)
1443
);
1544
}
1645

0 commit comments

Comments
 (0)