From 079744189345f0c00b5fca249f968f0b533ecdac Mon Sep 17 00:00:00 2001 From: Koxiaet Date: Mon, 28 Dec 2020 08:47:20 +0000 Subject: [PATCH] Make imports & docs more consistent (#524) --- src/command.rs | 6 ++++-- src/cursor.rs | 5 +++-- src/cursor/sys/windows.rs | 4 ++-- src/event.rs | 7 ++++--- src/event/read.rs | 4 ++-- src/event/source/unix.rs | 3 ++- src/event/stream.rs | 6 ++---- src/event/sys/unix/parse.rs | 3 ++- src/event/sys/windows.rs | 1 - src/lib.rs | 2 +- src/macros.rs | 8 ++++---- src/style.rs | 1 + src/style/attributes.rs | 3 ++- src/style/sys/windows.rs | 6 +++--- src/style/types/colored.rs | 4 ++-- src/terminal/sys/unix.rs | 15 ++++++++------- src/terminal/sys/windows.rs | 5 ++--- src/tty.rs | 1 + 18 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/command.rs b/src/command.rs index f24532b..3e61ac8 100644 --- a/src/command.rs +++ b/src/command.rs @@ -85,6 +85,7 @@ impl QueueableCommand for T { /// /// ```rust /// use std::io::{Write, stdout}; + /// /// use crossterm::{Result, QueueableCommand, style::Print}; /// /// fn main() -> Result<()> { @@ -113,7 +114,7 @@ impl QueueableCommand for T { /// # Notes /// /// * In the case of UNIX and Windows 10, ANSI codes are written to the given 'writer'. - /// * In case of Windows versions lower than 10, a direct WinApi call will be made. + /// * In case of Windows versions lower than 10, a direct WinAPI call will be made. /// The reason for this is that Windows versions lower than 10 do not support ANSI codes, /// and can therefore not be written to the given `writer`. /// Therefore, there is no difference between [execute](./trait.ExecutableCommand.html) @@ -150,6 +151,7 @@ impl ExecutableCommand for T { /// /// ```rust /// use std::io::{Write, stdout}; + /// /// use crossterm::{Result, ExecutableCommand, style::Print}; /// /// fn main() -> Result<()> { @@ -171,7 +173,7 @@ impl ExecutableCommand for T { /// # Notes /// /// * In the case of UNIX and Windows 10, ANSI codes are written to the given 'writer'. - /// * In case of Windows versions lower than 10, a direct WinApi call will be made. + /// * In case of Windows versions lower than 10, a direct WinAPI call will be made. /// The reason for this is that Windows versions lower than 10 do not support ANSI codes, /// and can therefore not be written to the given `writer`. /// Therefore, there is no difference between [execute](./trait.ExecutableCommand.html) diff --git a/src/cursor.rs b/src/cursor.rs index ee8d1b2..c212498 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -42,12 +42,13 @@ //! //! For manual execution control check out [crossterm::queue](../macro.queue.html). -pub use sys::position; +use std::fmt; #[cfg(windows)] use crate::Result; use crate::{impl_display, Command}; -use std::fmt; + +pub use sys::position; mod ansi; pub(crate) mod sys; diff --git a/src/cursor/sys/windows.rs b/src/cursor/sys/windows.rs index 8c49476..f41193a 100644 --- a/src/cursor/sys/windows.rs +++ b/src/cursor/sys/windows.rs @@ -1,4 +1,4 @@ -//! WinApi related logic to cursor manipulation. +//! WinAPI related logic to cursor manipulation. use std::{io, sync::Mutex}; @@ -106,7 +106,7 @@ pub(crate) fn restore_position() -> Result<()> { Ok(()) } -/// WinApi wrapper over terminal cursor behaviour. +/// WinAPI wrapper over terminal cursor behaviour. struct ScreenBufferCursor { screen_buffer: ScreenBuffer, } diff --git a/src/event.rs b/src/event.rs index c0705ff..3c0e19e 100644 --- a/src/event.rs +++ b/src/event.rs @@ -78,14 +78,15 @@ use parking_lot::RwLock; use serde::{Deserialize, Serialize}; use bitflags::bitflags; -use filter::{EventFilter, Filter}; use lazy_static::lazy_static; + +use crate::{Command, Result}; + +use filter::{EventFilter, Filter}; #[cfg(feature = "event-stream")] pub use stream::EventStream; use timeout::PollTimeout; -use crate::{Command, Result}; - mod ansi; pub(crate) mod filter; mod read; diff --git a/src/event/read.rs b/src/event/read.rs index 9ce5d83..a24170a 100644 --- a/src/event/read.rs +++ b/src/event/read.rs @@ -1,7 +1,5 @@ use std::{collections::vec_deque::VecDeque, io, time::Duration}; -use crate::ErrorKind; - #[cfg(unix)] use super::source::unix::UnixInternalEventSource; #[cfg(windows)] @@ -10,6 +8,8 @@ use super::source::windows::WindowsEventSource; use super::sys::Waker; use super::{filter::Filter, source::EventSource, timeout::PollTimeout, InternalEvent, Result}; +use crate::ErrorKind; + /// Can be used to read `InternalEvent`s. pub(crate) struct InternalEventReader { events: VecDeque, diff --git a/src/event/source/unix.rs b/src/event/source/unix.rs index bd6755c..fd33ffc 100644 --- a/src/event/source/unix.rs +++ b/src/event/source/unix.rs @@ -1,6 +1,7 @@ +use std::{collections::VecDeque, io, time::Duration}; + use mio::{unix::SourceFd, Events, Interest, Poll, Token}; use signal_hook::iterator::Signals; -use std::{collections::VecDeque, io, time::Duration}; use crate::{ErrorKind, Result}; diff --git a/src/event/stream.rs b/src/event/stream.rs index 6e51c6e..a7f61b5 100644 --- a/src/event/stream.rs +++ b/src/event/stream.rs @@ -5,14 +5,12 @@ use std::{ mpsc::{self, SyncSender}, Arc, }, + task::{Context, Poll}, thread, time::Duration, }; -use futures_core::{ - stream::Stream, - task::{Context, Poll}, -}; +use futures_core::stream::Stream; use crate::Result; diff --git a/src/event/sys/unix/parse.rs b/src/event/sys/unix/parse.rs index aba8ef5..90ec427 100644 --- a/src/event/sys/unix/parse.rs +++ b/src/event/sys/unix/parse.rs @@ -1,10 +1,11 @@ +use std::io; + use crate::{ event::{Event, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}, ErrorKind, Result, }; use super::super::super::InternalEvent; -use std::io; // Event parsing // diff --git a/src/event/sys/windows.rs b/src/event/sys/windows.rs index d0fa1dd..d872a6c 100644 --- a/src/event/sys/windows.rs +++ b/src/event/sys/windows.rs @@ -3,7 +3,6 @@ use std::sync::Mutex; use crossterm_winapi::{ConsoleMode, Handle}; - use lazy_static::lazy_static; use crate::Result; diff --git a/src/lib.rs b/src/lib.rs index b6195ec..e69a2c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ //! //! Linux and Windows 10 systems support ANSI escape codes. Those ANSI escape codes are strings or rather a //! byte sequence. When we `write` and `flush` those to the terminal we can perform some action. -//! For older windows systems a WinApi call is made. +//! For older windows systems a WinAPI call is made. //! //! ### Supported Commands //! diff --git a/src/macros.rs b/src/macros.rs index 26c7387..893e906 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -50,7 +50,7 @@ macro_rules! csi { /// /// # Notes /// -/// In case of Windows versions lower than 10, a direct WinApi call will be made. +/// In case of Windows versions lower than 10, a direct WinAPI call will be made. /// The reason for this is that Windows versions lower than 10 do not support ANSI codes, /// and can therefore not be written to the given `writer`. /// Therefore, there is no difference between [execute](macro.execute.html) @@ -104,7 +104,7 @@ macro_rules! queue { /// # Notes /// /// * In the case of UNIX and Windows 10, ANSI codes are written to the given 'writer'. -/// * In case of Windows versions lower than 10, a direct WinApi call will be made. +/// * In case of Windows versions lower than 10, a direct WinAPI call will be made. /// The reason for this is that Windows versions lower than 10 do not support ANSI codes, /// and can therefore not be written to the given `writer`. /// Therefore, there is no difference between [execute](macro.execute.html) @@ -248,7 +248,7 @@ mod tests { use crate::command::Command; use crate::error::Result as CrosstermResult; - // We need to test two different APIs: winapi and the write api. We + // We need to test two different APIs: WinAPI and the write api. We // don't know until runtime which we're supporting (via // Command::is_ansi_code_supported), so we have to test them both. The // CI environment hopefully includes both versions of windows. @@ -287,7 +287,7 @@ mod tests { } } - // Helper function for running tests against either winapi or an + // Helper function for running tests against either WinAPI or an // io::Write. // // This function will execute the `test` function, which should diff --git a/src/style.rs b/src/style.rs index 9dc5788..aba0b26 100644 --- a/src/style.rs +++ b/src/style.rs @@ -243,6 +243,7 @@ impl Command for SetBackgroundColor { /// For example: /// ```no_run /// use std::io::{stdout, Write}; +/// /// use crossterm::execute; /// use crossterm::style::{Color::{Green, Black}, Colors, Print, SetColors}; /// diff --git a/src/style/attributes.rs b/src/style/attributes.rs index b09ab02..958ff2f 100644 --- a/src/style/attributes.rs +++ b/src/style/attributes.rs @@ -1,6 +1,7 @@ -use crate::style::Attribute; use std::ops::{BitAnd, BitOr, BitXor}; +use crate::style::Attribute; + /// a bitset for all possible attributes #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct Attributes(u32); diff --git a/src/style/sys/windows.rs b/src/style/sys/windows.rs index 5cb9ff0..80c9ede 100644 --- a/src/style/sys/windows.rs +++ b/src/style/sys/windows.rs @@ -105,7 +105,7 @@ lazy_static! { } impl From for u16 { - /// Returns the WinApi color value (u16) from the `Colored` struct. + /// Returns the WinAPI color value (u16) from the `Colored` struct. fn from(colored: Colored) -> Self { match colored { Colored::ForegroundColor(color) => { @@ -137,7 +137,7 @@ impl From for u16 { original_color & !REMOVE_BG_MASK } - /* WinApi will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/ + /* WinAPI will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/ Color::Rgb { .. } => 0, Color::AnsiValue(_val) => 0, } @@ -169,7 +169,7 @@ impl From for u16 { original_color & !REMOVE_FG_MASK } - /* WinApi will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/ + /* WinAPI will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/ Color::Rgb { .. } => 0, Color::AnsiValue(_val) => 0, } diff --git a/src/style/types/colored.rs b/src/style/types/colored.rs index 1ee8a2d..d4e7aeb 100644 --- a/src/style/types/colored.rs +++ b/src/style/types/colored.rs @@ -1,8 +1,8 @@ -use crate::style::Color; - #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +use crate::style::Color; + /// Represents a foreground or background color. /// /// This can be converted to a [Colors](struct.Colors.html) by calling `into()` and applied diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index 2274c1e..4da3a88 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -1,7 +1,9 @@ //! UNIX related logic for terminal manipulation. + +use std::fs::File; +use std::os::unix::io::{IntoRawFd, RawFd}; use std::{io, mem, process, sync::Mutex}; -use crate::event::sys::unix::file_descriptor::{tty_fd, FileDesc}; use lazy_static::lazy_static; use libc::{ cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDOUT_FILENO, TCSANOW, @@ -9,8 +11,7 @@ use libc::{ }; use crate::error::{ErrorKind, Result}; -use std::fs::File; -use std::os::unix::io::{IntoRawFd, RawFd}; +use crate::event::sys::unix::file_descriptor::{tty_fd, FileDesc}; lazy_static! { // Some(Termios) -> we're in the raw mode and this is the previous mode @@ -40,7 +41,7 @@ pub(crate) fn size() -> Result<(u16, u16)> { STDOUT_FILENO }; - if let Ok(true) = wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }) { + if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() { Ok((size.ws_col, size.ws_row)) } else { tput_size().ok_or_else(|| std::io::Error::last_os_error().into()) @@ -127,14 +128,14 @@ fn get_terminal_attr(fd: RawFd) -> Result { } } -fn set_terminal_attr(fd: RawFd, termios: &Termios) -> Result { +fn set_terminal_attr(fd: RawFd, termios: &Termios) -> Result<()> { wrap_with_result(unsafe { tcsetattr(fd, TCSANOW, termios) }) } -pub fn wrap_with_result(result: i32) -> Result { +fn wrap_with_result(result: i32) -> Result<()> { if result == -1 { Err(ErrorKind::IoError(io::Error::last_os_error())) } else { - Ok(true) + Ok(()) } } diff --git a/src/terminal/sys/windows.rs b/src/terminal/sys/windows.rs index 277b63e..a5b04a6 100644 --- a/src/terminal/sys/windows.rs +++ b/src/terminal/sys/windows.rs @@ -1,4 +1,4 @@ -//! WinApi related logic for terminal manipulation. +//! WinAPI related logic for terminal manipulation. use crossterm_winapi::{Console, ConsoleMode, Coord, Handle, ScreenBuffer, Size}; use winapi::{ shared::minwindef::DWORD, @@ -295,9 +295,8 @@ fn clear_winapi(start_location: Coord, cells_to_write: u32, current_attribute: u mod tests { use std::{ffi::OsString, os::windows::ffi::OsStringExt}; - use winapi::um::wincon::GetConsoleTitleW; - use crossterm_winapi::ScreenBuffer; + use winapi::um::wincon::GetConsoleTitleW; use super::{scroll_down, scroll_up, set_size, set_window_title, size}; diff --git a/src/tty.rs b/src/tty.rs index 6972365..8828eb2 100644 --- a/src/tty.rs +++ b/src/tty.rs @@ -7,6 +7,7 @@ use std::os::unix::io::AsRawFd; #[cfg(windows)] use std::os::windows::io::AsRawHandle; + #[cfg(windows)] use winapi::um::consoleapi::GetConsoleMode;