Skip to content

Commit

Permalink
Merge pull request #71 from epage/new
Browse files Browse the repository at this point in the history
fix(stream): Clean up in prep for more auto-detection
  • Loading branch information
epage committed Mar 13, 2023
2 parents 64952c5 + 555d1fd commit f7415fa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
5 changes: 3 additions & 2 deletions crates/anstyle-stream/benches/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ fn stream(c: &mut Criterion) {
group.bench_function("WinconStream", |b| {
b.iter(|| {
let buffer = anstyle_stream::Buffer::with_capacity(content.len());
let mut stream =
anstyle_stream::WinconStream::new(anstyle_wincon::Console::new(buffer));
let mut stream = anstyle_stream::WinconStream::new(
anstyle_wincon::Console::new(buffer).unwrap(),
);

stream.write_all(content).unwrap();

Expand Down
34 changes: 27 additions & 7 deletions crates/anstyle-stream/src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ impl<S> AutoStream<S>
where
S: RawStream,
{
/// Auto-adapt for the stream's capabilities
/// Runtime control over styling behavior
#[cfg(feature = "auto")]
#[inline]
pub fn auto(raw: S) -> Self {
match concolor_override::get() {
pub fn new(raw: S, choice: ColorChoice) -> Self {
match choice {
ColorChoice::Auto => {
if raw.is_terminal() {
Self::always(raw)
Expand All @@ -42,6 +42,13 @@ where
}
}

/// Auto-adapt for the stream's capabilities
#[cfg(feature = "auto")]
#[inline]
pub fn auto(raw: S) -> Self {
Self::new(raw, concolor_override::get())
}

/// Force ANSI escape codes to be passed through as-is, no matter what the inner `Write`
/// supports.
#[inline]
Expand All @@ -67,10 +74,7 @@ where
#[cfg(feature = "wincon")]
{
if raw.is_terminal() && !concolor_query::windows::enable_ansi_colors().unwrap_or(true) {
let console = anstyle_wincon::Console::new(raw);
Self {
inner: StreamInner::Wincon(WinconStream::new(console)),
}
Self::wincon(raw).unwrap_or_else(|raw| Self::always_ansi_(raw))
} else {
Self::always_ansi_(raw)
}
Expand All @@ -86,6 +90,22 @@ where
AutoStream { inner }
}

#[inline]
#[cfg(feature = "wincon")]
fn wincon(raw: S) -> Result<Self, S> {
#[cfg(feature = "wincon")]
{
let console = anstyle_wincon::Console::new(raw)?;
Ok(Self {
inner: StreamInner::Wincon(WinconStream::new(console)),
})
}
#[cfg(not(feature = "wincon"))]
{
Err(raw)
}
}

/// Get the wrapped [`RawStream`]
#[inline]
pub fn into_inner(self) -> S {
Expand Down
8 changes: 4 additions & 4 deletions crates/anstyle-stream/src/wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod test {
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
fn write_all_no_escapes(s in "\\PC*") {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer).unwrap());
stream.write_all(s.as_bytes()).unwrap();
let buffer = stream.into_inner().into_inner();
let actual = std::str::from_utf8(buffer.as_ref()).unwrap();
Expand All @@ -105,7 +105,7 @@ mod test {
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
fn write_byte_no_escapes(s in "\\PC*") {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer).unwrap());
for byte in s.as_bytes() {
stream.write_all(&[*byte]).unwrap();
}
Expand All @@ -118,15 +118,15 @@ mod test {
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
fn write_all_random(s in any::<Vec<u8>>()) {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer).unwrap());
stream.write_all(s.as_slice()).unwrap();
}

#[test]
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
fn write_byte_random(s in any::<Vec<u8>>()) {
let buffer = crate::Buffer::new();
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer));
let mut stream = WinconStream::new(anstyle_wincon::Console::new(buffer).unwrap());
for byte in s.as_slice() {
stream.write_all(&[*byte]).unwrap();
}
Expand Down
3 changes: 2 additions & 1 deletion crates/anstyle-wincon/examples/dump.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
fn main() -> Result<(), lexopt::Error> {
let args = Args::parse()?;
let stdout = std::io::stdout();
let mut stdout = anstyle_wincon::Console::new(stdout.lock());
let mut stdout = anstyle_wincon::Console::new(stdout.lock())
.map_err(|_err| lexopt::Error::from("could not open `stdout` for color control"))?;

for fixed in 0..16 {
let style = style(fixed, args.layer, args.effects);
Expand Down
3 changes: 2 additions & 1 deletion crates/anstyle-wincon/examples/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fn main() {
fn main() -> Result<(), lexopt::Error> {
let args = Args::parse()?;
let stdout = std::io::stdout();
let mut stdout = anstyle_wincon::Console::new(stdout.lock());
let mut stdout = anstyle_wincon::Console::new(stdout.lock())
.map_err(|_err| lexopt::Error::from("could not open `stdout` for color control"))?;

let fg = args.fg.and_then(|c| c.into_ansi());
let bg = args.bg.and_then(|c| c.into_ansi());
Expand Down
13 changes: 9 additions & 4 deletions crates/anstyle-wincon/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ impl<S> Console<S>
where
S: crate::WinconStream + std::io::Write,
{
pub fn new(stream: S) -> Self {
pub fn new(stream: S) -> Result<Self, S> {
// HACK: Assuming the error from `get_colors()` will be present on `write` and doing basic
// ops on the stream will cause the same result
let (initial_fg, initial_bg) = stream.get_colors().unwrap_or_default();
Self {
let (initial_fg, initial_bg) = match stream.get_colors() {
Ok(ok) => ok,
Err(_) => {
return Err(stream);
}
};
Ok(Self {
stream: Some(stream),
initial_fg,
initial_bg,
last_fg: initial_fg,
last_bg: initial_bg,
}
})
}

/// Write colored text to the screen
Expand Down

0 comments on commit f7415fa

Please sign in to comment.