Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/src/term/out.rs

1.2 KiB, 1 run

created by r1870400018:77099, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Text written to standard output, or any other writer, as a fallible step: each write is
2//! flushed at once, and a failure is an error tagged `IO, Output, Write`, so that
3//! [`Error::hung_up`](crate::error::Error::hung_up) tells a reader that has gone, as `| head`
4//! does, from a real fault. `println!` panics then.
5
6use crate::prelude::*;
7
8use std::io::{
9 Stdout,
10 Write,
11};
12
13
14pub struct Out<W: Write> {
15 w: W,
16}
17
18impl Out<Stdout> {
19 pub fn stdout() -> Self {
20 Self::new(std::io::stdout())
21 }
22}
23
24impl<W: Write> Out<W> {
25
26 pub fn new(w: W) -> Self {
27 Self { w }
28 }
29
30 pub fn into_inner(self) -> W {
31 self.w
32 }
33
34 /// Writes `text` as it stands and flushes it.
35 pub fn put(&mut self, text: &str) -> Outcome<()> {
36 res!(self.w.write_all(text.as_bytes()), IO, Output, Write);
37 res!(self.w.flush(), IO, Output, Write);
38 Ok(())
39 }
40
41 /// Writes `text` and a newline, and flushes them.
42 pub fn line(&mut self, text: &str) -> Outcome<()> {
43 res!(self.w.write_all(text.as_bytes()), IO, Output, Write);
44 res!(self.w.write_all(b"\n"), IO, Output, Write);
45 res!(self.w.flush(), IO, Output, Write);
46 Ok(())
47 }
48}