formatter: pass data range to replay callback instead of data slice

New word-wrap function will be implemented in two passes. The first pass
splits byte slice to lines, and the second pass inserts "\n" based on that
while interleaving push/pop_label() calls and text fragments. Since the second
pass combines multiple data sources, byte indices are more convenient than
slices there.
This commit is contained in:
Yuya Nishihara 2023-03-05 12:33:40 +09:00
parent b41bdb548a
commit c5046235b5
2 changed files with 11 additions and 6 deletions

View file

@ -15,6 +15,7 @@
use std::borrow::BorrowMut;
use std::collections::HashMap;
use std::io::{Error, Write};
use std::ops::Range;
use std::sync::Arc;
use std::{fmt, io, mem};
@ -488,18 +489,20 @@ impl FormatRecorder {
}
pub fn replay(&self, formatter: &mut dyn Formatter) -> io::Result<()> {
self.replay_with(formatter, |formatter, data| formatter.write_all(data))
self.replay_with(formatter, |formatter, range| {
formatter.write_all(&self.data[range])
})
}
pub fn replay_with(
&self,
formatter: &mut dyn Formatter,
mut write_data: impl FnMut(&mut dyn Formatter, &[u8]) -> io::Result<()>,
mut write_data: impl FnMut(&mut dyn Formatter, Range<usize>) -> io::Result<()>,
) -> io::Result<()> {
let mut last_pos = 0;
let mut flush_data = |formatter: &mut dyn Formatter, pos| -> io::Result<()> {
if last_pos != pos {
write_data(formatter, &self.data[last_pos..pos])?;
write_data(formatter, last_pos..pos)?;
last_pos = pos;
}
Ok(())
@ -953,7 +956,8 @@ mod tests {
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
recorder
.replay_with(&mut formatter, |formatter, data| {
.replay_with(&mut formatter, |formatter, range| {
let data = &recorder.data()[range];
write!(formatter, "<<{}>>", str::from_utf8(data).unwrap())
})
.unwrap();

View file

@ -30,9 +30,10 @@ pub fn write_indented(
recorded_content: &FormatRecorder,
mut write_prefix: impl FnMut(&mut dyn Formatter) -> io::Result<()>,
) -> io::Result<()> {
let data = recorded_content.data();
let mut new_line = true;
recorded_content.replay_with(formatter, |formatter, data| {
for line in data.split_inclusive(|&c| c == b'\n') {
recorded_content.replay_with(formatter, |formatter, range| {
for line in data[range].split_inclusive(|&c| c == b'\n') {
if new_line && line != b"\n" {
// Prefix inherits the current labels. This is implementation detail
// and may be fixed later.