pr: write all characters in each row in a single loop - #10475
Conversation
Remove some unneeded parameters from the `get_line_for_printing()` helper function since the values are already contained in the `OutputOptions` parameter.
Write the contents of the entire row to a single String before writing the full row to stdout. Before this commit, each cell was put in its own String and then that String was written to stdout before moving on to the next cell in the row. After this commit, the contents of each cell in a row is appended to a single String, and that String is written to stdout only after all cells in the row have been processed. This change will make it much easier to make improvements to the logic of which characters to write at each point in the row, since the current logic does not match the output GNU `pr` very well. For example, the decision about whether to write a tab or multiple spaces requires knowing how many characters have been written in the current row, which was not easily accessible before.
7291b8f to
13690b7
Compare
|
GNU testsuite comparison: |
| if cell.is_none() && !merge { | ||
| out.write_all(result.as_bytes())?; | ||
| out.write_all(options.content_line_separator.as_bytes())?; | ||
| return Ok(false); |
There was a problem hiding this comment.
In the docs it says that "A bool indicating whether printing terminated early due to a" and here its false instead of true I think this is swapped
There was a problem hiding this comment.
Thanks. I'll try to cook up the missing test case to cover this.
|
|
||
| // Finally, terminate the line with the specified separator. | ||
| out.write_all(options.content_line_separator.as_bytes())?; | ||
| Ok(true) |
There was a problem hiding this comment.
Same for swapping this
| // If the cell is None, terminate the line early. | ||
| if cell.is_none() && !merge { | ||
| out.write_all(result.as_bytes())?; | ||
| out.write_all(options.content_line_separator.as_bytes())?; |
There was a problem hiding this comment.
I don't think we can determine if the content_line_seperator is written here, because that depends on the terminated_early variable used by the caller of this method since it should only be printed if "feed_line_present" is not equal to true
| result.push(' '); | ||
| } | ||
| } | ||
| result.truncate(col_start + min_width); |
There was a problem hiding this comment.
Here its truncating by bytes not by characters:
let prefix: String = result[..col_start].to_string();
let cell_truncated: String = result[col_start..].chars().take(min_width).collect();
result = prefix + &cell_truncated;
|
GNU testsuite comparison: |
Depends on #10474
Write the contents of the entire row to a single String before writing the full row to stdout. Before this commit, each cell was put in its own String and then that String was written to stdout before moving on to the next cell in the row. After this commit, the contents of each cell in a row is appended to a single String, and that String is written to stdout only after all cells in the row have been processed.
This change will make it much easier to make improvements to the logic of which characters to write at each point in the row, since the current logic does not match the output GNU
prvery well. For example, the decision about whether to write a tab or multiple spaces requires knowing how many characters have been written in the current row, which was not easily accessible before.