ingydotnet / yaml-libyaml-pm

Perl Binding to libyaml
http://search.cpan.org/dist/YAML-LibYAML/
33 stars 37 forks source link

Make indent spaces configurable #88

Closed zdm closed 5 years ago

zdm commented 5 years ago

Hi. Currently it uses 2 spaces to indenting lines when generating yaml. But it is hard to read, when yaml is large and has complex nested structure. Is it possible to make this constant configurable from perl code? My yaml looks much better with 4-spaces indentation.

zdm commented 5 years ago

You can insert this code to the "LibYAML/perl_libyaml.c", line 722, just after "yaml_emitter_initialize(&dumper.emitter);"

    /* set indent */
    SV* indent = get_sv("YAML::Indent", GV_ADD);
    if (SvIOK(indent)) yaml_emitter_set_indent(&dumper.emitter, SvIV(indent));

It will take value from $YAML::Indent or will use default (2 spaces).

perlpunk commented 5 years ago

Thanks! The plan was to avoid more global variables and at some point use a better API, but that's still not happening in the near future. @ingydotnet thoughts?

perlpunk commented 5 years ago

To be in sync with YAML.pm, it should be $YAML::XS::Indent, though

zdm commented 5 years ago

Yes, I used YAML::XS::IndentSpaces, but you can rename it to YAML::XS::Indent and insert to YAML.pm.

zdm commented 5 years ago

Oh, yes, you want to make it compatible with pure-perl YAMPL.pm, just rename var in code sample above to the YAML::Indent,

zdm commented 5 years ago

I fixed the code above and performed testing. Now you can just insert it to the source and it will works.

perlpunk commented 5 years ago

@ingydotnet has to decide...

ingydotnet commented 5 years ago

I am ok with this but I think that in general we look at config vars in YAML::XS by looking at $YAML::XS::Indent and then $YAML::Indent. That way people can switch between YAML implementations and not need to change their settings.

Please confirm or deny that this is the way that config variables work for YAML::XS.

zdm commented 5 years ago

I will create pull request, where indent value will $YAML::XS::Indent // $YAML::Indent // 2.

perlpunk commented 5 years ago

@ingydotnet YAML::XS is currently only looking at variables in YAML::XS namespace

perlpunk commented 5 years ago

fixed by #89