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
@const fmt_io::IOBuffer @const fmt_io::IOBuffer
fmt_tree::Union{JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead}, Nothing} fmt_tree::Union{JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead}, Nothing}
# User settings # User settings
quiet::Bool
verbose::Bool verbose::Bool
assert::Bool assert::Bool
debug::Bool debug::Bool
@ -47,7 +48,7 @@ end
function Context( function Context(
src_str; assert::Bool = true, debug::Bool = false, verbose::Bool = debug, 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_io = IOBuffer(src_str)
src_tree = JuliaSyntax.parseall(JuliaSyntax.GreenNode, src_str; ignore_warnings = true) src_tree = JuliaSyntax.parseall(JuliaSyntax.GreenNode, src_str; ignore_warnings = true)
@ -58,7 +59,7 @@ function Context(
assert = debug ? true : assert assert = debug ? true : assert
return Context( return Context(
src_str, src_tree, src_io, fmt_io, fmt_tree, 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 end

43
src/main.jl

@ -11,6 +11,8 @@ function panic(msg...)
for m in msg for m in msg
if m isa Exception if m isa Exception
showerror(stderr, m) showerror(stderr, m)
elseif m isa Vector{Base.StackFrame}
Base.show_backtrace(stderr, m)
else else
print(stderr, msg...) print(stderr, msg...)
end end
@ -20,6 +22,9 @@ function panic(msg...)
return errno return errno
end end
okln() = printstyled(stderr, "\n"; color = :green, bold = true)
errln() = printstyled(stderr, "\n"; color = :red, bold = true)
# Print a typical cli program help message # Print a typical cli program help message
function print_help() function print_help()
io = stdout io = stdout
@ -89,6 +94,7 @@ function main(argv)
# Default values # Default values
inputfiles = String[] inputfiles = String[]
outputfile = nothing outputfile = nothing
quiet = false
verbose = false verbose = false
debug = false debug = false
inplace = false inplace = false
@ -100,11 +106,13 @@ function main(argv)
x = popfirst!(argv) x = popfirst!(argv)
if x == "-i" || x == "--inplace" if x == "-i" || x == "--inplace"
inplace = true inplace = true
elseif x == "-q" || x == "--quiet"
quiet = true
elseif x == "-v" || x == "--verbose" elseif x == "-v" || x == "--verbose"
verbose = true verbose = true
elseif x == "-d" || x == "--diff" elseif x == "-d" || x == "--diff"
diff = true diff = true
elseif x == "c" || x == "--check" elseif x == "-c" || x == "--check"
check = true check = true
elseif x == "-vv" || x == "--debug" elseif x == "-vv" || x == "--debug"
debug = verbose = true debug = verbose = true
@ -189,12 +197,15 @@ function main(argv)
end end
# Check output # Check output
output_is_file = false
output_is_samefile = false
if inplace if inplace
@assert outputfile === nothing @assert outputfile === nothing
@assert isfile(inputfile) @assert isfile(inputfile)
@assert input_is_file @assert input_is_file
# @assert length(inputfiles) == 1 # checked above # @assert length(inputfiles) == 1 # checked above
output = inputfile output = inputfile
output_is_samefile = output_is_file = true
elseif check elseif check
@assert outputfile === nothing @assert outputfile === nothing
output = devnull output = devnull
@ -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") return panic("can not use same file for input and output, use `-i` to modify a file in place")
else else
output = outputfile 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
end end
# Call the library to format the text # Call the library to format the text
ctx = try ctx = try
ctx = Context(sourcetext; verbose = verbose, debug = debug, diff = diff, check = check) ctx = Context(sourcetext; quiet, verbose, debug, diff, check)
format_tree!(ctx) format_tree!(ctx)
ctx ctx
catch err 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 continue
end end
@ -223,16 +253,21 @@ function main(argv)
changed = ctx.fmt_tree !== ctx.src_tree changed = ctx.fmt_tree !== ctx.src_tree
if check if check
if changed if changed
print_progress && errln()
global errno = 1 global errno = 1
else
print_progress && okln()
end end
elseif changed || !inplace elseif changed || !inplace
try try
write(output, take!(ctx.fmt_io)) write(output, take!(ctx.fmt_io))
catch err catch err
print_progress && errln()
panic("could not write to output: ", err) panic("could not write to output: ", err)
end end
print_progress && okln()
else else
# Log if verbose perhaps print_progress && okln()
end end
if diff if diff
error("todo") error("todo")

Loading…
Cancel
Save