Skip to main content

Line

Struct Line 

Source
pub struct Line { /* private fields */ }
Expand description

A list of spans that sit on one terminal line.

The text of a line is the text of its spans, joined. Its Display prints that text without styles, which suits tests and plain output. Build one with Line::from(span), or collect spans into it.

Implementations§

Source§

impl Line

Source

pub fn new() -> Self

An empty line.

Source

pub fn push(&mut self, span: Span)

Adds span at the end of the line.

A span in the style of the line’s last span joins it. So two lines with the same text and styles are equal, however their spans were pushed.

The start of span can continue the line’s last grapheme cluster, as a combining mark or the rest of a ZWJ emoji sequence does. Those characters join the previous span and take its style, because a terminal cannot style part of a cluster.

So no cluster crosses a span boundary, and every width counts the cluster once. An empty span adds nothing, so none can sit between a cluster’s parts.

Source

pub fn spans(&self) -> &[Span]

The spans, in order. Their text joined is the text of the line.

Source

pub fn width(&self) -> u16

The width of the line in terminal cells. A width over u16::MAX saturates.

Source

pub fn is_empty(&self) -> bool

Whether the line holds no text.

Source

pub fn trim_end(&mut self)

Removes the spaces at the end of the line, and any span they empty.

A no-break space (U+00A0, U+2007, or U+202F) is not a space here, so it stays.

Source§

impl Line

Source

pub fn truncate(&self, width: u16) -> Self

The line cut to exactly width cells and ended with ELLIPSIS, or the line itself when it fits.

The text is cut at a cluster boundary. A wide cluster that straddles the cut is replaced by a space, so a cut line is never narrower than width. The padding and the ellipsis take the style of the text they replace. A width of 0 gives an empty line.

Source

pub fn fit(&self, width: u16, overflow: Overflow) -> Vec<Self>

The line fitted to width cells by overflow: wrapped into lines for Overflow::KeepWords and Overflow::Fold, or cut into one line for Overflow::Ellipsis.

A wrapped line never ends in a space: the spaces at the end of the line and at each break are dropped. An ellipsis line that fits keeps the spaces it ends with, as Line::truncate does. Call trim_end first; Text does this for you. A width of 0 counts as 1 when wrapping.

Trait Implementations§

Source§

impl Clone for Line

Source§

fn clone(&self) -> Self

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 Debug for Line

Source§

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

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

impl Default for Line

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Line

Source§

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

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

impl Eq for Line

Source§

impl Extend<Span> for Line

Source§

fn extend<I: IntoIterator<Item = Span>>(&mut self, spans: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<Span> for Line

Source§

fn from(span: Span) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Span> for Line

Source§

fn from_iter<I: IntoIterator<Item = Span>>(spans: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for Line

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Line

Source§

fn eq(&self, other: &Self) -> 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 Line

Auto Trait Implementations§

§

impl Forget for Line

§

impl Freeze for Line

§

impl Move for Line

§

impl RefUnwindSafe for Line

§

impl Send for Line

§

impl Sync for Line

§

impl Unpin for Line

§

impl UnsafeUnpin for Line

§

impl UnwindSafe for Line

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<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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.