From 3ae7113a61b03671b69adb1faac1bf61aea4b917 Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Sat, 8 Sep 2018 14:10:43 +0800 Subject: [PATCH 1/3] add support format buffer from stdin --- commandline.lua | 64 ++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/commandline.lua b/commandline.lua index 2587eb0..21095e3 100755 --- a/commandline.lua +++ b/commandline.lua @@ -6,6 +6,7 @@ if _VERSION ~= 'Lua 5.1' then end local help = [[Formats Lua code. + -i, --stdin Read buffer from stdin. -a, --autosave Flush formatted Lua in given file instead of stdout. -s, --spaces (default 2) Spaces to use as indentation. -t, --tabs (default 0) Tabulation(s) to use as indentation. @@ -28,7 +29,7 @@ end -- -- Check arguments -- -if #args == 0 then +if not args.stdin and #args == 0 then print 'No files to format.' return elseif not (args.spaces or args.tabs) then @@ -64,32 +65,47 @@ end -- Output formatted file -- local formatter = require 'formatter' -for _, filename in ipairs(args) do - -- - -- Reading file - -- - local file, err = io.open(filename, 'r') - if not file then print( err ) return end - local code, err = file:read('*a') - if not code then print( err ) return end - file:close() +-- Read from stdin +if args.stdin then + code = io.read("*a") + -- Format source + local formatted, errormessage = formatter.indentcode(code, delimiter, true, + indentation) - -- Format source - local formatted, errormessage = formatter.indentcode(code, delimiter, true, - indentation) - if formatted then - if args.autosave then - -- Saving formatted code straight in original file - local file, err = io.open(filename, 'w+') - if not file then print( err ) return end - local code, err = file:write(formatted) - if not code then print( err ) return end - file:close() + if formatted then + print( string.format('%s\n', formatted) ) + else + print(string.format('Unable to format stdin:\n%s', errormessage)) + end +else + for _, filename in ipairs(args) do + + -- + -- Reading file + -- + local file, err = io.open(filename, 'r') + if not file then print( err ) return end + local code, err = file:read('*a') + if not code then print( err ) return end + file:close() + + -- Format source + local formatted, errormessage = formatter.indentcode(code, delimiter, true, + indentation) + if formatted then + if args.autosave then + -- Saving formatted code straight in original file + local file, err = io.open(filename, 'w+') + if not file then print( err ) return end + local code, err = file:write(formatted) + if not code then print( err ) return end + file:close() + else + io.write(formatted) + end else - io.write(formatted) + print(string.format('Unable to format `%s`:\n%s', filename, errormessage)) end - else - print(string.format('Unable to format `%s`:\n%s', filename, errormessage)) end end From 5343e66db2960cb94379c44cb12bafc3b5284268 Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Sat, 8 Sep 2018 14:17:28 +0800 Subject: [PATCH 2/3] remove \n --- commandline.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commandline.lua b/commandline.lua index 21095e3..786ddd3 100755 --- a/commandline.lua +++ b/commandline.lua @@ -74,7 +74,7 @@ if args.stdin then indentation) if formatted then - print( string.format('%s\n', formatted) ) + print( formatted ) else print(string.format('Unable to format stdin:\n%s', errormessage)) end From 05045ea215d8b81a9777823eadafd480aaec0f7e Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Sat, 8 Sep 2018 14:19:42 +0800 Subject: [PATCH 3/3] reformat --- commandline.lua | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/commandline.lua b/commandline.lua index 786ddd3..7400d26 100755 --- a/commandline.lua +++ b/commandline.lua @@ -69,15 +69,14 @@ local formatter = require 'formatter' -- Read from stdin if args.stdin then code = io.read("*a") - -- Format source - local formatted, errormessage = formatter.indentcode(code, delimiter, true, - indentation) - - if formatted then - print( formatted ) - else - print(string.format('Unable to format stdin:\n%s', errormessage)) - end + -- Format source + local formatted, errormessage = formatter.indentcode(code, delimiter, true, + indentation) + if formatted then + print( formatted ) + else + print(string.format('Unable to format stdin:\n%s', errormessage)) + end else for _, filename in ipairs(args) do