From 4912ba7b1b6d88ca3847b1e023ffa71d61bb55ec Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 28 May 2024 23:50:16 +0200 Subject: [PATCH] main: some pretty printing when formatting files. --- src/Runic.jl | 5 +++-- src/main.jl | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Runic.jl b/src/Runic.jl index 2ea41f1..93a7acf 100644 --- a/src/Runic.jl +++ b/src/Runic.jl @@ -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 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( 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 diff --git a/src/main.jl b/src/main.jl index 09d254e..edac0da 100644 --- a/src/main.jl +++ b/src/main.jl @@ -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...) 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) # Default values inputfiles = String[] outputfile = nothing + quiet = false verbose = false debug = false inplace = false @@ -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) 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) 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) 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")