https://github.com/heaths/console-rs
Rust console APIs provide a testable abstraction over IO streams and support for color schemes.
https://github.com/heaths/console-rs
Last synced: about 2 months ago
JSON representation
Rust console APIs provide a testable abstraction over IO streams and support for color schemes.
- Host: GitHub
- URL: https://github.com/heaths/console-rs
- Owner: heaths
- License: mit
- Created: 2024-05-05T03:45:37.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-06T04:15:56.000Z (about 2 years ago)
- Last Synced: 2025-01-01T10:16:49.639Z (over 1 year ago)
- Language: Rust
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Rust Console APIs
[](https://github.com/heaths/console-rs/actions/workflows/ci.yml)
These Rust Console APIs provide a level of abstraction over virtual terminals, implement testable 'std::io::Write'
and will support color schemes.
_This name not final, but aligns with my [github.com/heaths/go-console](https://github.com/heaths/go-console) Go APIs._
## Examples
For all supported versions of Windows, you can enable [ANSI escape sequences](https://learn.microsoft.com/windows/console/console-virtual-terminal-sequences).
This should be unnecessary for other operating systems but the function is conveniently defined for all platforms.
```rust
console::enable_ansi_escape_sequences(std::io::stdout()).unwrap();
println!("\x1b[38;5;128mHello, world!\x1b[m");
```
You can also use the `Console` to write code for production and test:
```rust
use console::console::Console;
fn print(mut w: impl std::io::Write, s: &str) {
let _ = writeln!(w, "printing: {s}");
}
fn main() {
let mut console = Console::from_system().unwrap();
print(console, "sales receipt");
}
#[cfg(test)]
mod tests {
#[test]
fn prints_prefix() {
let mut stdout: Vec = Vec::new();
let mut console = Console::builder().stdout(&mut stdout).build();
print(console, "test");
assert_eq!("printing: test\n".as_bytes(), stdout.as_slice());
}
}
```
## License
Licensed under the [MIT](LICENSE.txt) license.