use std::io::{self, Write};
use crate::jobs::{CancelToken, Progress};
use crate::lines::LineReader;
use crate::text::rows::for_each_cell;
use crate::text::TextBuf;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ColumnGen {
Numbers { start: i64, step: i64, width: usize, hex: bool },
Text(String),
}
impl ColumnGen {
pub fn value(&self, i: u64) -> String {
match self {
ColumnGen::Numbers { start, step, width, hex } => {
let v = start.saturating_add(step.saturating_mul(i as i64));
let digits = if *hex { format!("{:x}", v.unsigned_abs()) } else { v.unsigned_abs().to_string() };
format!("{}{digits:0>width$}", if v < 0 { "-" } else { "" })
}
ColumnGen::Text(t) => t.clone(),
}
}
}
pub fn cell_offset(codec: &crate::encoding::Codec, line: &[u8], base: u64, col: u32) -> usize {
let mut result = line.len();
for_each_cell(codec, line, base, |c| {
if c.width == 0 {
return true;
}
if col <= c.cell {
result = (c.off - base) as usize;
return false;
}
if col < c.cell + c.width {
let at = (c.off - base) as usize;
result = if col - c.cell <= c.width / 2 { at } else { at + c.len as usize };
return false;
}
true
});
result
}
pub fn column_edit_to(buf: &TextBuf, from: u64, cells: (u32, u32), gen: &ColumnGen, out: &mut dyn Write, cancel: &CancelToken, progress: Option<&Progress>) -> io::Result<Option<u64>> {
let codec = buf.codec;
let len = buf.len();
let read_err = || io::Error::other("Could not read the document");
if let Some(p) = progress {
p.set_total(len);
}
let from = from.min(len);
let mut err = None;
buf.for_each_chunk(0..from, |_, b| match out.write_all(b) {
Ok(()) => true,
Err(e) => {
err = Some(e);
false
}
})
.ok_or_else(read_err)?;
if let Some(e) = err {
return Err(e);
}
let mut r = LineReader::new(buf);
let mut n = 0u64;
loop {
let before = r.taken();
let Some((off, line)) = r.next()?.map(|(o, l)| (o, l.to_vec())) else { break };
let total = r.taken() - before;
if off < from {
continue;
}
let lo = cell_offset(&codec, &line, off, cells.0);
let hi = cell_offset(&codec, &line, off, cells.1).max(lo);
out.write_all(&line[..lo])?;
out.write_all(&codec.encode(&gen.value(n)))?;
out.write_all(&line[hi..])?;
let brk = buf.read(off + line.len() as u64..off + total).ok_or_else(read_err)?;
out.write_all(&brk)?;
n += 1;
if n.is_multiple_of(4096) {
if cancel.is_cancelled() {
return Ok(None);
}
if let Some(p) = progress {
p.set_done(off);
}
}
}
Ok(Some(n))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::encoding::Codec;
use crate::file::{OpenMode, Source};
use crate::index::LineIndex;
use std::sync::Arc;
#[test]
fn values() {
let n = |start, step, width, hex| ColumnGen::Numbers { start, step, width, hex };
assert_eq!((0..3).map(|i| n(1, 1, 3, false).value(i)).collect::<Vec<_>>(), ["001", "002", "003"]);
assert_eq!(n(-2, 1, 2, false).value(0), "-02");
assert_eq!(n(250, 5, 0, true).value(2), "104");
assert_eq!(ColumnGen::Text("| ".into()).value(9), "| ");
}
#[test]
fn down_to_the_end_with_tabs_short_lines_and_crlf() {
let text = "keep\r\nab\tcd\r\nx\r\nlong line\r\nlast";
let mut f = tempfile::NamedTempFile::new().unwrap();
std::io::Write::write_all(&mut f, text.as_bytes()).unwrap();
let s = Arc::new(Source::open(f.path(), OpenMode::Auto).unwrap());
let buf = TextBuf::new(s.clone(), LineIndex::new(s.len()), Codec::utf8());
let mut out = Vec::new();
let gen = ColumnGen::Numbers { start: 1, step: 1, width: 1, hex: false };
let n = column_edit_to(&buf, 6, (2, 4), &gen, &mut out, &CancelToken::new(), None).unwrap().unwrap();
assert_eq!(n, 4);
assert_eq!(String::from_utf8(out).unwrap(), "keep\r\nab1cd\r\nx2\r\nlo3 line\r\nla4");
}
}