Struct TermBuffer

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

Represents a range of lines in a terminal and the cursor position. This is suitable when you don’t want to use an “alternate screen”, but rather retain previous terminal output, such as shell prompts/responses.

New frames are rendered by replacing the lines. All operations work on a relative coordinate system where (0, 0) is the top-left corner of the lines TermBuffer controls.

Further, we never check the actual cursor position, but rather move the cursor relative to its current position. The meaning of (0, 0) is actually the cursor position when TermBuffer first renders.

Implementations§

Source§

impl TermBuffer

Source

pub fn new() -> Self

Source

pub fn push_line(&mut self, row: impl Into<String>)

Add a row to the desired output

Examples found in repository?
examples/term-buffer-triangle.rs (line 47)
37pub fn main() {
38    let mut state = State(0, Direction::Grow);
39    let mut buf = TermBuffer::default();
40
41    loop {
42        let count = state.update();
43        // let count = MAX;
44
45        for i in 0..count {
46            let line = format!("|{}\\", "*".repeat(i));
47            buf.push_line(line);
48        }
49        buf.push_line(format!(" {}", "‾".repeat(count - 1)));
50
51        buf.render_frame();
52        buf.flush();
53        // x
54        std::thread::sleep(std::time::Duration::from_millis(15));
55    }
56}
Source

pub fn lines(&self) -> u16

Source

pub fn set_next_cursor(&mut self, cursor: (u16, u16))

Positions the cursor where (0, 0) is the first character printed by this program

Source

pub fn forget(&mut self) -> usize

This causes us to skip past the currently displayed buffer area and forget about it, resulting in future renders to happen below it. If this is called, and then the TermBuffer is dropped, the default behavior of clearing the area will be a no-op.

Source

pub fn render_frame(&mut self)

Perform the necessary update to the terminal. This may choose a more optimized update than a full frame.

Examples found in repository?
examples/term-buffer-triangle.rs (line 51)
37pub fn main() {
38    let mut state = State(0, Direction::Grow);
39    let mut buf = TermBuffer::default();
40
41    loop {
42        let count = state.update();
43        // let count = MAX;
44
45        for i in 0..count {
46            let line = format!("|{}\\", "*".repeat(i));
47            buf.push_line(line);
48        }
49        buf.push_line(format!(" {}", "‾".repeat(count - 1)));
50
51        buf.render_frame();
52        buf.flush();
53        // x
54        std::thread::sleep(std::time::Duration::from_millis(15));
55    }
56}
Source

pub fn render_one_line(&mut self, line_index: usize)

Source

pub fn render_full(&mut self)

Renders a complete frame to the terminal

Source

pub fn flush(&mut self)

Examples found in repository?
examples/term-buffer-triangle.rs (line 52)
37pub fn main() {
38    let mut state = State(0, Direction::Grow);
39    let mut buf = TermBuffer::default();
40
41    loop {
42        let count = state.update();
43        // let count = MAX;
44
45        for i in 0..count {
46            let line = format!("|{}\\", "*".repeat(i));
47            buf.push_line(line);
48        }
49        buf.push_line(format!(" {}", "‾".repeat(count - 1)));
50
51        buf.render_frame();
52        buf.flush();
53        // x
54        std::thread::sleep(std::time::Duration::from_millis(15));
55    }
56}

Trait Implementations§

Source§

impl Default for TermBuffer

Source§

fn default() -> Self

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

impl Drop for TermBuffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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.