Skip to content

Commit 0a8a92b

Browse files
sytsereitsmaestk
authored andcommitted
Reuse the writer creation code
1 parent a5c309b commit 0a8a92b

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

src/encode/writer/console.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,13 @@ mod imp {
319319
}
320320

321321
impl Writer {
322-
pub fn stdout() -> Option<Writer> {
322+
fn create_writer(inner_writer: StdWriter) -> Option<Writer> {
323323
unsafe {
324-
let handle = processenv::GetStdHandle(winbase::STD_OUTPUT_HANDLE);
324+
let handle = match inner_writer {
325+
StdWriter::Stdout(_) => processenv::GetStdHandle(winbase::STD_OUTPUT_HANDLE),
326+
StdWriter::Stderr(_) => processenv::GetStdHandle(winbase::STD_ERROR_HANDLE),
327+
};
328+
325329
if handle.is_null() || handle == handleapi::INVALID_HANDLE_VALUE {
326330
return None;
327331
}
@@ -336,7 +340,7 @@ mod imp {
336340
handle,
337341
defaults: info.wAttributes,
338342
},
339-
inner: StdWriter::stdout(),
343+
inner: inner_writer,
340344
};
341345

342346
match color_mode() {
@@ -346,31 +350,12 @@ mod imp {
346350
}
347351
}
348352

349-
pub fn stderr() -> Option<Writer> {
350-
unsafe {
351-
let handle = processenv::GetStdHandle(winbase::STD_ERROR_HANDLE);
352-
if handle.is_null() || handle == handleapi::INVALID_HANDLE_VALUE {
353-
return None;
354-
}
355-
356-
let mut info = mem::zeroed();
357-
if wincon::GetConsoleScreenBufferInfo(handle, &mut info) == 0 {
358-
return None;
359-
}
360-
361-
let writer = Writer {
362-
console: RawConsole {
363-
handle,
364-
defaults: info.wAttributes,
365-
},
366-
inner: StdWriter::stderr(),
367-
};
353+
pub fn stdout() -> Option<Writer> {
354+
Self::create_writer(StdWriter::stdout())
355+
}
368356

369-
match color_mode() {
370-
ColorMode::Auto | ColorMode::Always => Some(writer),
371-
ColorMode::Never => None,
372-
}
373-
}
357+
pub fn stderr() -> Option<Writer> {
358+
Self::create_writer(StdWriter::stderr())
374359
}
375360

376361
pub fn lock<'a>(&'a self) -> WriterLock<'a> {

0 commit comments

Comments
 (0)