Skip to content

Commit

Permalink
* update TerminalAccess::print to accept String as well as str
Browse files Browse the repository at this point in the history
* update prettyio demo to use print method
  • Loading branch information
apparebit committed Nov 1, 2024
1 parent 017e8e7 commit 59e1294
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
18 changes: 5 additions & 13 deletions src/prettyio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,27 @@ pub fn main() -> Result<()> {
let mut tty = terminal().access()?;
let mut entries = ThemeEntry::all();

write!(
tty,
"press ‹t› to query rotating theme color, ‹q› to quit\r\n\r\n"
)?;
tty.flush()?;
tty.print("press ‹t› to query rotating theme color, ‹q› to quit\r\n\r\n")?;

let mut iterations = 0;
let mut line = 0;
loop {
iterations += 1;
if 1000 <= iterations {
write!(tty, "✋")?;
tty.flush()?;
tty.print("✋")?;
break;
}

if 70 < line {
write!(tty, "\r\n")?;
tty.print("\r\n")?;
line = 0;
tty.flush()?;
}

let mut buffer = [0; 32];
let count = tty.read(&mut buffer)?;
if count == 0 {
write!(tty, "◦")?;
tty.print("◦")?;
line += 1;
tty.flush()?;
continue;
}

Expand All @@ -63,9 +56,8 @@ pub fn main() -> Result<()> {
}
}

write!(tty, "〉")?;
tty.print("〉")?;
line += 2;
tty.flush()?;

if terminate {
break;
Expand Down
5 changes: 3 additions & 2 deletions src/term/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::AsRef;
use std::fs::OpenOptions;
use std::io::{BufRead, BufReader, BufWriter, ErrorKind, IoSlice, IoSliceMut, Read, Result, Write};
use std::num::NonZeroU8;
Expand Down Expand Up @@ -285,8 +286,8 @@ impl TerminalAccess<'_> {
}

/// Write the entire string slice and flush thereafter.
pub fn print(&mut self, s: &str) -> Result<()> {
self.write_all(s.as_bytes())?;
pub fn print(&mut self, s: impl AsRef<str>) -> Result<()> {
self.write_all(s.as_ref().as_bytes())?;
self.flush()
}
}
Expand Down

0 comments on commit 59e1294

Please sign in to comment.