Skip to content

Commit

Permalink
[#209] add output-format option to show result without colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Euen committed May 19, 2015
1 parent 12221d4 commit 7330c94
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ environment values in your [configuration][config] file:
}
]
},
%% Optional to select the output format, the default is colors
{output_format, plain},
%% Only necessary for the 'webhook' functionality
{github_user, "user"},
{github_password, "password"}
Expand Down
3 changes: 2 additions & 1 deletion config/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
rules => [{elvis_project, old_configuration_format}]
}
]
}
},
{output_format, plain}
]
}
].
7 changes: 7 additions & 0 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,14 @@ option_spec_list() ->
Commands = "Provide the path to the configuration file. "
++ "When none is provided elvis checks if there's "
++ "an elvis.config file.",
OutputFormat = "It allows you to display the results in plain text. When "
++ "none is provided elvis displays the results in colors. "
++ "The options allowed are (plain | colors).",
[
{help, $h, "help", undefined, "Show this help information."},
{config, $c, "config", string, Commands},
{commands, undefined, "commands", undefined, "Show available commands."},
{output_format, undefined, "output-format", string, OutputFormat},
{code_path, $p, "code-path", string, "Add the directory in the code path."}
].

Expand All @@ -227,6 +231,9 @@ process_options([{config, Path} | Opts], Cmds, _) ->
process_options([commands | Opts], Cmds, Config) ->
commands(),
process_options(Opts, Cmds, Config);
process_options([{output_format, Format} | Opts], Cmds, Config) ->
ok = application:set_env(elvis, output_format, list_to_atom(Format)),
process_options(Opts, Cmds, Config);
process_options([{code_path, Path} | Opts], Cmds, Config) ->
code:add_path(Path),
process_options(Opts, Cmds, Config);
Expand Down
17 changes: 11 additions & 6 deletions src/elvis_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,17 @@ parse_colors(Message) ->
"white-bold" => "\e[1;37m",
"reset" => "\e[0m"},
Opts = [global, {return, list}],
Fun = fun(Key, Acc) ->
Regex = ["{{", Key, "}}"],
Color = maps:get(Key, Colors),
re:replace(Acc, Regex, Color, Opts)
end,
lists:foldl(Fun, Message, maps:keys(Colors)).
case application:get_env(elvis, output_format, colors) of
plain ->
re:replace(Message, "{{.*?}}", "", Opts);
colors ->
Fun = fun(Key, Acc) ->
Regex = ["{{", Key, "}}"],
Color = maps:get(Key, Colors),
re:replace(Acc, Regex, Color, Opts)
end,
lists:foldl(Fun, Message, maps:keys(Colors))
end.

-spec escape_format_str(string()) -> string().
escape_format_str(String) ->
Expand Down
13 changes: 13 additions & 0 deletions test/elvis_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
rock_with_file_config/1,
rock_with_old_config/1,
rock_this/1,
rock_without_colors/1,
%% Webhook
run_webhook/1,
run_webhook_ping/1,
Expand Down Expand Up @@ -171,6 +172,18 @@ rock_this(_Config) ->

ok.

-spec rock_without_colors(config()) -> ok.
rock_without_colors(_Config) ->
ConfigPath = "../../config/test.config",
ElvisConfig = elvis_config:load_file(ConfigPath),
Fun = fun() -> elvis:rock(ElvisConfig) end,
Expected = "\\e.*?m",
ok = try check_some_line_output(Fun, Expected, fun matches_regex/2) of
Result -> ct:fail("Unexpected result ~p", [Result])
catch
_:{badmatch, []} -> ok
end.

%%%%%%%%%%%%%%%
%%% Webhook

Expand Down

0 comments on commit 7330c94

Please sign in to comment.