Skip to content

Commit cf8b9d8

Browse files
committed
feat: more levels of --quiet
1 parent ccb31f3 commit cf8b9d8

File tree

6 files changed

+88
-50
lines changed

6 files changed

+88
-50
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797

9898
- [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=kamadorueda.alejandra)
9999
- [Neovim](./integrations/neovim/README.md)
100+
- [Vim](./integrations/vim/README.md)
100101
- [GNU Emacs](./integrations/gnu-emacs/README.md)
101102
- [Doom Emacs](./integrations/doom-emacs/README.md)
102103
- [Pre-commit](./integrations/pre-commit/README.md)

integrations/neovim/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
In order to use Alejandra with
44
[Neovim](https://neovim.io/)
5-
please use any of the following plugins:
5+
please use the `:%!alejandra -qq` command
6+
to format the current buffer,
7+
or use any of the following plugins:
68

79
- [Neoformat](https://github.com/sbdchd/neoformat)
810
- [null-ls.nvim](https://github.com/jose-elias-alvarez/null-ls.nvim)

integrations/vim/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Vim integration
2+
3+
In order to use Alejandra with
4+
[Vim](https://www.vim.org/)
5+
please use the `:%!alejandra -qq` command
6+
to format the current buffer.

src/alejandra_cli/src/cli.rs

+59-47
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
use std::io::Read;
22

3+
use clap::value_parser;
4+
use clap::ArgAction;
35
use clap::Parser;
46
use futures::future::RemoteHandle;
57
use futures::stream::FuturesUnordered;
68
use futures::task::SpawnExt;
79

810
use crate::ads::random_ad;
9-
10-
#[derive(Clone)]
11-
pub(crate) struct FormattedPath {
12-
pub path: String,
13-
pub status: alejandra::format::Status,
14-
}
15-
16-
const AFTER_HELP: &str = concat!(
17-
"Alejandra will exit with status code:\n",
18-
" 1, if any error occurs.\n",
19-
" 2, if --check was used and any file requires formatting.\n",
20-
" 0, otherwise.",
21-
);
11+
use crate::verbosity::Verbosity;
2212

2313
/// The Uncompromising Nix Code Formatter.
2414
#[derive(Debug, Parser)]
2515
#[clap(
2616
name="Alejandra",
2717
28-
after_help = AFTER_HELP,
18+
after_help = concat!(
19+
"Alejandra will exit with status code:\n",
20+
" 1, if any error occurs.\n",
21+
" 2, if --check was used and any file requires formatting.\n",
22+
" 0, otherwise.",
23+
),
2924
term_width = 80,
3025
version,
3126
)]
32-
struct Args {
27+
struct CLIArgs {
3328
/// Files or directories, or a single "-" (or leave empty) to format stdin.
3429
#[clap(multiple_values = true)]
3530
include: Vec<String>,
@@ -45,25 +40,34 @@ struct Args {
4540

4641
/// Number of formatting threads to spawn. Defaults to the number of
4742
/// physical CPUs.
48-
#[clap(long, short, value_parser = clap::value_parser!(u8).range(1..))]
43+
#[clap(long, short, value_parser = value_parser!(u8).range(1..))]
4944
threads: Option<u8>,
5045

51-
/// Hide the details, only show error messages.
52-
#[clap(long, short)]
53-
quiet: bool,
46+
/// Use once to hide informational messages,
47+
/// twice to hide error messages.
48+
#[clap(long, short, action = ArgAction::Count)]
49+
quiet: u8,
50+
}
51+
52+
#[derive(Clone)]
53+
struct FormattedPath {
54+
pub path: String,
55+
pub status: alejandra::format::Status,
5456
}
5557

56-
pub(crate) fn stdin(quiet: bool) -> FormattedPath {
58+
fn format_stdin(verbosity: Verbosity) -> FormattedPath {
5759
let mut before = String::new();
5860
let path = "<anonymous file on stdin>".to_string();
5961

60-
if !quiet {
62+
if verbosity.allows_info() {
6163
eprintln!("Formatting stdin.");
6264
eprintln!("Use --help to see all command line options.");
63-
eprintln!("use --quiet to suppress this and all messages.");
65+
eprintln!("use --quiet to suppress this and other messages.");
6466
}
6567

66-
std::io::stdin().read_to_string(&mut before).unwrap();
68+
std::io::stdin()
69+
.read_to_string(&mut before)
70+
.expect("Unable to read stdin.");
6771

6872
let (status, data) =
6973
alejandra::format::in_memory(path.clone(), before.clone());
@@ -73,15 +77,15 @@ pub(crate) fn stdin(quiet: bool) -> FormattedPath {
7377
FormattedPath { path, status }
7478
}
7579

76-
pub(crate) fn simple(
80+
fn format_paths(
7781
paths: Vec<String>,
7882
in_place: bool,
79-
quiet: bool,
83+
verbosity: Verbosity,
8084
threads: usize,
8185
) -> Vec<FormattedPath> {
8286
let paths_len = paths.len();
8387

84-
if !quiet {
88+
if verbosity.allows_info() {
8589
eprintln!(
8690
"{} {paths_len} file{} using {threads} thread{}.",
8791
"Checking style in",
@@ -94,7 +98,7 @@ pub(crate) fn simple(
9498
let pool = futures::executor::ThreadPoolBuilder::new()
9599
.pool_size(threads)
96100
.create()
97-
.expect("Unable to instantiate a new thread pool");
101+
.expect("Unable to instantiate a new thread pool.");
98102

99103
let futures: FuturesUnordered<RemoteHandle<FormattedPath>> = paths
100104
.into_iter()
@@ -103,7 +107,7 @@ pub(crate) fn simple(
103107
let status = alejandra::format::in_fs(path.clone(), in_place);
104108

105109
if let alejandra::format::Status::Changed(changed) = status {
106-
if changed && !quiet {
110+
if changed && verbosity.allows_info() {
107111
println!(
108112
"{}: {path}",
109113
if in_place {
@@ -117,33 +121,38 @@ pub(crate) fn simple(
117121

118122
FormattedPath { path: path.clone(), status }
119123
})
120-
.expect("Unable to spawn formatting task")
124+
.expect("Unable to spawn formatting task.")
121125
})
122126
.collect();
123127

124128
futures::executor::block_on_stream(futures).collect()
125129
}
126130

127131
pub fn main() -> std::io::Result<()> {
128-
let args = Args::parse();
132+
let args = CLIArgs::parse();
129133

130134
let in_place = !args.check;
131135

132136
let include: Vec<&str> =
133137
args.include.iter().map(String::as_str).collect::<Vec<&str>>();
134138

135-
let threads = args
136-
.threads
137-
.map_or_else(num_cpus::get_physical, |threads| threads as usize);
139+
let threads =
140+
args.threads.map_or_else(num_cpus::get_physical, Into::<usize>::into);
141+
142+
let verbosity = match args.quiet {
143+
0 => Verbosity::Everything,
144+
1 => Verbosity::NoInfo,
145+
_ => Verbosity::NoErrors,
146+
};
138147

139148
let formatted_paths = match &include[..] {
140149
&[] | &["-"] => {
141-
vec![crate::cli::stdin(args.quiet)]
150+
vec![crate::cli::format_stdin(verbosity)]
142151
},
143152
include => {
144153
let paths = crate::find::nix_files(include, &args.exclude);
145154

146-
crate::cli::simple(paths, in_place, args.quiet, threads)
155+
crate::cli::format_paths(paths, in_place, verbosity, threads)
147156
},
148157
};
149158

@@ -155,18 +164,21 @@ pub fn main() -> std::io::Result<()> {
155164
.count();
156165

157166
if errors > 0 {
158-
eprintln!();
159-
eprintln!(
160-
"Failed! {errors} error{} found at:",
161-
if errors == 1 { "" } else { "s" }
162-
);
163-
for formatted_path in formatted_paths {
164-
if let alejandra::format::Status::Error(error) =
165-
formatted_path.status
166-
{
167-
eprintln!("- {}: {error}", formatted_path.path);
167+
if verbosity.allows_errors() {
168+
eprintln!();
169+
eprintln!(
170+
"Failed! {errors} error{} found at:",
171+
if errors == 1 { "" } else { "s" }
172+
);
173+
for formatted_path in formatted_paths {
174+
if let alejandra::format::Status::Error(error) =
175+
formatted_path.status
176+
{
177+
eprintln!("- {}: {error}", formatted_path.path);
178+
}
168179
}
169180
}
181+
170182
std::process::exit(1);
171183
}
172184

@@ -181,7 +193,7 @@ pub fn main() -> std::io::Result<()> {
181193
.count();
182194

183195
if changed > 0 {
184-
if !args.quiet {
196+
if verbosity.allows_info() {
185197
eprintln!();
186198
eprintln!(
187199
"{}! {changed} file{} {}.",
@@ -204,7 +216,7 @@ pub fn main() -> std::io::Result<()> {
204216
std::process::exit(if in_place { 0 } else { 2 });
205217
}
206218

207-
if !args.quiet {
219+
if verbosity.allows_info() {
208220
eprintln!();
209221
eprintln!("Congratulations! Your code complies with the Alejandra style.");
210222
eprintln!();

src/alejandra_cli/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
pub mod ads;
1+
mod ads;
22
pub mod cli;
3-
pub mod find;
3+
mod find;
4+
mod verbosity;

src/alejandra_cli/src/verbosity.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[derive(Clone, Copy)]
2+
pub(crate) enum Verbosity {
3+
Everything,
4+
NoInfo,
5+
NoErrors,
6+
}
7+
8+
impl Verbosity {
9+
pub(crate) fn allows_info(&self) -> bool {
10+
matches!(self, Verbosity::Everything)
11+
}
12+
13+
pub(crate) fn allows_errors(&self) -> bool {
14+
self.allows_info() || matches!(self, Verbosity::NoInfo)
15+
}
16+
}

0 commit comments

Comments
 (0)