12 releases (4 breaking)
Uses old Rust 2015
0.5.2 | Oct 19, 2017 |
---|---|
0.5.0 | Sep 24, 2017 |
0.4.2 | Jul 25, 2017 |
#8 in #styled
185KB
4.5K
SLoC
stijl
Cross-platform simple styled text streams.
Usage
Add this to your Cargo.toml
:
[dependencies]
stijl = "0.5.2"
and this to your crate root:
extern crate stijl;
Examples
lib.rs
:
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).