TermWriter

Struct TermWriter 

Source
pub struct TermWriter<'a, O>
where O: Write + AsRawFd + 'a,
{ /* private fields */ }

Implementations§

Source§

impl<'a, O> TermWriter<'a, O>
where O: Write + AsRawFd + 'a,

Source

pub fn write_bytes(self, buf: &[u8]) -> Self

Source

pub fn print<T: AsRef<str>>(self, s: T) -> Self

Examples found in repository?
examples/term.rs (line 20)
5pub fn main() {
6    let term = Term::new().unwrap();
7
8    // save the terminal's settings
9    let settings = term.settings();
10
11    term.writer()
12        .bold()
13        .println("Demo Signup Form")
14        .println("")
15        .done()
16        .unwrap();
17
18    term.writer()
19        .foreground("green")
20        .print("\t--> ")
21        .print("Username: ")
22        .done()
23        .unwrap();
24
25    let name = term.readline().unwrap();
26
27    // Turn echo off to hide the password
28    term.update(settings.clone().echo(false)).unwrap();
29
30    term.writer()
31        .foreground("green")
32        .print("\t--> ")
33        .print("Password: ")
34        .done()
35        .unwrap();
36    let password = term.readline().unwrap();
37
38    term.println("");
39
40    term.writer()
41        .foreground("green")
42        .print("\t--> ")
43        .print("Password (Confirm): ")
44        .done()
45        .unwrap();
46    let password2 = term.readline().unwrap();
47
48    // restore the original settings
49    term.update(settings).unwrap();
50
51    term.println("");
52    term.println("");
53
54    if password != password2 {
55        term.writer()
56            .foreground("red")
57            .bold()
58            .println("Passwords don't match!")
59            .done()
60            .unwrap();
61    } else {
62        term.writer()
63            .foreground("cyan")
64            .print("Thank you, ")
65            .foreground("cyan")
66            .bold()
67            .println(name.trim())
68            .done()
69            .unwrap();
70    }
71}
Source

pub fn println<T: AsRef<str>>(self, s: T) -> Self

Examples found in repository?
examples/term.rs (line 13)
5pub fn main() {
6    let term = Term::new().unwrap();
7
8    // save the terminal's settings
9    let settings = term.settings();
10
11    term.writer()
12        .bold()
13        .println("Demo Signup Form")
14        .println("")
15        .done()
16        .unwrap();
17
18    term.writer()
19        .foreground("green")
20        .print("\t--> ")
21        .print("Username: ")
22        .done()
23        .unwrap();
24
25    let name = term.readline().unwrap();
26
27    // Turn echo off to hide the password
28    term.update(settings.clone().echo(false)).unwrap();
29
30    term.writer()
31        .foreground("green")
32        .print("\t--> ")
33        .print("Password: ")
34        .done()
35        .unwrap();
36    let password = term.readline().unwrap();
37
38    term.println("");
39
40    term.writer()
41        .foreground("green")
42        .print("\t--> ")
43        .print("Password (Confirm): ")
44        .done()
45        .unwrap();
46    let password2 = term.readline().unwrap();
47
48    // restore the original settings
49    term.update(settings).unwrap();
50
51    term.println("");
52    term.println("");
53
54    if password != password2 {
55        term.writer()
56            .foreground("red")
57            .bold()
58            .println("Passwords don't match!")
59            .done()
60            .unwrap();
61    } else {
62        term.writer()
63            .foreground("cyan")
64            .print("Thank you, ")
65            .foreground("cyan")
66            .bold()
67            .println(name.trim())
68            .done()
69            .unwrap();
70    }
71}
Source

pub fn bold(self) -> Self

Examples found in repository?
examples/term.rs (line 12)
5pub fn main() {
6    let term = Term::new().unwrap();
7
8    // save the terminal's settings
9    let settings = term.settings();
10
11    term.writer()
12        .bold()
13        .println("Demo Signup Form")
14        .println("")
15        .done()
16        .unwrap();
17
18    term.writer()
19        .foreground("green")
20        .print("\t--> ")
21        .print("Username: ")
22        .done()
23        .unwrap();
24
25    let name = term.readline().unwrap();
26
27    // Turn echo off to hide the password
28    term.update(settings.clone().echo(false)).unwrap();
29
30    term.writer()
31        .foreground("green")
32        .print("\t--> ")
33        .print("Password: ")
34        .done()
35        .unwrap();
36    let password = term.readline().unwrap();
37
38    term.println("");
39
40    term.writer()
41        .foreground("green")
42        .print("\t--> ")
43        .print("Password (Confirm): ")
44        .done()
45        .unwrap();
46    let password2 = term.readline().unwrap();
47
48    // restore the original settings
49    term.update(settings).unwrap();
50
51    term.println("");
52    term.println("");
53
54    if password != password2 {
55        term.writer()
56            .foreground("red")
57            .bold()
58            .println("Passwords don't match!")
59            .done()
60            .unwrap();
61    } else {
62        term.writer()
63            .foreground("cyan")
64            .print("Thank you, ")
65            .foreground("cyan")
66            .bold()
67            .println(name.trim())
68            .done()
69            .unwrap();
70    }
71}
Source

pub fn italics(self) -> Self

Source

pub fn underline(self) -> Self

Source

pub fn invisible(self) -> Self

Source

pub fn standout(self) -> Self

Source

pub fn dim(self) -> Self

Source

pub fn clear(self) -> Self

Source

pub fn done(self) -> Result<usize>

Examples found in repository?
examples/term.rs (line 15)
5pub fn main() {
6    let term = Term::new().unwrap();
7
8    // save the terminal's settings
9    let settings = term.settings();
10
11    term.writer()
12        .bold()
13        .println("Demo Signup Form")
14        .println("")
15        .done()
16        .unwrap();
17
18    term.writer()
19        .foreground("green")
20        .print("\t--> ")
21        .print("Username: ")
22        .done()
23        .unwrap();
24
25    let name = term.readline().unwrap();
26
27    // Turn echo off to hide the password
28    term.update(settings.clone().echo(false)).unwrap();
29
30    term.writer()
31        .foreground("green")
32        .print("\t--> ")
33        .print("Password: ")
34        .done()
35        .unwrap();
36    let password = term.readline().unwrap();
37
38    term.println("");
39
40    term.writer()
41        .foreground("green")
42        .print("\t--> ")
43        .print("Password (Confirm): ")
44        .done()
45        .unwrap();
46    let password2 = term.readline().unwrap();
47
48    // restore the original settings
49    term.update(settings).unwrap();
50
51    term.println("");
52    term.println("");
53
54    if password != password2 {
55        term.writer()
56            .foreground("red")
57            .bold()
58            .println("Passwords don't match!")
59            .done()
60            .unwrap();
61    } else {
62        term.writer()
63            .foreground("cyan")
64            .print("Thank you, ")
65            .foreground("cyan")
66            .bold()
67            .println(name.trim())
68            .done()
69            .unwrap();
70    }
71}
Source

pub fn err(&self) -> &Option<Error>

Source

pub fn written(&self) -> usize

Source

pub fn foreground<T: Into<Color>>(self, color: T) -> Self

Set the terminal’s foreground color.

T a ansi::Color enum, a number (u8) or a string. strings may name a color, or provide custom r, g, and b values. Any of the following are valid:

  • “black”
  • “red”
  • “green”
  • “yellow”
  • “blue”
  • “magenta”
  • “cyan”
  • “grey”
  • “white” (same as “bright grey”)
  • “bright*” (* may be any of the other named colors)
  • “#rrggbb”
  • “#rgb”
  • “rgb(r, g, b)”

Numbers are also valid, the number must fit inside a u8 (so it should be in the range 0-255).

§Compatibility

Everything layed out above will work as far as this library, but the chances of it actually being supported across any meaningful number of terminals is closed to 0.

§24-bit Colors

Full RGB (that is, 24-bit color) terminals are pretty rare. When full 24-bit colors are not supported, the color will get as close as it can with whatever the terminal provides.

§8-bit Colors

256 color terminals are fairly common, using depending on indices beyond 15 is still risky though.

§4/3-bit Colors

Basically all terminals will support 3-bits, many will support 4 (that 4th bit gives the option of a “bright” variant).

Examples found in repository?
examples/term.rs (line 19)
5pub fn main() {
6    let term = Term::new().unwrap();
7
8    // save the terminal's settings
9    let settings = term.settings();
10
11    term.writer()
12        .bold()
13        .println("Demo Signup Form")
14        .println("")
15        .done()
16        .unwrap();
17
18    term.writer()
19        .foreground("green")
20        .print("\t--> ")
21        .print("Username: ")
22        .done()
23        .unwrap();
24
25    let name = term.readline().unwrap();
26
27    // Turn echo off to hide the password
28    term.update(settings.clone().echo(false)).unwrap();
29
30    term.writer()
31        .foreground("green")
32        .print("\t--> ")
33        .print("Password: ")
34        .done()
35        .unwrap();
36    let password = term.readline().unwrap();
37
38    term.println("");
39
40    term.writer()
41        .foreground("green")
42        .print("\t--> ")
43        .print("Password (Confirm): ")
44        .done()
45        .unwrap();
46    let password2 = term.readline().unwrap();
47
48    // restore the original settings
49    term.update(settings).unwrap();
50
51    term.println("");
52    term.println("");
53
54    if password != password2 {
55        term.writer()
56            .foreground("red")
57            .bold()
58            .println("Passwords don't match!")
59            .done()
60            .unwrap();
61    } else {
62        term.writer()
63            .foreground("cyan")
64            .print("Thank you, ")
65            .foreground("cyan")
66            .bold()
67            .println(name.trim())
68            .done()
69            .unwrap();
70    }
71}
Source

pub fn background<T: Into<Color>>(self, color: T) -> Self

Source

pub fn shift_cursor(self, x: isize, y: isize) -> Self

Source

pub fn default_background(self) -> Self

Source

pub fn default_foreground(self) -> Self

Trait Implementations§

Source§

impl<'a, O> Write for TermWriter<'a, O>
where O: Write + AsRawFd,

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<'a, O> !Freeze for TermWriter<'a, O>

§

impl<'a, O> !RefUnwindSafe for TermWriter<'a, O>

§

impl<'a, O> !Send for TermWriter<'a, O>

§

impl<'a, O> Sync for TermWriter<'a, O>
where O: Sync,

§

impl<'a, O> Unpin for TermWriter<'a, O>

§

impl<'a, O> !UnwindSafe for TermWriter<'a, O>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.