Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for unicode in riak-shell #68

Merged
merged 1 commit into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/history_EXT.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ show_history(Cmd, #state{history = Hist} = S) ->
Msg1 = "The history contains:\n",
FormatFn = fun({N, Cmd1}) ->
Cmd2 = riak_shell_util:pretty_pr_cmd(Cmd1),
{N, io_lib:format("~s", [Cmd2])}
{N, io_lib:format("~ts", [Cmd2])}
end,
Hist2 = [FormatFn(X) || X <- Hist],
Msg2 = riak_shell_util:print_key_vals(lists:reverse(Hist2)),
Expand Down
20 changes: 17 additions & 3 deletions src/riak_shell_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ to_list(F) when is_float(F) -> mochinum:digits(F);
to_list(L) when is_list(L) -> L.

pretty_pr_cmd(Cmd) ->
Cmd2 = re:replace(Cmd, "\n", " ", [global, {return, list}]),
Cmd3 = re:replace(Cmd2, "[ ]+", " ", [global, {return, list}]),
_Cmd4 = re:replace(Cmd3, "^ ", "", [{return, list}]).
%% complex list-to-binary unicode dance to make
%% regexs work with unicode input
CmdBin = unicode:characters_to_binary(Cmd),
{ok, Regex1} = re:compile("(\n|[ ]+)", [unicode]),
CmdBin2 = re:replace(CmdBin, Regex1, " ", [global, {return, binary}]),
{ok, Regex2} = re:compile("^ ", [unicode]),
_Cmd2 = re:replace(CmdBin2, Regex2, "", [{return, list}]).

datetime() ->
{{Y, M, D}, {H, Mn, S}} = calendar:universal_time(),
Expand All @@ -106,3 +110,13 @@ datetime() ->

pad(X) when is_integer(X) ->
io_lib:format("~2.10.0B", [X]).

-ifdef(TEST).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests! Huzzah!

-include_lib("eunit/include/eunit.hrl").

pretty_pr_with_unicode_test() ->
%% 8217 is smart quotes
Input = [8217, 65, 8217],
?assertEqual(Input, pretty_pr_cmd(Input)).

-endif.