Skip to content

when print record format by lager:pr which has binary value, need space after '=' #554

@yidayoung

Description

@yidayoung

when print a record like #rec{field =<<"">>}, it will trans to string "#rec{field=<<"">>}"
every thing works fine, but when paste this string to shell will cause syntax error before: '=<'
because need space after '=' like this "#rec{field= <<"">>}"

so, can we change this format here

{FieldStr, FieldLen} = print(Field, Max - ExtraChars, Options),
{ValueStr, ValueLen} = print(Value, Max - (FieldLen + ExtraChars), Options),
{Final, FLen} = record_fields(T, Max - (FieldLen + ValueLen + ExtraChars), dec_depth(Options)),
{[FieldStr++"="++ValueStr++Terminator|Final], FLen + FieldLen + ValueLen + ExtraChars}.


one way is change binary print function

print(<<>>, _Max, #print_options{depth=1}) ->
{"<<>>", 4};
print(Bin, _Max, #print_options{depth=1}) when is_binary(Bin) ->
{"<<...>>", 7};
print(<<>>, _Max, Options) ->
case Options#print_options.force_strings of
true ->
{"", 0};
false ->
{"<<>>", 4}
end;
print(Binary, 0, _Options) when is_bitstring(Binary) ->
{"<<..>>", 6};
print(Bin, Max, _Options) when is_binary(Bin), Max < 2 ->
{"<<...>>", 7};
print(Binary, Max, Options) when is_binary(Binary) ->
B = binary_to_list(Binary, 1, lists:min([Max, byte_size(Binary)])),

but this may cause some other behavior changed

or just add ensure_space after print function, like this

    {FieldStr, FieldLen} = print(Field, Max - ExtraChars, Options),
    {ValueStr, ValueLen} = print(Value, Max - (FieldLen + ExtraChars), Options),
    {ValueStr2, ValueLen2} = ensure_space(Value, ValueStr, ValueLen),
    {Final, FLen} = record_fields(T, Max - (FieldLen + ValueLen + ExtraChars), dec_depth(Options)),
    {[FieldStr++"="++ValueStr2++Terminator|Final], FLen + FieldLen + ValueLen2 + ExtraChars}.

ensure_space(Value, ValueStr, ValueLen) when is_binary(Value) ->
    {" "+ValueStr, ValueLen+1};
ensure_space(_Value, ValueStr, ValueLen) ->
    {ValueStr, ValueLen}.

if add ensure_space is enough, i can make a pr for this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions