Skip to content

Commit

Permalink
Merge pull request #99 from inaka/jfacorro.86.improve.command.line.fe…
Browse files Browse the repository at this point in the history
…edback

[Closes #86] Improve command line feedback.
  • Loading branch information
elbrujohalcon committed Sep 8, 2014
2 parents b86af86 + 5b593e2 commit 4079ff5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ rock() ->

-spec rock(elvis_config:config()) -> ok | {fail, elvis_result:file()}.
rock(Config = #{files := Files, rules := _Rules}) ->
Fun = fun (File) -> elvis_utils:load_file_data(Config, File) end,
elvis_utils:info("Loading files...~n"),
Fun = fun (File) ->
Path = elvis_utils:path(File),
elvis_utils:info("Loading ~s~n", [Path]),
elvis_utils:load_file_data(Config, File)
end,
LoadedFiles = lists:map(Fun, Files),

elvis_utils:info("Applying rules...~n"),
Results = [apply_rules(Config, File) || File <- LoadedFiles],

elvis_result:print(Results),
case elvis_result:status(Results) of
fail -> {fail, Results};
ok -> ok
Expand Down Expand Up @@ -102,7 +108,9 @@ apply_rules(Config = #{rules := Rules}, File) ->
Acc = {[], Config, File},
{RulesResults, _, _} = lists:foldl(fun apply_rule/2, Acc, Rules),

elvis_result:new(file, File, RulesResults).
Results = elvis_result:new(file, File, RulesResults),
elvis_result:print(Results),
Results.

apply_rule({Module, Function, Args}, {Result, Config, File}) ->
Results = Module:Function(Config, File, Args),
Expand Down
6 changes: 3 additions & 3 deletions src/elvis_result.erl
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ print(#{file := File, rules := Rules}) ->
fail -> "FAIL"
end,

io:format("# ~s [~s]~n", [Path, Status]),
elvis_utils:info("# ~s [~s]~n", [Path, Status]),
print(Rules);

print(#{items := []}) ->
ok;
print(#{name := Name, items := Items}) ->
io:format(" - ~s~n", [atom_to_list(Name)]),
elvis_utils:info(" - ~s~n", [atom_to_list(Name)]),
print(Items);

print(#{message := Msg, info := Info}) ->
io:format(" - " ++ Msg ++ "~n", Info).
elvis_utils:info(" - " ++ Msg ++ "~n", Info).

-spec status([rule()]) -> ok | fail.
status([]) ->
Expand Down
25 changes: 24 additions & 1 deletion src/elvis_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

-export([
src/1,
path/1,
parse_tree/2,
load_file_data/2,

Expand All @@ -20,7 +21,11 @@
%% General
erlang_halt/1,
to_str/1,
maps_get/3
maps_get/3,

%% Output
info/1,
info/2
]).

-export_type([file/0]).
Expand Down Expand Up @@ -48,6 +53,13 @@ src(File = #{path := Path}) ->
src(File) ->
throw({invalid_file, File}).

%% @doc Given a file() returns its path.
-spec path(file()) -> string().
path(#{path := Path}) ->
Path;
path(File) ->
throw({invalid_file, File}).

%% @doc Add the root node of the parse tree to the file data.
-spec parse_tree(elvis_config:config(), file()) ->
{elvis_code:tree_node(), file()}.
Expand Down Expand Up @@ -191,3 +203,14 @@ maps_get(Key, Map, Default) ->
true -> maps:get(Key, Map);
false -> Default
end.

-spec info(string()) -> ok.
info(Message) ->
info(Message, []).

-spec info(string(), [term()]) -> ok.
info(Message, Args) ->
case application:get_env(elvis, no_output) of
{ok, true} -> ok;
_ -> io:format(Message, Args)
end.

0 comments on commit 4079ff5

Please sign in to comment.