psych icon indicating copy to clipboard operation
psych copied to clipboard

Default line_width does not reflect the libyaml default of 80

Open glebm opened this issue 8 years ago • 0 comments

This causes the folded style to not apply unless line_width: is passed explicitly:

> s = 'asjfal sf;lkasfj;aslkfaj slfkasfkajf a;slkfjaslkfaslkfj aslkfjaslkf asklfj askl fjaklsfj aslkfjaslfjaslkfjlkffjsa'
> Psych.dump(s, line_width: 80) == Psych.dump(s)
 => false
> puts Psych.dump(s, line_width: 80)
--- >-
  asjfal sf;lkasfj;aslkfaj slfkasfkajf a;slkfjaslkfaslkfj aslkfjaslkf asklfj askl
  fjaklsfj aslkfjaslfjaslkfjlkffjsa
> puts Psych.dump(s)
--- asjfal sf;lkasfj;aslkfaj slfkasfkajf a;slkfjaslkfaslkfj aslkfjaslkf asklfj askl
  fjaklsfj aslkfjaslfjaslkfjlkffjsa
...

The libyaml default of 80 happens as follows:

First, the emitter best_width is set to 0 on initialization here:

memset(emitter, 0, sizeof(yaml_emitter_t));

It is then set to 80 in yaml_emitter_emit_stream_start here:

if (emitter->best_width >= 0
        && emitter->best_width <= emitter->best_indent*2) {
    emitter->best_width = 80;
}

On the Ruby side, we pass the options including :line_width into the emitter, but the code that picks the node style does not account for the libyaml defaults:

elsif @line_width && o.length > @line_width
    style = Nodes::Scalar::FOLDED

We should probably also default to 80 in yaml_tree.rb for uniform handling.

glebm avatar Nov 14 '15 10:11 glebm