Remove writer parameter from execute_winapi (#570)

This commit is contained in:
Kestrer 2021-06-10 14:20:00 +01:00 committed by GitHub
parent e45008367a
commit e1260446e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 54 additions and 58 deletions

View File

@ -24,7 +24,7 @@ pub trait Command {
/// ///
/// This method does not need to be accessed manually, as it is used by the crossterm's [Command Api](../#command-api) /// This method does not need to be accessed manually, as it is used by the crossterm's [Command Api](../#command-api)
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, writer: impl FnMut() -> Result<()>) -> Result<()>; fn execute_winapi(&self) -> Result<()>;
/// Returns whether the ansi code representation of this command is supported by windows. /// Returns whether the ansi code representation of this command is supported by windows.
/// ///
@ -43,8 +43,8 @@ impl<T: Command + ?Sized> Command for &T {
#[inline] #[inline]
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
T::execute_winapi(self, _writer) T::execute_winapi(self)
} }
#[cfg(windows)] #[cfg(windows)]
@ -122,12 +122,11 @@ impl<T: Write + ?Sized> QueueableCommand for T {
fn queue(&mut self, command: impl Command) -> Result<&mut Self> { fn queue(&mut self, command: impl Command) -> Result<&mut Self> {
#[cfg(windows)] #[cfg(windows)]
if !command.is_ansi_code_supported() { if !command.is_ansi_code_supported() {
command.execute_winapi(|| { // There may be queued commands in this writer, but `execute_winapi` will execute the
write_command_ansi(self, &command)?; // command immediately. To prevent commands being executed out of order we flush the
// winapi doesn't support queuing // writer now.
self.flush()?; self.flush()?;
Ok(()) command.execute_winapi()?;
})?;
return Ok(self); return Ok(self);
} }
@ -224,9 +223,7 @@ fn write_command_ansi<C: Command>(
pub(crate) fn execute_fmt(f: &mut impl fmt::Write, command: impl Command) -> fmt::Result { pub(crate) fn execute_fmt(f: &mut impl fmt::Write, command: impl Command) -> fmt::Result {
#[cfg(windows)] #[cfg(windows)]
if !command.is_ansi_code_supported() { if !command.is_ansi_code_supported() {
return command return command.execute_winapi().map_err(|_| fmt::Error);
.execute_winapi(|| panic!("this writer should not be possible to use here"))
.map_err(|_| fmt::Error);
} }
command.write_ansi(f) command.write_ansi(f)

View File

@ -67,7 +67,7 @@ impl Command for MoveTo {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_to(self.0, self.1) sys::move_to(self.0, self.1)
} }
} }
@ -87,7 +87,7 @@ impl Command for MoveToNextLine {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_to_next_line(self.0) sys::move_to_next_line(self.0)
} }
} }
@ -107,7 +107,7 @@ impl Command for MoveToPreviousLine {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_to_previous_line(self.0) sys::move_to_previous_line(self.0)
} }
} }
@ -126,7 +126,7 @@ impl Command for MoveToColumn {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_to_column(self.0) sys::move_to_column(self.0)
} }
} }
@ -145,7 +145,7 @@ impl Command for MoveToRow {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_to_row(self.0) sys::move_to_row(self.0)
} }
} }
@ -167,7 +167,7 @@ impl Command for MoveUp {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_up(self.0) sys::move_up(self.0)
} }
} }
@ -189,7 +189,7 @@ impl Command for MoveRight {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_right(self.0) sys::move_right(self.0)
} }
} }
@ -211,7 +211,7 @@ impl Command for MoveDown {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_down(self.0) sys::move_down(self.0)
} }
} }
@ -233,7 +233,7 @@ impl Command for MoveLeft {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::move_left(self.0) sys::move_left(self.0)
} }
} }
@ -255,7 +255,7 @@ impl Command for SavePosition {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::save_position() sys::save_position()
} }
} }
@ -277,7 +277,7 @@ impl Command for RestorePosition {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::restore_position() sys::restore_position()
} }
} }
@ -296,7 +296,7 @@ impl Command for Hide {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::show_cursor(false) sys::show_cursor(false)
} }
} }
@ -315,7 +315,7 @@ impl Command for Show {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::show_cursor(true) sys::show_cursor(true)
} }
} }
@ -335,7 +335,7 @@ impl Command for EnableBlinking {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
Ok(()) Ok(())
} }
} }
@ -355,7 +355,7 @@ impl Command for DisableBlinking {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
Ok(()) Ok(())
} }
} }
@ -391,7 +391,7 @@ impl Command for SetCursorShape {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
Ok(()) Ok(())
} }
} }

View File

@ -255,7 +255,7 @@ impl Command for EnableMouseCapture {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::windows::enable_mouse_capture() sys::windows::enable_mouse_capture()
} }
@ -284,7 +284,7 @@ impl Command for DisableMouseCapture {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::windows::disable_mouse_capture() sys::windows::disable_mouse_capture()
} }

View File

@ -106,10 +106,7 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<KeyEvent> {
Some(KeyCode::Char(character)) Some(KeyCode::Char(character))
} }
} else { } else {
match std::char::from_u32(character_raw as u32) { std::char::from_u32(character_raw as u32).map(KeyCode::Char)
Some(ch) => Some(KeyCode::Char(ch)),
None => None,
}
} }
} }
}; };

View File

@ -278,10 +278,7 @@ mod tests {
f.write_str(self.value) f.write_str(self.value)
} }
fn execute_winapi( fn execute_winapi(&self) -> CrosstermResult<()> {
&self,
_writer: impl FnMut() -> CrosstermResult<()>,
) -> CrosstermResult<()> {
self.stream.borrow_mut().push(self.value); self.stream.borrow_mut().push(self.value);
Ok(()) Ok(())
} }

View File

@ -205,7 +205,7 @@ impl Command for SetForegroundColor {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::windows::set_foreground_color(self.0) sys::windows::set_foreground_color(self.0)
} }
} }
@ -229,7 +229,7 @@ impl Command for SetBackgroundColor {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::windows::set_background_color(self.0) sys::windows::set_background_color(self.0)
} }
} }
@ -270,7 +270,7 @@ impl Command for SetColors {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
if let Some(color) = self.0.foreground { if let Some(color) = self.0.foreground {
sys::windows::set_foreground_color(color)?; sys::windows::set_foreground_color(color)?;
} }
@ -297,7 +297,7 @@ impl Command for SetAttribute {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
// attributes are not supported by WinAPI. // attributes are not supported by WinAPI.
Ok(()) Ok(())
} }
@ -324,7 +324,7 @@ impl Command for SetAttributes {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
// attributes are not supported by WinAPI. // attributes are not supported by WinAPI.
Ok(()) Ok(())
} }
@ -346,7 +346,7 @@ impl<D: Display> Command for PrintStyledContent<D> {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
Ok(()) Ok(())
} }
} }
@ -365,7 +365,7 @@ impl Command for ResetColor {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::windows::reset() sys::windows::reset()
} }
} }
@ -382,8 +382,13 @@ impl<T: Display> Command for Print<T> {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, mut writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
writer() panic!("tried to execute Print command using WinAPI, use ANSI instead");
}
#[cfg(windows)]
fn is_ansi_code_supported(&self) -> bool {
true
} }
} }
@ -404,5 +409,5 @@ impl_display!(for ResetColor);
/// Utility function for ANSI parsing in Color and Colored. /// Utility function for ANSI parsing in Color and Colored.
/// Gets the next element of `iter` and tries to parse it as a u8. /// Gets the next element of `iter` and tries to parse it as a u8.
fn parse_next_u8<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Option<u8> { fn parse_next_u8<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Option<u8> {
iter.next().and_then(|s| u8::from_str_radix(s, 10).ok()) iter.next().and_then(|s| s.parse().ok())
} }

View File

@ -127,7 +127,7 @@ impl Command for DisableLineWrap {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
let screen_buffer = ScreenBuffer::current()?; let screen_buffer = ScreenBuffer::current()?;
let console_mode = ConsoleMode::from(screen_buffer.handle().clone()); let console_mode = ConsoleMode::from(screen_buffer.handle().clone());
let new_mode = console_mode.mode()? & !ENABLE_WRAP_AT_EOL_OUTPUT; let new_mode = console_mode.mode()? & !ENABLE_WRAP_AT_EOL_OUTPUT;
@ -146,7 +146,7 @@ impl Command for EnableLineWrap {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
let screen_buffer = ScreenBuffer::current()?; let screen_buffer = ScreenBuffer::current()?;
let console_mode = ConsoleMode::from(screen_buffer.handle().clone()); let console_mode = ConsoleMode::from(screen_buffer.handle().clone());
let new_mode = console_mode.mode()? | ENABLE_WRAP_AT_EOL_OUTPUT; let new_mode = console_mode.mode()? | ENABLE_WRAP_AT_EOL_OUTPUT;
@ -186,7 +186,7 @@ impl Command for EnterAlternateScreen {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
let alternate_screen = ScreenBuffer::create(); let alternate_screen = ScreenBuffer::create();
alternate_screen.show()?; alternate_screen.show()?;
Ok(()) Ok(())
@ -224,7 +224,7 @@ impl Command for LeaveAlternateScreen {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
let screen_buffer = ScreenBuffer::from(Handle::current_out_handle()?); let screen_buffer = ScreenBuffer::from(Handle::current_out_handle()?);
screen_buffer.show()?; screen_buffer.show()?;
Ok(()) Ok(())
@ -264,7 +264,7 @@ impl Command for ScrollUp {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::scroll_up(self.0) sys::scroll_up(self.0)
} }
} }
@ -286,7 +286,7 @@ impl Command for ScrollDown {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::scroll_down(self.0) sys::scroll_down(self.0)
} }
} }
@ -313,7 +313,7 @@ impl Command for Clear {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::clear(self.0) sys::clear(self.0)
} }
} }
@ -332,7 +332,7 @@ impl Command for SetSize {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::set_size(self.0, self.1) sys::set_size(self.0, self.1)
} }
} }
@ -351,7 +351,7 @@ impl<T: fmt::Display> Command for SetTitle<T> {
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
sys::set_window_title(&self.0) sys::set_window_title(&self.0)
} }
} }

View File

@ -371,7 +371,7 @@ mod tests {
let test_title = "this is a crossterm test title"; let test_title = "this is a crossterm test title";
set_window_title(test_title).unwrap(); set_window_title(test_title).unwrap();
let mut raw = [0 as u16; 128]; let mut raw = [0_u16; 128];
let length = unsafe { GetConsoleTitleW(raw.as_mut_ptr(), raw.len() as u32) } as usize; let length = unsafe { GetConsoleTitleW(raw.as_mut_ptr(), raw.len() as u32) } as usize;
assert_ne!(0, length); assert_ne!(0, length);