Browse Source

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

This patch implements a new command line argument `--verbose` which
enables verbose ouutput. Runic is now silent by default so `--verbose`
re-enables the verbose file printing from previous releases.

This patch also adds a progress prefix to each file of the form
`[file/nfiles]` to verbose output.
pull/123/head
Fredrik Ekre 12 months ago committed by GitHub
parent
commit
1fb902811e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      CHANGELOG.md
  2. 6
      README.md
  3. 44
      src/main.jl
  4. 102
      test/maintests.jl

7
CHANGELOG.md

@ -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/), 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). 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 ## [v1.2.0] - 2024-12-09
### Added ### Added
- New command line option `--lines=a:b` for limiting formatting to lines `a` to `b`. - New command line option `--lines=a:b` for limiting formatting to lines `a` to `b`.

6
README.md

@ -111,6 +111,12 @@ OPTIONS
-o <file>, --output=<file> -o <file>, --output=<file>
File to write formatted output to. If no output is given, or if the file File to write formatted output to. If no output is given, or if the file
is `-`, output is written to stdout. 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 In addition to the CLI there is also the two function `Runic.format_file` and

44
src/main.jl

@ -85,12 +85,15 @@ function panic(
return errno return errno
end end
function okln() blue(str) = printstyled(stderr, str; color = :blue)
function okln(str)
blue(str)
printstyled(stderr, ""; color = :green, bold = true) printstyled(stderr, ""; color = :green, bold = true)
println(stderr) println(stderr)
return return
end end
function errln() function errln(str)
blue(str)
printstyled(stderr, ""; color = :red, bold = true) printstyled(stderr, ""; color = :red, bold = true)
println(stderr) println(stderr)
return return
@ -143,6 +146,9 @@ function print_help()
File to write formatted output to. If no output is given, or if the file File to write formatted output to. If no output is given, or if the file
is `-`, output is written to stdout. is `-`, output is written to stdout.
-v, --verbose
Enable verbose output.
--version --version
Print Runic and julia version information. Print Runic and julia version information.
""" """
@ -313,7 +319,8 @@ function main(argv)
end end
# Loop over the input files # Loop over the input files
for inputfile in inputfiles nfiles_str = string(length(inputfiles))
for (file_counter, inputfile) in enumerate(inputfiles)
# Read the input # Read the input
if input_is_stdin if input_is_stdin
@assert length(inputfiles) == 1 @assert length(inputfiles) == 1
@ -355,21 +362,25 @@ function main(argv)
end end
end end
# Print file info unless quiet and unless stdin and/or stdout is involved # Print file info if `verbose` unless piping from/to stdin/stdout
print_progress = !(quiet || input_is_stdin || !(output.output_is_file || check)) print_progress = verbose && !(input_is_stdin || !(output.output_is_file || check))
# Print file info unless quiet and unless input/output is stdin/stdout # Print file info unless quiet and unless input/output is stdin/stdout
if print_progress if print_progress
@assert inputfile != "-" @assert inputfile != "-"
input_pretty = relpath(inputfile) input_pretty = relpath(inputfile)
prefix = string(
"[", lpad(string(file_counter), textwidth(nfiles_str), " "), "/",
nfiles_str, "] "
)
if Sys.iswindows() if Sys.iswindows()
input_pretty = replace(input_pretty, "\\" => "/") input_pretty = replace(input_pretty, "\\" => "/")
end end
if check if check
str = "Checking `$(input_pretty)` " str = string(prefix, "Checking `", input_pretty, "` ")
ndots = 80 - textwidth(str) - 1 - 1 ndots = 80 - textwidth(str) - 1 - 1
dots = ndots > 0 ? "."^ndots : "" dots = ndots > 0 ? "."^ndots : ""
printstyled(stderr, str * dots * " "; color = :blue) str = string(str, dots, " ")
else else
if output.output_is_samefile if output.output_is_samefile
output_pretty = " " output_pretty = " "
@ -380,10 +391,10 @@ function main(argv)
end end
output_pretty = " -> `$(output_pretty)` " output_pretty = " -> `$(output_pretty)` "
end end
str = "Formatting `$(input_pretty)`$(output_pretty)" str = string(prefix, "Formatting `", input_pretty, "`", output_pretty)
ndots = 80 - textwidth(str) - 1 - 1 ndots = 80 - textwidth(str) - 1 - 1
dots = ndots > 0 ? "."^ndots : "" dots = ndots > 0 ? "."^ndots : ""
printstyled(stderr, str * dots * " "; color = :blue) str = string(str, dots, " ")
end end
end end
@ -393,7 +404,7 @@ function main(argv)
format_tree!(ctx′) format_tree!(ctx′)
ctx′ ctx′
catch err catch err
print_progress && errln() print_progress && errln(str)
if err isa JuliaSyntax.ParseError if err isa JuliaSyntax.ParseError
panic("failed to parse input: ", err) panic("failed to parse input: ", err)
continue continue
@ -421,24 +432,25 @@ function main(argv)
changed = !nodes_equal(ctx.fmt_tree, ctx.src_tree) changed = !nodes_equal(ctx.fmt_tree, ctx.src_tree)
if check if check
if changed if changed
print_progress && errln() print_progress && errln(str)
global errno = 1 global errno = 1
else else
print_progress && okln() print_progress && okln(str)
end end
elseif changed || !inplace elseif changed || !inplace
@assert output.which !== :devnull @assert output.which !== :devnull
try try
writeo(output, seekstart(ctx.fmt_io)) writeo(output, seekstart(ctx.fmt_io))
catch err catch err
print_progress && errln() print_progress && errln(str)
panic("could not write to output file `$(output.file)`: ", err) panic("could not write to output file `$(output.file)`: ", err)
continue
end end
print_progress && okln() print_progress && okln(str)
else else
print_progress && okln() print_progress && okln(str)
end end
if diff if changed && diff
mktempdir() do dir mktempdir() do dir
a = mkdir(joinpath(dir, "a")) a = mkdir(joinpath(dir, "a"))
b = mkdir(joinpath(dir, "b")) b = mkdir(joinpath(dir, "b"))

102
test/maintests.jl

@ -91,11 +91,20 @@ function maintests(f::R) where {R}
write(f_in, bad) write(f_in, bad)
f_out = "out.jl" f_out = "out.jl"
for argv in [["--output=$f_out", f_in], ["-o", f_out, f_in]] 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) rm(f_out, force = true)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@test rc == 0 @test rc == 0
@test isempty(fd1) @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 !occursin("", fd2) @test !occursin("", fd2)
@test read(f_out, String) == good @test read(f_out, String) == good
@ -107,11 +116,19 @@ function maintests(f::R) where {R}
cdtmp() do cdtmp() do
f_in = "in.jl" f_in = "in.jl"
for argv in [["--inplace", f_in], ["-i", f_in]] 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) write(f_in, bad)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@test rc == 0 @test rc == 0
@test isempty(fd1) @test isempty(fd1)
@test occursin("Formatting `in.jl` ...", fd2) @test occursin("[1/1] Formatting `in.jl` ...", fd2)
@test occursin("", fd2) @test occursin("", fd2)
@test !occursin("", fd2) @test !occursin("", fd2)
@test read(f_in, String) == good @test read(f_in, String) == good
@ -125,8 +142,15 @@ function maintests(f::R) where {R}
write(f_in, good) write(f_in, good)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@test rc == 0 @test rc == 0
@test isempty(fd1) @test isempty(fd1) && isempty(fd2)
@test occursin("Formatting `in.jl` ...", 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 !occursin("", fd2) @test !occursin("", fd2)
@test read(f_in, String) == good @test read(f_in, String) == good
@ -144,6 +168,15 @@ function maintests(f::R) where {R}
markdownfile = "markdown.md" markdownfile = "markdown.md"
write(markdownfile, "this is not a Julia file") write(markdownfile, "this is not a Julia file")
for argv in [["--inplace", "."], ["-i", "."], ["-i", ".", "src"]] 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(fgood, good)
write(fbad, bad) write(fbad, bad)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@ -151,6 +184,7 @@ function maintests(f::R) where {R}
@test isempty(fd1) @test isempty(fd1)
@test occursin("Formatting `good.jl` ...", fd2) @test occursin("Formatting `good.jl` ...", fd2)
@test occursin("Formatting `src/bad.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("", fd2) @test !occursin("", fd2)
@test !occursin("git.jl", fd2) @test !occursin("git.jl", fd2)
@ -163,11 +197,19 @@ function maintests(f::R) where {R}
cdtmp() do cdtmp() do
f_in = "in.jl" f_in = "in.jl"
for argv in [["--check", f_in], ["-c", f_in]] 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) write(f_in, bad)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@test rc == 1 @test rc == 1
@test isempty(fd1) @test isempty(fd1)
@test occursin("Checking `in.jl` ...", fd2) @test occursin("[1/1] Checking `in.jl` ...", fd2)
@test !occursin("", fd2) @test !occursin("", fd2)
@test occursin("", fd2) @test occursin("", fd2)
@test read(f_in, String) == bad @test read(f_in, String) == bad
@ -178,11 +220,18 @@ function maintests(f::R) where {R}
cdtmp() do cdtmp() do
f_in = "in.jl" f_in = "in.jl"
for argv in [["--check", f_in], ["-c", f_in]] 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) write(f_in, good)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@test rc == 0 @test rc == 0
@test isempty(fd1) @test isempty(fd1)
@test occursin("Checking `in.jl` ...", fd2) @test occursin("[1/1] Checking `in.jl` ...", fd2)
@test occursin("", fd2) @test occursin("", fd2)
@test !occursin("", fd2) @test !occursin("", fd2)
@test read(f_in, String) == good @test read(f_in, String) == good
@ -200,6 +249,16 @@ function maintests(f::R) where {R}
markdownfile = "markdown.md" markdownfile = "markdown.md"
write(markdownfile, "this is not a Julia file") write(markdownfile, "this is not a Julia file")
for argv in [["--check", "."], ["-c", "."]] 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(fgood, good)
write(fbad, bad) write(fbad, bad)
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@ -207,6 +266,7 @@ function maintests(f::R) where {R}
@test isempty(fd1) @test isempty(fd1)
@test occursin("Checking `good.jl` ...", fd2) @test occursin("Checking `good.jl` ...", fd2)
@test occursin("Checking `src/bad.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("", fd2) @test occursin("", fd2)
@test !occursin("git.jl", fd2) @test !occursin("git.jl", fd2)
@ -225,7 +285,20 @@ function maintests(f::R) where {R}
rc, fd1, fd2 = runic(argv) rc, fd1, fd2 = runic(argv)
@test rc == 1 @test rc == 1
@test isempty(fd1) @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("", fd2) @test occursin("", fd2)
@test occursin("diff --git", fd2) @test occursin("diff --git", fd2)
@ -236,6 +309,21 @@ function maintests(f::R) where {R}
end end
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 # Error paths
# runic -o # runic -o
let (rc, fd1, fd2) = runic(["-o"]) let (rc, fd1, fd2) = runic(["-o"])

Loading…
Cancel
Save