Skip to content

Commit

Permalink
Warnings cleanup (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka authored and TimonPost committed Sep 5, 2019
1 parent ec44719 commit 6834669
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 72 deletions.
2 changes: 1 addition & 1 deletion crossterm_input/src/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl TerminalInput {
pub fn read_line(&self) -> io::Result<String> {
let mut rv = String::new();
io::stdin().read_line(&mut rv)?;
let len = rv.trim_right_matches(&['\r', '\n'][..]).len();
let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
rv.truncate(len);
Ok(rv)
}
Expand Down
20 changes: 10 additions & 10 deletions crossterm_input/src/input/unix_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub struct AsyncReader {
impl AsyncReader {
/// Construct a new instance of the `AsyncReader`.
/// The reading will immediately start when calling this function.
pub fn new(function: Box<Fn(&Sender<u8>, &Arc<AtomicBool>) + Send>) -> AsyncReader {
pub fn new(function: Box<dyn Fn(&Sender<u8>, &Arc<AtomicBool>) + Send>) -> AsyncReader {
let shutdown_handle = Arc::new(AtomicBool::new(false));

let (event_tx, event_rx) = mpsc::channel();
Expand Down Expand Up @@ -242,7 +242,7 @@ where
Some(b'O') => {
match iter.next() {
// F1-F4
Some(val @ b'P'...b'S') => {
Some(val @ b'P'..=b'S') => {
InputEvent::Keyboard(KeyEvent::F(1 + val - b'P'))
}
_ => return Err(error),
Expand All @@ -263,10 +263,10 @@ where
b'\n' | b'\r' => InputEvent::Keyboard(KeyEvent::Char('\n')),
b'\t' => InputEvent::Keyboard(KeyEvent::Char('\t')),
b'\x7F' => InputEvent::Keyboard(KeyEvent::Backspace),
c @ b'\x01'...b'\x1A' => {
c @ b'\x01'..=b'\x1A' => {
InputEvent::Keyboard(KeyEvent::Ctrl((c as u8 - 0x1 + b'a') as char))
}
c @ b'\x1C'...b'\x1F' => {
c @ b'\x1C'..=b'\x1F' => {
InputEvent::Keyboard(KeyEvent::Ctrl((c as u8 - 0x1C + b'4') as char))
}
b'\0' => InputEvent::Keyboard(KeyEvent::Null),
Expand All @@ -290,7 +290,7 @@ where
Some(b'[') => match iter.next() {
// NOTE (@imdaveho): cannot find when this occurs;
// having another '[' after ESC[ not a likely scenario
Some(val @ b'A'...b'E') => InputEvent::Keyboard(KeyEvent::F(1 + val - b'A')),
Some(val @ b'A'..=b'E') => InputEvent::Keyboard(KeyEvent::F(1 + val - b'A')),
_ => InputEvent::Unknown,
},
Some(b'D') => InputEvent::Keyboard(KeyEvent::Left),
Expand Down Expand Up @@ -350,7 +350,7 @@ where
let cy = nums.next().unwrap().parse::<u16>().unwrap();

match cb {
0...2 | 64...65 => {
0..=2 | 64..=65 => {
let button = match cb {
0 => MouseButton::Left,
1 => MouseButton::Middle,
Expand All @@ -370,7 +370,7 @@ where
_ => InputEvent::Unknown,
}
}
Some(c @ b'0'...b'9') => {
Some(c @ b'0'..=b'9') => {
// Numbered escape code.
let mut buf = Vec::new();
buf.push(c);
Expand Down Expand Up @@ -430,9 +430,9 @@ where
4 | 8 => InputEvent::Keyboard(KeyEvent::End),
5 => InputEvent::Keyboard(KeyEvent::PageUp),
6 => InputEvent::Keyboard(KeyEvent::PageDown),
v @ 11...15 => InputEvent::Keyboard(KeyEvent::F(v - 10)),
v @ 17...21 => InputEvent::Keyboard(KeyEvent::F(v - 11)),
v @ 23...24 => InputEvent::Keyboard(KeyEvent::F(v - 12)),
v @ 11..=15 => InputEvent::Keyboard(KeyEvent::F(v - 10)),
v @ 17..=21 => InputEvent::Keyboard(KeyEvent::F(v - 11)),
v @ 23..=24 => InputEvent::Keyboard(KeyEvent::F(v - 12)),
_ => InputEvent::Unknown,
}
}
Expand Down
12 changes: 7 additions & 5 deletions crossterm_utils/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{execute, impl_display, queue, write_cout, Result};
#[cfg(windows)]
use crate::Result;
use crate::{execute, impl_display, queue, write_cout};
use std::fmt::Display;
use std::io::Write;

Expand Down Expand Up @@ -31,13 +33,13 @@ pub trait Command {
/// This can be used in order to get more performance.
pub trait QueueableCommand<T: Display> {
/// Queues the given command for later execution.
fn queue(mut self, command: impl Command<AnsiType = T>) -> Self;
fn queue(self, command: impl Command<AnsiType = T>) -> Self;
}

/// A trait that defines behaviour for a command that will be executed immediately.
pub trait ExecutableCommand<T: Display> {
/// Execute the given command directly.
fn execute(mut self, command: impl Command<AnsiType = T>) -> Self;
fn execute(self, command: impl Command<AnsiType = T>) -> Self;
}

impl<T, A> QueueableCommand<A> for T
Expand Down Expand Up @@ -66,7 +68,7 @@ where
/// Because of that there is no difference between `execute` and `queue` for those windows versions.
/// - Queuing might sound that there is some scheduling going on, however, this means that we write to the stdout without flushing which will cause commands to be stored in the buffer without them being written to the terminal.
fn queue(mut self, command: impl Command<AnsiType = A>) -> Self {
queue!(self, command);
let _ = queue!(self, command);
self
}
}
Expand All @@ -90,7 +92,7 @@ where
/// This is happening because Windows versions lower then 10 do not support ANSI codes, and thus they can't be written to the given buffer.
/// Because of that there is no difference between `execute` and `queue` for those windows versions.
fn execute(mut self, command: impl Command<AnsiType = A>) -> Self {
execute!(self, command);
let _ = execute!(self, command);
self
}
}
Expand Down
83 changes: 42 additions & 41 deletions crossterm_utils/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Append a the first few characters of an ANSI escape code to the given string.

#[macro_export]
macro_rules! csi {
($( $l:expr ),*) => { concat!("\x1B[", $( $l ),*) };
Expand Down Expand Up @@ -66,42 +65,43 @@ macro_rules! write_cout {
/// - Queuing might sound that there is some scheduling going on, however, this means that we write to the stdout without flushing which will cause commands to be stored in the buffer without them being written to the terminal.
#[macro_export]
macro_rules! queue {
($write:expr, $($command:expr), *) =>
{{
($write:expr, $($command:expr), *) => {{
// Silent warning when the macro is used inside the `command` module
#[allow(unused_imports)]
use $crate::Command;
let mut error = None;

$(
#[cfg(windows)]
{
{
if $crate::supports_ansi() {
match write!($write, "{}",$command.get_ansi_code()) {
match write!($write, "{}", $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
} else {
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
}
#[cfg(unix)]
match write!($write, "{}",$command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
#[cfg(unix)]
match write!($write, "{}", $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
)*

if let Some(error) = error {
error
}else {
} else {
Ok(())
}
}}
Expand Down Expand Up @@ -137,42 +137,43 @@ macro_rules! queue {
/// Because of that there is no difference between `execute` and `queue` for those windows versions.
#[macro_export]
macro_rules! execute {
($write:expr, $($command:expr), *) =>
{{
use $crate::{Command, write_cout};
($write:expr, $($command:expr), *) => {{
// Silent warning when the macro is used inside the `command` module
#[allow(unused_imports)]
use $crate::{Command, write_cout};
let mut error = None;

$(
#[cfg(windows)]
{
{
if $crate::supports_ansi() {
match write_cout!($write, $command.get_ansi_code()) {
match write_cout!($write, $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
} else {
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
}
#[cfg(unix)]
match write_cout!($write, $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
#[cfg(unix)]
match write_cout!($write, $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
)*

if let Some(error) = error {
error
}else {
} else {
Ok(())
}
}}
Expand Down
24 changes: 12 additions & 12 deletions examples/command.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#![allow(dead_code)]

extern crate crossterm;

use crossterm::{
execute, queue, Clear, ClearType, Color, Colorize, ExecutableCommand, Goto, Output,
PrintStyledFont, QueueableCommand,
execute, queue, Clear, ClearType, ExecutableCommand, Goto, Output, QueueableCommand,
};
use std::fmt::Display;
use std::io::{stdout, Stdout, Write};
use std::io::{stdout, Write};

/// execute commands by using normal functions
fn execute_command_directly_using_functions() {
// single command
stdout().execute(Output("Text1 ".to_string()));
let _ = stdout().execute(Output("Text1 ".to_string()));

// multiple commands
stdout()
let _ = stdout()
.execute(Output("Text2 ".to_string()))
.execute(Output("Text3 ".to_string()));
}

/// execute commands by using macro's
fn execute_command_directly_using_macros() {
// single command
execute!(stdout(), Output("Text1 ".to_string()));
let _ = execute!(stdout(), Output("Text1 ".to_string()));

// multiple commands
execute!(
let _ = execute!(
stdout(),
Output("Text2 ".to_string()),
Output("Text 3".to_string())
Expand All @@ -49,18 +49,18 @@ fn later_execution_command_using_functions() {
::std::thread::sleep(std::time::Duration::from_millis(2000));

// when you call this all commands will be executed
sdout.flush();
let _ = sdout.flush();
}

/// queue commands without executing them directly by using macro's
fn later_execution_command_directly_using_macros() {
let mut stdout = stdout();

// single command
queue!(stdout, Output("Text1 ".to_string()));
let _ = queue!(stdout, Output("Text1 ".to_string()));

// multiple commands
queue!(
let _ = queue!(
stdout,
Clear(ClearType::All),
Goto(5, 5),
Expand All @@ -70,7 +70,7 @@ fn later_execution_command_directly_using_macros() {
::std::thread::sleep(std::time::Duration::from_millis(2000));

// when you call this all commands will be executed
stdout.flush();
let _ = stdout.flush();
}

fn main() {}
3 changes: 3 additions & 0 deletions examples/crossterm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Remove once the TODO below is fixed
#![allow(unused_variables)]

extern crate crossterm;

use crossterm::{Color, Crossterm};
Expand Down
2 changes: 0 additions & 2 deletions examples/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
extern crate crossterm;

use self::crossterm::{color, Attribute, Color, Colored, Colorize, Styler};
use std::io::stdout;

/// print some red text | demonstration.
pub fn paint_foreground() {
Expand Down Expand Up @@ -412,6 +411,5 @@ pub fn reset_fg_and_bg() {
}

fn main() {
use std::io::Write;
print_all_background_colors_with_method()
}
2 changes: 1 addition & 1 deletion examples/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,5 @@ pub fn exit() {
}

fn main() {
scroll_down();
let _ = scroll_down();
}

0 comments on commit 6834669

Please sign in to comment.