Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement --verbose and make Runic silent by default #121

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- New command line argument `--verbose` which enables verbose output ([#121]).
### Changed
- Runic is now silent by default. Use `--verbose` to enable the verbose file progress
printing from previous releases ([#121]).

## [v1.2.0] - 2024-12-09
### Added
- New command line option `--lines=a:b` for limiting formatting to lines `a` to `b`.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ OPTIONS
-o <file>, --output=<file>
File to write formatted output to. If no output is given, or if the file
is `-`, output is written to stdout.

-v, --verbose
Enable verbose output.

--version
Print Runic and julia version information.
```

In addition to the CLI there is also the two function `Runic.format_file` and
Expand Down
44 changes: 28 additions & 16 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@
return errno
end

function okln()
blue(str) = printstyled(stderr, str; color = :blue)
function okln(str)
blue(str)

Check warning on line 90 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L88-L90

Added lines #L88 - L90 were not covered by tests
printstyled(stderr, "✔"; color = :green, bold = true)
println(stderr)
return
end
function errln()
function errln(str)
blue(str)

Check warning on line 96 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L95-L96

Added lines #L95 - L96 were not covered by tests
printstyled(stderr, "✖"; color = :red, bold = true)
println(stderr)
return
Expand Down Expand Up @@ -143,6 +146,9 @@
File to write formatted output to. If no output is given, or if the file
is `-`, output is written to stdout.

-v, --verbose
Enable verbose output.

--version
Print Runic and julia version information.
"""
Expand Down Expand Up @@ -313,7 +319,8 @@
end

# Loop over the input files
for inputfile in inputfiles
nfiles_str = string(length(inputfiles))
for (file_counter, inputfile) in enumerate(inputfiles)
# Read the input
if input_is_stdin
@assert length(inputfiles) == 1
Expand Down Expand Up @@ -355,21 +362,25 @@
end
end

# Print file info unless quiet and unless stdin and/or stdout is involved
print_progress = !(quiet || input_is_stdin || !(output.output_is_file || check))
# Print file info if `verbose` unless piping from/to stdin/stdout
print_progress = verbose && !(input_is_stdin || !(output.output_is_file || check))

# Print file info unless quiet and unless input/output is stdin/stdout
if print_progress
@assert inputfile != "-"
input_pretty = relpath(inputfile)
prefix = string(

Check warning on line 372 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L372

Added line #L372 was not covered by tests
"[", lpad(string(file_counter), textwidth(nfiles_str), " "), "/",
nfiles_str, "] "
)
if Sys.iswindows()
input_pretty = replace(input_pretty, "\\" => "/")
end
if check
str = "Checking `$(input_pretty)` "
str = string(prefix, "Checking `", input_pretty, "` ")

Check warning on line 380 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L380

Added line #L380 was not covered by tests
ndots = 80 - textwidth(str) - 1 - 1
dots = ndots > 0 ? "."^ndots : ""
printstyled(stderr, str * dots * " "; color = :blue)
str = string(str, dots, " ")

Check warning on line 383 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L383

Added line #L383 was not covered by tests
else
if output.output_is_samefile
output_pretty = " "
Expand All @@ -380,10 +391,10 @@
end
output_pretty = " -> `$(output_pretty)` "
end
str = "Formatting `$(input_pretty)`$(output_pretty)"
str = string(prefix, "Formatting `", input_pretty, "`", output_pretty)

Check warning on line 394 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L394

Added line #L394 was not covered by tests
ndots = 80 - textwidth(str) - 1 - 1
dots = ndots > 0 ? "."^ndots : ""
printstyled(stderr, str * dots * " "; color = :blue)
str = string(str, dots, " ")

Check warning on line 397 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L397

Added line #L397 was not covered by tests
end
end

Expand All @@ -393,7 +404,7 @@
format_tree!(ctx′)
ctx′
catch err
print_progress && errln()
print_progress && errln(str)

Check warning on line 407 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L407

Added line #L407 was not covered by tests
if err isa JuliaSyntax.ParseError
panic("failed to parse input: ", err)
continue
Expand Down Expand Up @@ -421,24 +432,25 @@
changed = !nodes_equal(ctx.fmt_tree, ctx.src_tree)
if check
if changed
print_progress && errln()
print_progress && errln(str)

Check warning on line 435 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L435

Added line #L435 was not covered by tests
global errno = 1
else
print_progress && okln()
print_progress && okln(str)
end
elseif changed || !inplace
@assert output.which !== :devnull
try
writeo(output, seekstart(ctx.fmt_io))
catch err
print_progress && errln()
print_progress && errln(str)

Check warning on line 445 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L445

Added line #L445 was not covered by tests
panic("could not write to output file `$(output.file)`: ", err)
continue

Check warning on line 447 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L447

Added line #L447 was not covered by tests
end
print_progress && okln()
print_progress && okln(str)

Check warning on line 449 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L449

Added line #L449 was not covered by tests
else
print_progress && okln()
print_progress && okln(str)

Check warning on line 451 in src/main.jl

View check run for this annotation

Codecov / codecov/patch

src/main.jl#L451

Added line #L451 was not covered by tests
end
if diff
if changed && diff
mktempdir() do dir
a = mkdir(joinpath(dir, "a"))
b = mkdir(joinpath(dir, "b"))
Expand Down
102 changes: 95 additions & 7 deletions test/maintests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,20 @@ function maintests(f::R) where {R}
write(f_in, bad)
f_out = "out.jl"
for argv in [["--output=$f_out", f_in], ["-o", f_out, f_in]]
rm(f_out, force = true)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1) && isempty(fd2)
@test read(f_out, String) == good
@test read(f_in, String) == bad
end
# --verbose
let argv = ["--verbose", "--output=$f_out", f_in]
rm(f_out, force = true)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1)
@test occursin("Formatting `in.jl` -> `out.jl` ...", fd2)
@test occursin("[1/1] Formatting `in.jl` -> `out.jl` ...", fd2)
@test occursin("✔", fd2)
@test !occursin("✖", fd2)
@test read(f_out, String) == good
Expand All @@ -107,11 +116,19 @@ function maintests(f::R) where {R}
cdtmp() do
f_in = "in.jl"
for argv in [["--inplace", f_in], ["-i", f_in]]
write(f_in, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1) && isempty(fd2)
@test read(f_in, String) == good
end
# --verbose
let argv = ["-v", "--inplace", f_in]
write(f_in, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1)
@test occursin("Formatting `in.jl` ...", fd2)
@test occursin("[1/1] Formatting `in.jl` ...", fd2)
@test occursin("✔", fd2)
@test !occursin("✖", fd2)
@test read(f_in, String) == good
Expand All @@ -125,8 +142,15 @@ function maintests(f::R) where {R}
write(f_in, good)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1)
@test occursin("Formatting `in.jl` ...", fd2)
@test isempty(fd1) && isempty(fd2)
@test read(f_in, String) == good
end
# --verbose
let argv = ["--verbose", "--inplace", f_in]
write(f_in, good)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test occursin("[1/1] Formatting `in.jl` ...", fd2)
@test occursin("✔", fd2)
@test !occursin("✖", fd2)
@test read(f_in, String) == good
Expand All @@ -144,13 +168,23 @@ function maintests(f::R) where {R}
markdownfile = "markdown.md"
write(markdownfile, "this is not a Julia file")
for argv in [["--inplace", "."], ["-i", "."], ["-i", ".", "src"]]
write(fgood, good)
write(fbad, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1) && isempty(fd2)
@test read(fgood, String) == read(fbad, String) == good
end
# --verbose
let argv = ["-v", "--inplace", "."]
write(fgood, good)
write(fbad, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1)
@test occursin("Formatting `good.jl` ...", fd2)
@test occursin("Formatting `src/bad.jl` ...", fd2)
@test occursin("[1/2]", fd2) && occursin("[2/2]", fd2)
@test occursin("✔", fd2)
@test !occursin("✖", fd2)
@test !occursin("git.jl", fd2)
Expand All @@ -163,11 +197,19 @@ function maintests(f::R) where {R}
cdtmp() do
f_in = "in.jl"
for argv in [["--check", f_in], ["-c", f_in]]
write(f_in, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 1
@test isempty(fd1) && isempty(fd2)
@test read(f_in, String) == bad
end
# --verbose
let argv = ["--verbose", "--check", f_in]
write(f_in, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 1
@test isempty(fd1)
@test occursin("Checking `in.jl` ...", fd2)
@test occursin("[1/1] Checking `in.jl` ...", fd2)
@test !occursin("✔", fd2)
@test occursin("✖", fd2)
@test read(f_in, String) == bad
Expand All @@ -178,11 +220,18 @@ function maintests(f::R) where {R}
cdtmp() do
f_in = "in.jl"
for argv in [["--check", f_in], ["-c", f_in]]
write(f_in, good)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1) && isempty(fd2)
@test read(f_in, String) == good
end
let argv = ["-v", "--check", f_in]
write(f_in, good)
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1)
@test occursin("Checking `in.jl` ...", fd2)
@test occursin("[1/1] Checking `in.jl` ...", fd2)
@test occursin("✔", fd2)
@test !occursin("✖", fd2)
@test read(f_in, String) == good
Expand All @@ -200,13 +249,24 @@ function maintests(f::R) where {R}
markdownfile = "markdown.md"
write(markdownfile, "this is not a Julia file")
for argv in [["--check", "."], ["-c", "."]]
write(fgood, good)
write(fbad, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 1
@test isempty(fd1) && isempty(fd2)
@test read(fgood, String) == good
@test read(fbad, String) == bad
end
# --verbose
let argv = ["--verbose", "--check", "."]
write(fgood, good)
write(fbad, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 1
@test isempty(fd1)
@test occursin("Checking `good.jl` ...", fd2)
@test occursin("Checking `src/bad.jl` ...", fd2)
@test occursin("[1/2]", fd2) && occursin("[2/2]", fd2)
@test occursin("✔", fd2)
@test occursin("✖", fd2)
@test !occursin("git.jl", fd2)
Expand All @@ -225,7 +285,20 @@ function maintests(f::R) where {R}
rc, fd1, fd2 = runic(argv)
@test rc == 1
@test isempty(fd1)
@test occursin("Checking `in.jl` ...", fd2)
@test !occursin("Checking `in.jl` ...", fd2)
@test !occursin("✔", fd2)
@test !occursin("✖", fd2)
@test occursin("diff --git", fd2)
@test occursin("-1+1", fd2)
@test occursin("+1 + 1", fd2)
@test read(f_in, String) == bad
end
let argv = ["-v", "--check", "--diff", f_in]
write(f_in, bad)
rc, fd1, fd2 = runic(argv)
@test rc == 1
@test isempty(fd1)
@test occursin("[1/1] Checking `in.jl` ...", fd2)
@test !occursin("✔", fd2)
@test occursin("✖", fd2)
@test occursin("diff --git", fd2)
Expand All @@ -236,6 +309,21 @@ function maintests(f::R) where {R}
end
end

# runic --verbose
cdtmp() do
f_in = "in.jl"
write(f_in, good)
let argv = ["--verbose"; "--check"; fill(f_in, 10)]
rc, fd1, fd2 = runic(argv)
@test rc == 0
@test isempty(fd1)
for i in 1:9
@test occursin("[ $(i)/10] Checking `in.jl` ...", fd2)
end
@test occursin("[10/10] Checking `in.jl` ...", fd2)
end
end

# Error paths
# runic -o
let (rc, fd1, fd2) = runic(["-o"])
Expand Down
Loading