From 432cf481fec727b7b3d29a3c86b56c747780bd7c Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh Date: Mon, 20 Aug 2018 20:46:00 +0300 Subject: [PATCH] Use WinConsoleInfo as a field in WinConsole Signed-off-by: Mohammad AlSaleh --- src/win.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/win.rs b/src/win.rs index 6c94e667..36a8a6b7 100644 --- a/src/win.rs +++ b/src/win.rs @@ -45,10 +45,7 @@ pub struct WinConsoleInfo { /// A Terminal implementation which uses the Win32 Console API. pub struct WinConsole { buf: T, - def_foreground: color::Color, - def_background: color::Color, - foreground: color::Color, - background: color::Color, + info: WinConsoleInfo, } fn color_to_bits(color: color::Color) -> u16 { @@ -173,8 +170,8 @@ impl WinConsole { let out = conout()?; let _unused = self.buf.flush(); let mut accum: WORD = 0; - accum |= color_to_bits(self.foreground); - accum |= color_to_bits(self.background) << 4; + accum |= color_to_bits(self.info.foreground); + accum |= color_to_bits(self.info.background) << 4; unsafe { SetConsoleTextAttribute(*out, accum); } @@ -182,21 +179,18 @@ impl WinConsole { } /// Create a new WinConsole with the given WinConsoleInfo and out - pub fn new_with_consoleinfo(out: T, consoleinfo: WinConsoleInfo) -> WinConsole { + pub fn new_with_consoleinfo(out: T, info: WinConsoleInfo) -> WinConsole { WinConsole { buf: out, - def_foreground: consoleinfo.def_foreground, - def_background: consoleinfo.def_background, - foreground: consoleinfo.foreground, - background: consoleinfo.background, + info, } } /// Returns `Err` whenever the terminal cannot be created for some /// reason. pub fn new(out: T) -> io::Result> { - let consoleinfo = WinConsoleInfo::from_env()?; - Ok(Self::new_with_consoleinfo(out, consoleinfo)) + let info = WinConsoleInfo::from_env()?; + Ok(Self::new_with_consoleinfo(out, info)) } } @@ -214,14 +208,14 @@ impl Terminal for WinConsole { type Output = T; fn fg(&mut self, color: color::Color) -> Result<()> { - self.foreground = color; + self.info.foreground = color; self.apply()?; Ok(()) } fn bg(&mut self, color: color::Color) -> Result<()> { - self.background = color; + self.info.background = color; self.apply()?; Ok(()) @@ -230,12 +224,12 @@ impl Terminal for WinConsole { fn attr(&mut self, attr: Attr) -> Result<()> { match attr { Attr::ForegroundColor(f) => { - self.foreground = f; + self.info.foreground = f; self.apply()?; Ok(()) } Attr::BackgroundColor(b) => { - self.background = b; + self.info.background = b; self.apply()?; Ok(()) } @@ -253,8 +247,8 @@ impl Terminal for WinConsole { } fn reset(&mut self) -> Result<()> { - self.foreground = self.def_foreground; - self.background = self.def_background; + self.info.foreground = self.info.def_foreground; + self.info.background = self.info.def_background; self.apply()?; Ok(())