Browse Source

main: some pretty printing when formatting files.

pull/19/head
Fredrik Ekre 2 years ago
parent
commit
4912ba7b1b
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 5
      src/Runic.jl
  2. 43
      src/main.jl

5
src/Runic.jl

@ -33,6 +33,7 @@ mutable struct Context @@ -33,6 +33,7 @@ mutable struct Context
@const fmt_io::IOBuffer
fmt_tree::Union{JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead}, Nothing}
# User settings
quiet::Bool
verbose::Bool
assert::Bool
debug::Bool
@ -47,7 +48,7 @@ end @@ -47,7 +48,7 @@ end
function Context(
src_str; assert::Bool = true, debug::Bool = false, verbose::Bool = debug,
diff::Bool = false, check::Bool = false,
diff::Bool = false, check::Bool = false, quiet::Bool = false,
)
src_io = IOBuffer(src_str)
src_tree = JuliaSyntax.parseall(JuliaSyntax.GreenNode, src_str; ignore_warnings = true)
@ -58,7 +59,7 @@ function Context( @@ -58,7 +59,7 @@ function Context(
assert = debug ? true : assert
return Context(
src_str, src_tree, src_io, fmt_io, fmt_tree,
verbose, assert, debug, check, diff, nothing, nothing,
quiet, verbose, assert, debug, check, diff, nothing, nothing,
)
end

43
src/main.jl

@ -11,6 +11,8 @@ function panic(msg...) @@ -11,6 +11,8 @@ function panic(msg...)
for m in msg
if m isa Exception
showerror(stderr, m)
elseif m isa Vector{Base.StackFrame}
Base.show_backtrace(stderr, m)
else
print(stderr, msg...)
end
@ -20,6 +22,9 @@ function panic(msg...) @@ -20,6 +22,9 @@ function panic(msg...)
return errno
end
okln() = printstyled(stderr, "\n"; color = :green, bold = true)
errln() = printstyled(stderr, "\n"; color = :red, bold = true)
# Print a typical cli program help message
function print_help()
io = stdout
@ -89,6 +94,7 @@ function main(argv) @@ -89,6 +94,7 @@ function main(argv)
# Default values
inputfiles = String[]
outputfile = nothing
quiet = false
verbose = false
debug = false
inplace = false
@ -100,11 +106,13 @@ function main(argv) @@ -100,11 +106,13 @@ function main(argv)
x = popfirst!(argv)
if x == "-i" || x == "--inplace"
inplace = true
elseif x == "-q" || x == "--quiet"
quiet = true
elseif x == "-v" || x == "--verbose"
verbose = true
elseif x == "-d" || x == "--diff"
diff = true
elseif x == "c" || x == "--check"
elseif x == "-c" || x == "--check"
check = true
elseif x == "-vv" || x == "--debug"
debug = verbose = true
@ -189,12 +197,15 @@ function main(argv) @@ -189,12 +197,15 @@ function main(argv)
end
# Check output
output_is_file = false
output_is_samefile = false
if inplace
@assert outputfile === nothing
@assert isfile(inputfile)
@assert input_is_file
# @assert length(inputfiles) == 1 # checked above
output = inputfile
output_is_samefile = output_is_file = true
elseif check
@assert outputfile === nothing
output = devnull
@ -206,16 +217,35 @@ function main(argv) @@ -206,16 +217,35 @@ function main(argv)
return panic("can not use same file for input and output, use `-i` to modify a file in place")
else
output = outputfile
output_is_file = true
end
end
# Print file info unless quiet and unless stdin and/or stdout is involved
print_progress = !(quiet || !input_is_file || !(output_is_file || check))
# Print file info unless quiet and unless formatting stdin -> stdout
if print_progress
input_pretty = relpath(inputfile)
if check
printstyled(stderr, "Checking `$(input_pretty)` ... "; color = :blue)
else
to = output_is_samefile ? "" : "-> `$(relpath(output))` "
printstyled(stderr, "Formatting `$(inputfile)` $(to) ... "; color = :blue)
end
end
# Call the library to format the text
ctx = try
ctx = Context(sourcetext; verbose = verbose, debug = debug, diff = diff, check = check)
ctx = Context(sourcetext; quiet, verbose, debug, diff, check)
format_tree!(ctx)
ctx
catch err
panic(err)
print_progress && errln()
# Limit stacktrace to 5 frames because Runic uses recursion a lot and 5 should
# be enough to see where the error occurred.
bt = stacktrace(catch_backtrace())[1:5]
panic(err, bt)
continue
end
@ -223,16 +253,21 @@ function main(argv) @@ -223,16 +253,21 @@ function main(argv)
changed = ctx.fmt_tree !== ctx.src_tree
if check
if changed
print_progress && errln()
global errno = 1
else
print_progress && okln()
end
elseif changed || !inplace
try
write(output, take!(ctx.fmt_io))
catch err
print_progress && errln()
panic("could not write to output: ", err)
end
print_progress && okln()
else
# Log if verbose perhaps
print_progress && okln()
end
if diff
error("todo")

Loading…
Cancel
Save