aboutsummaryrefslogtreecommitdiffstats
path: root/src/output.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/output.rs')
-rw-r--r--src/output.rs42
1 files changed, 32 insertions, 10 deletions
diff --git a/src/output.rs b/src/output.rs
index 7669f4c..9b72438 100644
--- a/src/output.rs
+++ b/src/output.rs
@@ -1,5 +1,3 @@
-use crate::error::*;
-
use futures_lite::io::AsyncWriteExt as _;
use crate::private::Output as _;
@@ -14,7 +12,10 @@ impl ScreenGuard {
/// Switches the terminal on `stdout` to alternate screen mode and returns
/// a guard object. This is typically called as part of
/// [`Output::new`](Output::new).
- pub async fn new() -> Result<Self> {
+ ///
+ /// # Errors
+ /// * `Error::WriteStdout`: failed to write initialization to stdout
+ pub async fn new() -> crate::error::Result<Self> {
write_stdout(
&mut blocking::Unblock::new(std::io::stdout()),
crate::INIT,
@@ -24,7 +25,10 @@ impl ScreenGuard {
}
/// Switch back from alternate screen mode early.
- pub async fn cleanup(&mut self) -> Result<()> {
+ ///
+ /// # Errors
+ /// * `Error::WriteStdout`: failed to write deinitialization to stdout
+ pub async fn cleanup(&mut self) -> crate::error::Result<()> {
if self.cleaned_up {
return Ok(());
}
@@ -43,6 +47,8 @@ impl Drop for ScreenGuard {
/// call `cleanup` manually instead.
fn drop(&mut self) {
futures_lite::future::block_on(async {
+ // https://github.com/rust-lang/rust-clippy/issues/8003
+ #[allow(clippy::let_underscore_drop)]
let _ = self.cleanup().await;
});
}
@@ -85,7 +91,10 @@ impl crate::Textmode for Output {}
impl Output {
/// Creates a new `Output` instance containing a
/// [`ScreenGuard`](ScreenGuard) instance.
- pub async fn new() -> Result<Self> {
+ ///
+ /// # Errors
+ /// * `Error::WriteStdout`: failed to write initialization to stdout
+ pub async fn new() -> crate::error::Result<Self> {
let mut self_ = Self::new_without_screen();
self_.screen = Some(ScreenGuard::new().await?);
Ok(self_)
@@ -93,6 +102,7 @@ impl Output {
/// Creates a new `Output` instance without creating a
/// [`ScreenGuard`](ScreenGuard) instance.
+ #[must_use]
pub fn new_without_screen() -> Self {
let (rows, cols) = match terminal_size::terminal_size() {
Some((terminal_size::Width(w), terminal_size::Height(h))) => {
@@ -121,7 +131,10 @@ impl Output {
/// Draws the in-memory screen to the terminal on `stdout`. This is done
/// using a diff mechanism to only update the parts of the terminal which
/// are different from the in-memory screen.
- pub async fn refresh(&mut self) -> Result<()> {
+ ///
+ /// # Errors
+ /// * `Error::WriteStdout`: failed to write screen state to stdout
+ pub async fn refresh(&mut self) -> crate::error::Result<()> {
let diff = self.next().screen().state_diff(self.cur().screen());
write_stdout(&mut self.stdout, &diff).await?;
self.cur_mut().process(&diff);
@@ -133,7 +146,10 @@ impl Output {
/// mechanism like `refresh`. This can be useful when the current state of
/// the terminal screen is unknown, such as after the terminal has been
/// resized.
- pub async fn hard_refresh(&mut self) -> Result<()> {
+ ///
+ /// # Errors
+ /// * `Error::WriteStdout`: failed to write screen state to stdout
+ pub async fn hard_refresh(&mut self) -> crate::error::Result<()> {
let contents = self.next().screen().state_formatted();
write_stdout(&mut self.stdout, &contents).await?;
self.cur_mut().process(&contents);
@@ -144,8 +160,14 @@ impl Output {
async fn write_stdout(
stdout: &mut blocking::Unblock<std::io::Stdout>,
buf: &[u8],
-) -> Result<()> {
- stdout.write_all(buf).await.map_err(Error::WriteStdout)?;
- stdout.flush().await.map_err(Error::WriteStdout)?;
+) -> crate::error::Result<()> {
+ stdout
+ .write_all(buf)
+ .await
+ .map_err(crate::error::Error::WriteStdout)?;
+ stdout
+ .flush()
+ .await
+ .map_err(crate::error::Error::WriteStdout)?;
Ok(())
}