mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-06 20:42:10 +00:00
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:
parent
b41bdb548a
commit
c5046235b5
2 changed files with 11 additions and 6 deletions
|
@ -15,6 +15,7 @@
|
||||||
use std::borrow::BorrowMut;
|
use std::borrow::BorrowMut;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{Error, Write};
|
use std::io::{Error, Write};
|
||||||
|
use std::ops::Range;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{fmt, io, mem};
|
use std::{fmt, io, mem};
|
||||||
|
|
||||||
|
@ -488,18 +489,20 @@ impl FormatRecorder {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replay(&self, formatter: &mut dyn Formatter) -> io::Result<()> {
|
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(
|
pub fn replay_with(
|
||||||
&self,
|
&self,
|
||||||
formatter: &mut dyn Formatter,
|
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<()> {
|
) -> io::Result<()> {
|
||||||
let mut last_pos = 0;
|
let mut last_pos = 0;
|
||||||
let mut flush_data = |formatter: &mut dyn Formatter, pos| -> io::Result<()> {
|
let mut flush_data = |formatter: &mut dyn Formatter, pos| -> io::Result<()> {
|
||||||
if last_pos != pos {
|
if last_pos != pos {
|
||||||
write_data(formatter, &self.data[last_pos..pos])?;
|
write_data(formatter, last_pos..pos)?;
|
||||||
last_pos = pos;
|
last_pos = pos;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -953,7 +956,8 @@ mod tests {
|
||||||
let mut output: Vec<u8> = vec![];
|
let mut output: Vec<u8> = vec![];
|
||||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||||
recorder
|
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())
|
write!(formatter, "<<{}>>", str::from_utf8(data).unwrap())
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -30,9 +30,10 @@ pub fn write_indented(
|
||||||
recorded_content: &FormatRecorder,
|
recorded_content: &FormatRecorder,
|
||||||
mut write_prefix: impl FnMut(&mut dyn Formatter) -> io::Result<()>,
|
mut write_prefix: impl FnMut(&mut dyn Formatter) -> io::Result<()>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
|
let data = recorded_content.data();
|
||||||
let mut new_line = true;
|
let mut new_line = true;
|
||||||
recorded_content.replay_with(formatter, |formatter, data| {
|
recorded_content.replay_with(formatter, |formatter, range| {
|
||||||
for line in data.split_inclusive(|&c| c == b'\n') {
|
for line in data[range].split_inclusive(|&c| c == b'\n') {
|
||||||
if new_line && line != b"\n" {
|
if new_line && line != b"\n" {
|
||||||
// Prefix inherits the current labels. This is implementation detail
|
// Prefix inherits the current labels. This is implementation detail
|
||||||
// and may be fixed later.
|
// and may be fixed later.
|
||||||
|
|
Loading…
Reference in a new issue