hecto/src/row.rs
2021-03-10 16:21:07 -05:00

129 lines
2.9 KiB
Rust

use std::cmp;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Default)]
pub struct Row {
string: String,
len: usize,
}
impl From<&str> for Row {
fn from(slice: &str) -> Self {
Self {
string: String::from(slice),
len: slice.graphemes(true).count(),
}
}
}
impl Row {
pub fn render(&self, start: usize, end: usize) -> String {
let end = cmp::min(end, self.string.len());
let start = cmp::min(start, end);
let mut result = String::new();
#[allow(clippy::integer_arithmetic)]
for grapheme in self.string[..]
.graphemes(true)
.skip(start)
.take(end - start)
{
if grapheme == "\t" {
result.push_str(" ");
} else {
result.push_str(grapheme);
}
}
result
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn insert(&mut self, at: usize, c: char) {
if at >= self.len() {
self.string.push(c);
self.len += 1;
return;
}
let mut result = String::new();
let mut length = 0;
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
length += 1;
if index == at {
length += 1;
result.push(c);
}
result.push_str(grapheme);
}
self.len = length;
self.string = result;
}
#[allow(clippy::integer_arithmetic)]
pub fn delete(&mut self, at: usize) {
if at >= self.len() {
return
}
let mut result = String::new();
let mut length = 0;
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
if index != at {
length += 1;
result.push_str(grapheme);
}
}
self.len = length;
self.string = result;
}
pub fn append(&mut self, new: &Self) {
self.string = format!("{}{}", self.string, new.string);
self.len += new.len;
}
pub fn split(&mut self, at: usize) -> Self {
let mut row = String::new();
let mut length = 0;
let mut splitted_row = String::new();
let mut splittend_length = 0;
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
if index < at {
length += 1;
row.push_str(grapheme);
} else {
splittend_length += 1;
splitted_row.push_str(grapheme);
}
}
self.string = row;
self.len = length;
Self {
string: splitted_row,
len: splittend_length,
}
}
pub fn as_bytes(&self) -> &[u8] {
self.string.as_bytes()
}
}