Skip to content

Commit

Permalink
Update docstrings, convert to String faster, fix --help
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 5, 2024
1 parent 0f12148 commit 9e00683
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mutable struct Context
end

function Context(
src_str; assert::Bool = true, debug::Bool = false, verbose::Bool = debug,
src_str::String; assert::Bool = true, debug::Bool = false, verbose::Bool = debug,
diff::Bool = false, check::Bool = false, quiet::Bool = false,
)
src_io = IOBuffer(src_str)
Expand Down Expand Up @@ -366,25 +366,32 @@ function format_tree!(ctx::Context)
end

"""
format_string(str::AbstractString) -> String
Runic.format_string(str::AbstractString) -> String
Format a string.
Format string `str` and return the formatted string.
"""
function format_string(str::AbstractString)
ctx = Context(str)
format_tree!(ctx)
return String(take!(ctx.fmt_io))
end

# TODO: Implement the check and diff options here too.
"""
format_file(inputfile::AbstractString, outputfile::AbstractString; inplace::Bool=false)
Runic.format_file(
inputfile::AbstractString, outputfile::AbstractString = inputfile;
inplace::Bool=false,
)
Format file `inputfile` and write the formatted text to `outputfile`.
Format a file.
Setting the keyword argument `inplace = true` is required if `inputfile` and `outputfile`
are the same file.
"""
function format_file(inputfile::AbstractString, outputfile::AbstractString = inputfile; inplace::Bool = false)
# Argument handling
inputfile = normpath(abspath(inputfile))
outputfile = normpath(abspath(outputfile))
inputfile = normpath(abspath(String(inputfile)))
outputfile = normpath(abspath(String(outputfile)))
str = read(inputfile, String)
if !inplace && (outputfile == inputfile || (isfile(outputfile) && samefile(inputfile, outputfile)))
error("input and output must not be the same when `inplace = false`")
Expand Down
3 changes: 3 additions & 0 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ function main(argv)
x = popfirst!(argv)
if x == "-i" || x == "--inplace"
inplace = true
elseif x == "--help"
print_help()
return errno
elseif x == "-q" || x == "--quiet"
quiet = true
elseif x == "-v" || x == "--verbose"
Expand Down

0 comments on commit 9e00683

Please sign in to comment.