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)
#[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.
///
@ -43,8 +43,8 @@ impl<T: Command + ?Sized> Command for &T {
#[inline]
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
T::execute_winapi(self, _writer)
fn execute_winapi(&self) -> Result<()> {
T::execute_winapi(self)
}
#[cfg(windows)]
@ -122,12 +122,11 @@ impl<T: Write + ?Sized> QueueableCommand for T {
fn queue(&mut self, command: impl Command) -> Result<&mut Self> {
#[cfg(windows)]
if !command.is_ansi_code_supported() {
command.execute_winapi(|| {
write_command_ansi(self, &command)?;
// winapi doesn't support queuing
self.flush()?;
Ok(())
})?;
// There may be queued commands in this writer, but `execute_winapi` will execute the
// command immediately. To prevent commands being executed out of order we flush the
// writer now.
self.flush()?;
command.execute_winapi()?;
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 {
#[cfg(windows)]
if !command.is_ansi_code_supported() {
return command
.execute_winapi(|| panic!("this writer should not be possible to use here"))
.map_err(|_| fmt::Error);
return command.execute_winapi().map_err(|_| fmt::Error);
}
command.write_ansi(f)

View File

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

View File

@ -255,7 +255,7 @@ impl Command for EnableMouseCapture {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::windows::enable_mouse_capture()
}
@ -284,7 +284,7 @@ impl Command for DisableMouseCapture {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
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))
}
} else {
match std::char::from_u32(character_raw as u32) {
Some(ch) => Some(KeyCode::Char(ch)),
None => None,
}
std::char::from_u32(character_raw as u32).map(KeyCode::Char)
}
}
};

View File

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

View File

@ -205,7 +205,7 @@ impl Command for SetForegroundColor {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::windows::set_foreground_color(self.0)
}
}
@ -229,7 +229,7 @@ impl Command for SetBackgroundColor {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::windows::set_background_color(self.0)
}
}
@ -270,7 +270,7 @@ impl Command for SetColors {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
if let Some(color) = self.0.foreground {
sys::windows::set_foreground_color(color)?;
}
@ -297,7 +297,7 @@ impl Command for SetAttribute {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
// attributes are not supported by WinAPI.
Ok(())
}
@ -324,7 +324,7 @@ impl Command for SetAttributes {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
// attributes are not supported by WinAPI.
Ok(())
}
@ -346,7 +346,7 @@ impl<D: Display> Command for PrintStyledContent<D> {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
Ok(())
}
}
@ -365,7 +365,7 @@ impl Command for ResetColor {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::windows::reset()
}
}
@ -382,8 +382,13 @@ impl<T: Display> Command for Print<T> {
}
#[cfg(windows)]
fn execute_winapi(&self, mut writer: impl FnMut() -> Result<()>) -> Result<()> {
writer()
fn execute_winapi(&self) -> Result<()> {
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.
/// 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> {
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)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
let screen_buffer = ScreenBuffer::current()?;
let console_mode = ConsoleMode::from(screen_buffer.handle().clone());
let new_mode = console_mode.mode()? & !ENABLE_WRAP_AT_EOL_OUTPUT;
@ -146,7 +146,7 @@ impl Command for EnableLineWrap {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
let screen_buffer = ScreenBuffer::current()?;
let console_mode = ConsoleMode::from(screen_buffer.handle().clone());
let new_mode = console_mode.mode()? | ENABLE_WRAP_AT_EOL_OUTPUT;
@ -186,7 +186,7 @@ impl Command for EnterAlternateScreen {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
let alternate_screen = ScreenBuffer::create();
alternate_screen.show()?;
Ok(())
@ -224,7 +224,7 @@ impl Command for LeaveAlternateScreen {
}
#[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()?);
screen_buffer.show()?;
Ok(())
@ -264,7 +264,7 @@ impl Command for ScrollUp {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::scroll_up(self.0)
}
}
@ -286,7 +286,7 @@ impl Command for ScrollDown {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::scroll_down(self.0)
}
}
@ -313,7 +313,7 @@ impl Command for Clear {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::clear(self.0)
}
}
@ -332,7 +332,7 @@ impl Command for SetSize {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::set_size(self.0, self.1)
}
}
@ -351,7 +351,7 @@ impl<T: fmt::Display> Command for SetTitle<T> {
}
#[cfg(windows)]
fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
fn execute_winapi(&self) -> Result<()> {
sys::set_window_title(&self.0)
}
}

View File

@ -371,7 +371,7 @@ mod tests {
let test_title = "this is a crossterm test title";
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;
assert_ne!(0, length);