Skip to content

Commit

Permalink
feat: add writer() and writer_mut() to termion and crossterm backends (
Browse files Browse the repository at this point in the history
…#991)

It is sometimes useful to obtain access to the writer if we want to see
what has been written so far. For example, when using &mut [u8] as a
writer.

Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
  • Loading branch information
enricozb and joshka authored May 21, 2024
1 parent fadc73d commit 0b5fd6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ where
pub const fn new(writer: W) -> Self {
Self { writer }
}

/// Gets the writer.
#[stability::unstable(
feature = "backend-writer",
issue = "https://github.com/ratatui-org/ratatui/pull/991"
)]
pub const fn writer(&self) -> &W {
&self.writer
}

/// Gets the writer as a mutable reference.
///
/// Note: writing to the writer may cause incorrect output after the write. This is due to the
/// way that the Terminal implements diffing Buffers.
#[stability::unstable(
feature = "backend-writer",
issue = "https://github.com/ratatui-org/ratatui/pull/991"
)]
pub fn writer_mut(&mut self) -> &mut W {
&mut self.writer
}
}

impl<W> Write for CrosstermBackend<W>
Expand Down
20 changes: 20 additions & 0 deletions src/backend/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ where
pub const fn new(writer: W) -> Self {
Self { writer }
}

/// Gets the writer.
#[stability::unstable(
feature = "backend-writer",
issue = "https://github.com/ratatui-org/ratatui/pull/991"
)]
pub const fn writer(&self) -> &W {
&self.writer
}

/// Gets the writer as a mutable reference.
/// Note: writing to the writer may cause incorrect output after the write. This is due to the
/// way that the Terminal implements diffing Buffers.
#[stability::unstable(
feature = "backend-writer",
issue = "https://github.com/ratatui-org/ratatui/pull/991"
)]
pub fn writer_mut(&mut self) -> &mut W {
&mut self.writer
}
}

impl<W> Write for TermionBackend<W>
Expand Down

0 comments on commit 0b5fd6b

Please sign in to comment.