diff --git a/resources/test/fixtures/E402.py b/resources/test/fixtures/E402.py index ac9a330b73..5181460118 100644 --- a/resources/test/fixtures/E402.py +++ b/resources/test/fixtures/E402.py @@ -8,6 +8,7 @@ except ImportError: else: pass + import c if x > 0: @@ -16,7 +17,6 @@ else: import e y = x + 1 - import f diff --git a/src/logging.rs b/src/logging.rs index af89aa5a17..15d3183d7e 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -3,16 +3,15 @@ use fern; #[macro_export] macro_rules! tell_user { - ($writer:expr,$($arg:tt)*) => { - writeln!( - $writer, + ($($arg:tt)*) => { + println!( "[{}] {}", chrono::Local::now() .format("%H:%M:%S %p") .to_string() .dimmed(), format_args!($($arg)*) - )? + ) } } diff --git a/src/main.rs b/src/main.rs index 131569504b..432a503b57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use std::io::{stdout, BufWriter, Write}; use std::path::PathBuf; use std::process::ExitCode; use std::sync::mpsc::channel; @@ -141,7 +140,8 @@ fn inner_main() -> Result { set_up_logging(cli.verbose)?; let mut settings = Settings::from_paths(&cli.files); - let mut printer = Printer::new(BufWriter::new(stdout()), cli.format); + + let mut printer = Printer::new(cli.format); if !cli.select.is_empty() { settings.select(cli.select); @@ -155,12 +155,16 @@ fn inner_main() -> Result { if cli.watch { if cli.fix { - println!("Warning: --fix is not enabled in watch mode.") + println!("Warning: --fix is not enabled in watch mode."); + } + + if cli.format != SerializationFormat::Text { + println!("Warning: --format 'text' is used in watch mode."); } // Perform an initial run instantly. - clearscreen::clear()?; - tell_user!(printer.writer, "Starting linter in watch mode...\n"); + printer.clear_screen()?; + tell_user!("Starting linter in watch mode...\n"); let messages = run_once(&cli.files, &settings, !cli.no_cache, false)?; if !cli.quiet { @@ -179,8 +183,8 @@ fn inner_main() -> Result { Ok(e) => { if let Some(path) = e.path { if path.to_string_lossy().ends_with(".py") { - clearscreen::clear()?; - tell_user!(printer.writer, "File change detected...\n"); + printer.clear_screen()?; + tell_user!("File change detected...\n"); let messages = run_once(&cli.files, &settings, !cli.no_cache, false)?; if !cli.quiet { diff --git a/src/printer.rs b/src/printer.rs index 6d7a701ffa..625be3747c 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -1,5 +1,4 @@ use colored::Colorize; -use std::io::Write; use anyhow::Result; use clap::ValueEnum; @@ -7,20 +6,19 @@ use clap::ValueEnum; use crate::message::Message; use crate::tell_user; -#[derive(Clone, ValueEnum, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, ValueEnum, PartialEq, Eq, Debug)] pub enum SerializationFormat { Text, Json, } -pub struct Printer { - pub writer: W, +pub struct Printer { format: SerializationFormat, } -impl Printer { - pub fn new(writer: W, format: SerializationFormat) -> Self { - Self { writer, format } +impl Printer { + pub fn new(format: SerializationFormat) -> Self { + Self { format } } pub fn write_once(&mut self, messages: &[Message]) -> Result<()> { @@ -33,29 +31,25 @@ impl Printer { match self.format { SerializationFormat::Json => { - writeln!(self.writer, "{}", serde_json::to_string_pretty(&messages)?)? + println!("{}", serde_json::to_string_pretty(&messages)?) } SerializationFormat::Text => { if !fixed.is_empty() { - writeln!( - self.writer, + println!( "Found {} error(s) ({} fixed).", outstanding.len(), fixed.len() - )? + ) } else { - writeln!(self.writer, "Found {} error(s).", outstanding.len())? + println!("Found {} error(s).", outstanding.len()) } for message in outstanding { - writeln!(self.writer, "{}", message)? + println!("{}", message) } if num_fixable > 0 { - writeln!( - self.writer, - "{num_fixable} potentially fixable with the --fix option." - )? + println!("{num_fixable} potentially fixable with the --fix option.") } } } @@ -65,18 +59,22 @@ impl Printer { pub fn write_continuously(&mut self, messages: &[Message]) -> Result<()> { tell_user!( - self.writer, "Found {} error(s). Watching for file changes.", messages.len(), ); if !messages.is_empty() { - writeln!(self.writer, "\n")?; + println!(); for message in messages { - writeln!(self.writer, "{}", message)? + println!("{}", message) } } Ok(()) } + + pub fn clear_screen(&mut self) -> Result<()> { + clearscreen::clear()?; + Ok(()) + } }