Revert writer logic, just use plain println!

This commit is contained in:
Patrick Haller 2022-09-14 20:08:58 +02:00
parent 3a625599f4
commit 6845a808d2
4 changed files with 33 additions and 32 deletions

View File

@ -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

View File

@ -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)*)
)?
)
}
}

View File

@ -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<ExitCode> {
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<ExitCode> {
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<ExitCode> {
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 {

View File

@ -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<W> {
pub writer: W,
pub struct Printer {
format: SerializationFormat,
}
impl<W: Write> Printer<W> {
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<W: Write> Printer<W> {
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<W: Write> Printer<W> {
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(())
}
}