malvim mostly working, ish

This commit is contained in:
stjet
2024-10-19 05:15:54 +00:00
parent 4311b424c8
commit 4a972783a8
4 changed files with 213 additions and 27 deletions

View File

@@ -1,10 +1,39 @@
pub trait Substring {
fn substring(&self, start: usize, end: usize) -> &str;
fn remove(&self, index: usize, len: usize) -> String;
}
impl Substring for String {
fn substring(&self, start: usize, end: usize) -> &str {
let mut byte_start = 0;
let mut byte_end = 0;
let mut chars = self.chars();
for i in 0..end {
let char_length = chars.next().unwrap().len_utf8();
if i < start {
byte_start += char_length;
}
if i == end {
break;
}
byte_end += char_length;
}
&self[byte_start..byte_end]
}
fn remove(&self, index: usize, len: usize) -> String {
self.substring(0, index).to_string() + self.substring(index + len, self.len())
}
}
//the tuple is first, line #, actual line
pub fn calc_actual_lines<'a>(lines: impl Iterator<Item = &'a String>, max_chars_per_line: usize) -> Vec<(bool, usize, String)> {
pub fn calc_actual_lines<'a>(lines: impl Iterator<Item = &'a String>, max_chars_per_line: usize, one_extra: bool) -> Vec<(bool, usize, String)> {
let mut actual_lines = Vec::new();
let mut line_num = 0;
for real_line in lines {
let mut line = real_line.to_string();
let mut line = real_line.to_string() + if one_extra { " " } else { "" };
let mut first = true;
loop {
if line.chars().count() <= max_chars_per_line {
@@ -26,3 +55,16 @@ pub fn calc_actual_lines<'a>(lines: impl Iterator<Item = &'a String>, max_chars_
actual_lines
}
pub fn calc_new_cursor_pos(cursor_pos: usize, new_length: usize) -> usize {
if cursor_pos >= new_length {
if new_length == 0 {
0
} else {
new_length - 1
}
} else {
cursor_pos
}
}