66
77main (Args ) ->
88 case getopt :parse (cli_options (), Args ) of
9- {ok , {Opts , _Other }} ->
9+ {ok , {Opts , Other }} ->
1010 Dump = proplists :get_value (dump , Opts ),
1111 Indent = proplists :get_value (indent , Opts ),
1212 if Dump =:= undefined , Indent =:= undefined ->
1313 start_server (Opts );
1414 is_list (Indent ) ->
15- indent (Indent );
15+ indent ([ Indent | Other ], proplists : get_value ( verbose , Opts , 0 ) );
1616 is_list (Dump ) ->
1717 dump_file (Dump )
1818 end ;
@@ -24,9 +24,9 @@ main(Args) ->
2424cli_options () ->
2525 [
2626 {dump , $d , " dump" , string , " Dump sourcer db for file" },
27- {indent , $i , " indent" , string , " Indent file and exit" },
2827 {port , $p , " port" , integer , " LSP server port" },
29- {verbose , $v , " verbose" , integer , " Verbosity level" }
28+ {verbose , $v , " verbose" , integer , " Verbosity level" },
29+ {indent , $i , " indent" , string , " Indent file(s) and exit" }
3030 ].
3131
3232start_server (Opts ) ->
@@ -64,19 +64,28 @@ dump_file(File) ->
6464 io :format (" Analyze: ~.6w ms~n " , [AT div 1000 ]),
6565 ok .
6666
67- indent (File ) ->
68- case file :read_file (File ) of
69- {ok , BinSrc } ->
70- Enc = encoding (BinSrc ),
71- Src = unicode :characters_to_list (BinSrc , Enc ),
72- {ST ,Indented } = timer :tc (fun () -> sourcer_indent :lines (Src ) end ),
73- ok = file :write_file (File , unicode :characters_to_binary (Indented , utf8 , Enc )),
74- io :format (" Indent: ~.6w ms~n " , [ST div 1000 ]),
75- ok ;
76- {error , Error } ->
77- io :format (" Could not read file: ~s \n Reason: ~p~n " ,[File , Error ]),
67+ indent ([File |Files ], Verbose ) ->
68+ try case file :read_file (File ) of
69+ {ok , BinSrc } ->
70+ Enc = encoding (BinSrc ),
71+ Src = unicode :characters_to_list (BinSrc , Enc ),
72+ {ST ,Indented } = timer :tc (fun () -> sourcer_indent :lines (Src ) end ),
73+ ok = file :write_file (File , unicode :characters_to_binary (Indented , utf8 , Enc )),
74+ Verbose > 0 andalso io :format (" Indent: ~.6w ms ~s~n " , [ST div 1000 , File ]),
75+ indent (Files , Verbose );
76+ {error , Error } ->
77+ Str = io_lib :format (" Could not read file: ~ts \n Reason: ~p~n " , [File , Error ]),
78+ throw ({error ,Str })
79+ end
80+ catch throw :{error , Desc } ->
81+ io :format (" ~ts " , [Desc ]),
82+ erlang :halt (1 );
83+ error :_What ->
84+ io :format (" Error could not indent file: ~ts \n " , [File ]),
7885 erlang :halt (1 )
79- end .
86+ end ;
87+ indent ([], _ ) ->
88+ ok .
8089
8190encoding (Bin ) ->
8291 case epp :read_encoding_from_binary (Bin ) of
0 commit comments