Crate stijl [−] [src]
Simple styled text streams.
Basic usage
stdout()
and stderr()
are
drop-in replacements for std::io::stdout()
and
std::io::stderr()
, which wrap them in a
CLIStream
that supports eight foreground
colors and emphasized text.
Example
use std::io::Write; use stijl::{CLIStream, DoStyle, Red}; let stream = &mut stijl::stdout(DoStyle::Auto); stream.fg(Red); stream.em(); write!(stream, "Warning: "); stream.reset(); writeln!(stream, " this text is red.");
Animations
The get_size
and
rewind_lines
methods of CLIStream
can help make
progress bars, spinners, and other simple animations.
Example
use stijl::{CLIStream, DoStyle}; use std::{time, thread}; let delay = time::Duration::from_millis(100); let stream = &mut stijl::stdout(DoStyle::Auto); let max = stream.get_size().cols as usize; let mut pos = 0; for _ in 0..1000 { // draw_indicator is left as an exercise for the reader draw_indicator(stream, pos, max); thread::sleep(delay); if max != 0 { pos = (pos + 1) % max; } stream.rewind_lines(1); }
Multithreading
The objects returned by stdout()
and
stderr()
implement
LockableStream
, with a
lock
method for
synchronizing the stream.
To reduce contention, multiple threads can write to their own
BufStream
objects and when finished,
print them to a LockableStream
.
Example
use stijl::{BufStream, DoStyle}; let mut buf = BufStream::new(); // ... // Do work // Write to buf // ... let stream = &mut stijl::stdout(DoStyle::Auto); let stream = &mut stream.lock(); buf.playback(stream)?;
Platform notes
stdout
and stderr
return a
TermStream
object for terminals that understand terminfo-style
escape sequences, including the Cygwin and MSYS terminals, and the
Windows 10 console (Anniversary Update or newer). They return a
ConStream
struct for consoles in earlier versions of Windows.
On Windows, the same binary will produce equivalent styled output
in either the console or a Cygwin terminal. However, in Cygwin,
CLIStream::get_size()
currently always returns the default size (80x24).
Structs
BufStream |
A |
Color |
A terminal color. |
Error |
An error that occurred writing to a |
TermStream |
A styled stream using terminfo escape sequences. |
WinSize |
The dimensions of a terminal window. |
Enums
DoStyle |
Strategies for applying styles to text. |
TerminalMode |
The type of terminal, if any, for a standard stream handle. |
Constants
Black |
Color 0. |
Blue |
Color 4 (color 1 in Windows console) |
Cyan |
Color 6 (color 3 in Windows console) |
Green |
Color 2 |
Magenta |
Color 5 |
Red |
Color 1 (color 4 in Windows console) |
White |
Color 7 |
Yellow |
Color 3 (color 6 in Windows console) |
Traits
CLIStream |
A |
LockableStream |
A |
Stream |
An output stream with simple styling. |
Functions
stderr |
A |
stdout |
A |
Type Definitions
Result |
Either success or failure. |