Skip to main content

Term

Struct Term 

Source
pub struct Term {
    pub width: usize,
    pub color: bool,
    pub stream: Stream,
    pub interactive: bool,
}
Expand description

What the output looks like: layout width, colour and destination.

Construct it once per run with Term::stdout, Term::stderr or Term::stderr_forced and pass it to every renderer. Tests use Term::plain and Term::ansi, which never touch the real terminal.

Fields§

§width: usize

Columns available for layout; panels span exactly this many.

§color: bool

Whether renderers should emit ANSI colour and text attributes.

§stream: Stream

Where print_lines writes.

§interactive: bool

Whether standard output is a terminal, so screen control such as clear_screen is allowed.

Implementations§

Source§

impl Term

Source

pub const MIN_WIDTH: usize = 40

Narrowest layout width ever used, however small the terminal reports itself.

Source

pub const FALLBACK_WIDTH: usize = 80

Layout width when no terminal size is available (output is piped, for example).

Source

pub fn stdout() -> Self

A terminal for standard output.

The width is that of the terminal (falling back to Self::FALLBACK_WIDTH, never below Self::MIN_WIDTH). Colour follows anstream’s auto-detection: on for a terminal, off when NO_COLOR is set, CLICOLOR=0 or TERM=dumb, and off when output is redirected (unless CLICOLOR_FORCE is set).

Source

pub fn stderr() -> Self

A terminal for standard error with colour auto-detected from stderr itself.

Source

pub fn stderr_forced() -> Self

A terminal for standard error whose output is replayed to the terminal later.

The shell wrapper captures stderr into a file and prints it when the command ends, so stderr itself is not a terminal and auto-detection would turn colour off. Colour is forced on here unless a terminal that is attached says no: when stdout (or else stderr) is a terminal, its auto-detection decides, which honours NO_COLOR, CLICOLOR=0 and TERM=dumb.

Source

pub fn plain(width: usize) -> Self

A colourless terminal of the given width that is never interactive.

Rendered output contains no escape sequences at all. Meant for tests and for output that is known to be consumed by a program.

Source

pub fn ansi(width: usize) -> Self

Like Term::plain but with colour on, for testing ANSI output.

Source

pub fn with_width(self, width: usize) -> Self

The same terminal with a different layout width, for content that is rendered inside a container (see Panel::body_width).

Source

pub fn paint(&self, style: Style, text: &str) -> String

text in style when colour is on, otherwise unchanged. The result is self-contained.

use shellgame::ui::{Term, style};

assert_eq!(Term::plain(80).paint(style::BOLD, "x"), "x");
assert_eq!(Term::ansi(80).paint(style::BOLD, "x"), "\x1b[1mx\x1b[0m");
Source

pub fn paint_lines(&self, style: Style, lines: &[String]) -> Vec<String>

Applies Term::paint to every line.

Trait Implementations§

Source§

impl Clone for Term

Source§

fn clone(&self) -> Term

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Term

Source§

impl Debug for Term

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Term

Source§

impl PartialEq for Term

Source§

fn eq(&self, other: &Term) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Term

Auto Trait Implementations§

§

impl Freeze for Term

§

impl RefUnwindSafe for Term

§

impl Send for Term

§

impl Sync for Term

§

impl Unpin for Term

§

impl UnsafeUnpin for Term

§

impl UnwindSafe for Term

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.