summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2023-05-24 13:42:14 -0400
committerDan Davison <dandavison7@gmail.com>2023-05-24 13:50:56 -0400
commitd92c3ead769326461ea082632e3aa15ca7700d4e (patch)
tree540a3fac7c10ffb43b13a4d9c8e587c291e4e921
parentfeec45b5d724f3feb29b59e48f5335513e00a3b0 (diff)
Clean up
-rw-r--r--src/align.rs117
-rw-r--r--src/ansi/mod.rs4
2 files changed, 53 insertions, 68 deletions
diff --git a/src/align.rs b/src/align.rs
index edab1954..7ef40f64 100644
--- a/src/align.rs
+++ b/src/align.rs
@@ -142,74 +142,10 @@ impl<'a> Alignment<'a> {
run_length_encode(self.operations())
}
- /// Compute custom distance metric from the filled table. The distance metric is
- ///
- /// (total length of edits) / (total length of longer string)
- ///
- /// where length is measured in number of unicode grapheme clusters.
- #[allow(dead_code)]
- pub fn distance(&self) -> f64 {
- let (numer, denom) = self.distance_parts();
- (numer as f64) / (denom as f64)
- }
-
- #[allow(dead_code)]
- pub fn distance_parts(&self) -> (usize, usize) {
- let (mut numer, mut denom) = (0, 0);
- for op in self.operations() {
- if op != NoOp {
- numer += 1;
- }
- denom += 1;
- }
- (numer, denom)
- }
-
- /// Compute levenshtein distance from the filled table.
- #[allow(dead_code)]
- pub fn levenshtein_distance(&self) -> usize {
- self.table[self.index(self.x.len(), self.y.len())].cost
- }
-
// Row-major storage of 2D array.
fn index(&self, i: usize, j: usize) -> usize {
j * self.dim[1] + i
}
-
- #[allow(dead_code)]
- fn format_cell(&self, cell: &Cell) -> String {
- let parent = &self.table[cell.parent];
- let op = match cell.operation {
- Deletion => "-",
- Insertion => "+",
- NoOp => ".",
- };
- format!("{}{}{}", parent.cost, op, cell.cost)
- }
-
- #[allow(dead_code)]
- fn print(&self) {
- println!("x: {:?}", self.x);
- println!("y: {:?}", self.y);
- println!();
- print!(" ");
- for j in 0..self.dim[1] {
- print!("{} ", if j > 0 { self.x[j - 1] } else { " " })
- }
- println!();
-
- for i in 0..self.dim[0] {
- for j in 0..self.dim[1] {
- if j == 0 {
- print!("{} ", if i > 0 { self.y[i - 1] } else { " " })
- }
- let cell = &self.table[self.index(j, i)];
- print!("{} ", self.format_cell(cell));
- }
- println!();
- }
- println!();
- }
}
fn run_length_encode<T>(sequence: Vec<T>) -> Vec<(T, usize)>
@@ -454,4 +390,57 @@ mod tests {
);
Alignment::new(x, y).operations()
}
+
+ impl<'a> Alignment<'a> {
+ pub fn distance_parts(&self) -> (usize, usize) {
+ let (mut numer, mut denom) = (0, 0);
+ for op in self.operations() {
+ if op != NoOp {
+ numer += 1;
+ }
+ denom += 1;
+ }
+ (numer, denom)
+ }
+
+ /// Compute levenshtein distance from the filled table.
+ pub fn levenshtein_distance(&self) -> usize {
+ self.table[self.index(self.x.len(), self.y.len())].cost
+ }
+
+ #[allow(dead_code)]
+ fn format_cell(&self, cell: &Cell) -> String {
+ let parent = &self.table[cell.parent];
+ let op = match cell.operation {
+ Deletion => "-",
+ Insertion => "+",
+ NoOp => ".",
+ };
+ format!("{}{}{}", parent.cost, op, cell.cost)
+ }
+
+ #[allow(dead_code)]
+ fn print(&self) {
+ println!("x: {:?}", self.x);
+ println!("y: {:?}", self.y);
+ println!();
+ print!(" ");
+ for j in 0..self.dim[1] {
+ print!("{} ", if j > 0 { self.x[j - 1] } else { " " })
+ }
+ println!();
+
+ for i in 0..self.dim[0] {
+ for j in 0..self.dim[1] {
+ if j == 0 {
+ print!("{} ", if i > 0 { self.y[i - 1] } else { " " })
+ }
+ let cell = &self.table[self.index(j, i)];
+ print!("{} ", self.format_cell(cell));
+ }
+ println!();
+ }
+ println!();
+ }
+ }
}
diff --git a/src/ansi/mod.rs b/src/ansi/mod.rs
index 64b51c8b..a0f7f973 100644
--- a/src/ansi/mod.rs
+++ b/src/ansi/mod.rs
@@ -20,7 +20,6 @@ pub fn strip_ansi_codes(s: &str) -> String {
}
pub fn measure_text_width(s: &str) -> usize {
- // TODO: how should e.g. '\n' be handled?
ansi_strings_iterator(s).fold(0, |acc, (element, is_ansi)| {
acc + if is_ansi { 0 } else { element.width() }
})
@@ -54,9 +53,6 @@ pub fn truncate_str<'a>(s: &'a str, display_width: usize, tail: &str) -> Cow<'a,
for g in t.graphemes(true) {
let w = g.width();
if used + w > display_width {
- // use space to fill the gap left by truncate. Need another
- // option to custom this or just use `WrapOption.wrap_right_prefix_symbol`
- // in the future.
result.push_str(&" ".repeat(display_width.saturating_sub(used)));
break;
}