use std::borrow::Cow;
use crate::{
grid::config::{Entity, Position},
grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
settings::{CellOption, TableOption},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Charset;
impl Charset {
pub fn clean() -> CleanCharset {
CleanCharset
}
}
#[derive(Debug, Default, Clone)]
pub struct CleanCharset;
impl CleanCharset {
pub fn clean(s: &str) -> Cow<'_, str> {
Cow::Owned(clean_charset(s))
}
}
impl<R, D, C> TableOption<R, C, D> for CleanCharset
where
R: Records + ExactRecords + RecordsMut<String> + PeekableRecords,
{
fn change(self, records: &mut R, _: &mut C, _: &mut D) {
for row in 0..records.count_rows() {
for col in 0..records.count_columns() {
let pos = Position::new(row, col);
let text = records.get_text(pos);
let text = clean_charset(text);
records.set(pos, text);
}
}
}
}
impl<R, C> CellOption<R, C> for CleanCharset
where
R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
{
fn change(self, records: &mut R, _: &mut C, entity: Entity) {
let count_rows = records.count_rows();
let count_cols = records.count_columns();
for pos in entity.iter(count_rows, count_cols) {
let text = records.get_text(pos);
let text = clean_charset(text);
records.set(pos, text);
}
}
}
fn clean_charset(text: &str) -> String {
text.replace(|c| c != '\n' && c < ' ', "")
}