mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
feat: --check no longer modifies files in-place
- Also took the time to improve the messages so that it is more clear what is being done
This commit is contained in:
parent
5cbb3486c7
commit
9935b00892
2 changed files with 68 additions and 34 deletions
|
@ -109,21 +109,30 @@ pub(crate) fn stdin(quiet: bool) -> FormattedPath {
|
||||||
FormattedPath { path, status }
|
FormattedPath { path, status }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn simple(paths: Vec<String>, quiet: bool) -> Vec<FormattedPath> {
|
pub(crate) fn simple(
|
||||||
|
paths: Vec<String>,
|
||||||
|
in_place: bool,
|
||||||
|
quiet: bool,
|
||||||
|
) -> Vec<FormattedPath> {
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
if !quiet {
|
if !quiet {
|
||||||
eprintln!("Formatting: {} files", paths.len());
|
eprintln!("Processing: {} files", paths.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
paths
|
paths
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|path| {
|
.map(|path| {
|
||||||
let status = alejandra_engine::format::in_place(path.clone());
|
let status =
|
||||||
|
alejandra_engine::format::in_fs(path.clone(), in_place);
|
||||||
|
|
||||||
if let alejandra_engine::format::Status::Changed(changed) = status {
|
if let alejandra_engine::format::Status::Changed(changed) = status {
|
||||||
if changed && !quiet {
|
if changed && !quiet {
|
||||||
eprintln!("Changed: {}", &path);
|
if in_place {
|
||||||
|
eprintln!("Changed: {}", &path);
|
||||||
|
} else {
|
||||||
|
eprintln!("Would be changed: {}", &path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +141,10 @@ pub(crate) fn simple(paths: Vec<String>, quiet: bool) -> Vec<FormattedPath> {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn tui(paths: Vec<String>) -> std::io::Result<Vec<FormattedPath>> {
|
pub(crate) fn tui(
|
||||||
|
paths: Vec<String>,
|
||||||
|
in_place: bool,
|
||||||
|
) -> std::io::Result<Vec<FormattedPath>> {
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use termion::input::TermRead;
|
use termion::input::TermRead;
|
||||||
use termion::raw::IntoRawMode;
|
use termion::raw::IntoRawMode;
|
||||||
|
@ -183,7 +195,8 @@ pub(crate) fn tui(paths: Vec<String>) -> std::io::Result<Vec<FormattedPath>> {
|
||||||
let sender_finished = sender;
|
let sender_finished = sender;
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
paths.into_par_iter().for_each_with(sender_paths, |sender, path| {
|
paths.into_par_iter().for_each_with(sender_paths, |sender, path| {
|
||||||
let status = alejandra_engine::format::in_place(path.clone());
|
let status =
|
||||||
|
alejandra_engine::format::in_fs(path.clone(), in_place);
|
||||||
|
|
||||||
if let Err(error) = sender
|
if let Err(error) = sender
|
||||||
.send(Event::FormattedPath(FormattedPath { path, status }))
|
.send(Event::FormattedPath(FormattedPath { path, status }))
|
||||||
|
@ -279,9 +292,15 @@ pub(crate) fn tui(paths: Vec<String>) -> std::io::Result<Vec<FormattedPath>> {
|
||||||
tui::widgets::Block::default()
|
tui::widgets::Block::default()
|
||||||
.borders(tui::widgets::Borders::ALL)
|
.borders(tui::widgets::Borders::ALL)
|
||||||
.title(format!(
|
.title(format!(
|
||||||
" Formatting ({} changed, {} unchanged, {} \
|
" Formatting ({} {}, {} unchanged, {} errors) ",
|
||||||
errors) ",
|
paths_changed,
|
||||||
paths_changed, paths_unchanged, paths_with_errors
|
if in_place {
|
||||||
|
"changed"
|
||||||
|
} else {
|
||||||
|
"would be changed"
|
||||||
|
},
|
||||||
|
paths_unchanged,
|
||||||
|
paths_with_errors
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
.gauge_style(
|
.gauge_style(
|
||||||
|
@ -313,16 +332,20 @@ pub(crate) fn tui(paths: Vec<String>) -> std::io::Result<Vec<FormattedPath>> {
|
||||||
changed,
|
changed,
|
||||||
) => tui::text::Span::styled(
|
) => tui::text::Span::styled(
|
||||||
if changed {
|
if changed {
|
||||||
"CHANGED "
|
if in_place {
|
||||||
|
"CHANGED "
|
||||||
|
} else {
|
||||||
|
"WOULD BE CHANGED "
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
"OK "
|
"OK "
|
||||||
},
|
},
|
||||||
tui::style::Style::default()
|
tui::style::Style::default()
|
||||||
.fg(tui::style::Color::Green),
|
.fg(tui::style::Color::Green),
|
||||||
),
|
),
|
||||||
alejandra_engine::format::Status::Error(_) => {
|
alejandra_engine::format::Status::Error(_) => {
|
||||||
tui::text::Span::styled(
|
tui::text::Span::styled(
|
||||||
"ERROR ",
|
"ERROR ",
|
||||||
tui::style::Style::default()
|
tui::style::Style::default()
|
||||||
.fg(tui::style::Color::Red),
|
.fg(tui::style::Color::Red),
|
||||||
)
|
)
|
||||||
|
@ -352,7 +375,7 @@ pub(crate) fn tui(paths: Vec<String>) -> std::io::Result<Vec<FormattedPath>> {
|
||||||
pub fn main() -> std::io::Result<()> {
|
pub fn main() -> std::io::Result<()> {
|
||||||
let matches = crate::cli::parse(std::env::args().collect());
|
let matches = crate::cli::parse(std::env::args().collect());
|
||||||
|
|
||||||
let check = matches.is_present("check");
|
let in_place = !matches.is_present("check");
|
||||||
let threads = matches.value_of("threads").unwrap();
|
let threads = matches.value_of("threads").unwrap();
|
||||||
let threads: usize = threads.parse().unwrap();
|
let threads: usize = threads.parse().unwrap();
|
||||||
let quiet = matches.is_present("quiet");
|
let quiet = matches.is_present("quiet");
|
||||||
|
@ -377,9 +400,9 @@ pub fn main() -> std::io::Result<()> {
|
||||||
&& atty::is(atty::Stream::Stdin)
|
&& atty::is(atty::Stream::Stdin)
|
||||||
&& atty::is(atty::Stream::Stdout)
|
&& atty::is(atty::Stream::Stdout)
|
||||||
{
|
{
|
||||||
crate::cli::tui(paths)?
|
crate::cli::tui(paths, in_place)?
|
||||||
} else {
|
} else {
|
||||||
crate::cli::simple(paths, quiet)
|
crate::cli::simple(paths, in_place, quiet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => vec![crate::cli::stdin(quiet)],
|
None => vec![crate::cli::stdin(quiet)],
|
||||||
|
@ -424,22 +447,24 @@ pub fn main() -> std::io::Result<()> {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
eprintln!();
|
eprintln!();
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Success! {} file{} {} changed",
|
"Success! {} file{} {}",
|
||||||
changed,
|
changed,
|
||||||
if changed >= 2 { "s" } else { "" },
|
if changed >= 2 { "s" } else { "" },
|
||||||
if changed >= 2 { "were" } else { "was" },
|
if in_place {
|
||||||
|
if changed >= 2 { "were changed" } else { "was changed" }
|
||||||
|
} else {
|
||||||
|
"would be changed"
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if check {
|
|
||||||
std::process::exit(2);
|
std::process::exit(if in_place { 0 } else { 2 });
|
||||||
} else {
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !quiet {
|
if !quiet {
|
||||||
eprintln!();
|
eprintln!();
|
||||||
eprintln!("Success! Your code complies the Alejandra style");
|
eprintln!("Success! Your code complies the Alejandra style");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,26 +39,35 @@ pub fn in_memory(path: String, before: String) -> (Status, String) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn in_place(path: String) -> Status {
|
pub fn in_fs(path: String, in_place: bool) -> Status {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
match std::fs::read_to_string(&path) {
|
match std::fs::read_to_string(&path) {
|
||||||
Ok(before) => {
|
Ok(before) => {
|
||||||
let (status, data) = crate::format::in_memory(path.clone(), before);
|
let (status, data) = crate::format::in_memory(path.clone(), before);
|
||||||
|
|
||||||
if let Status::Changed(changed) = status {
|
match status {
|
||||||
if changed {
|
Status::Changed(changed) => {
|
||||||
return match std::fs::File::create(path) {
|
if in_place {
|
||||||
Ok(mut file) => match file.write_all(data.as_bytes()) {
|
if changed {
|
||||||
Ok(_) => status,
|
match std::fs::File::create(path) {
|
||||||
Err(error) => Status::from(error),
|
Ok(mut file) => {
|
||||||
},
|
match file.write_all(data.as_bytes()) {
|
||||||
Err(error) => Status::from(error),
|
Ok(_) => Status::Changed(true),
|
||||||
};
|
Err(error) => Status::from(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => Status::from(error),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Status::Changed(false)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Status::Changed(changed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Status::Error(error) => Status::Error(error),
|
||||||
}
|
}
|
||||||
|
|
||||||
status
|
|
||||||
}
|
}
|
||||||
Err(error) => Status::from(error),
|
Err(error) => Status::from(error),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue