Skip to content

Commit

Permalink
fzf-v2: fix ScoringMatrix's Debug impl
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 23, 2023
1 parent e511810 commit 245cc0b
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/algos/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,24 +807,61 @@ impl core::fmt::Debug for ScoringMatrix<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use core::fmt::Write;

// The matrix should never be empty, but just in case.
if self.slice.is_empty() {
return f.write_str("[ ]");
}

fn width(score: Score) -> usize {
if score == 0 {
1
} else {
(score.ilog10() + 1) as usize
}
}

let mut rows = self.rows(self.top_left_idx());

while let Some(cell) = rows.next(self) {
let (opening_char, closing_char) =
match (cell.is_first_row(self), cell.is_last_row(self)) {
(true, true) => ('[', ']'),
(true, false) => ('β”Œ', '┐'),
(false, true) => ('β””', 'β”˜'),
(false, false) => ('β”‚', 'β”‚'),
};
let max_score_width = {
let max_score = self.slice.iter().copied().max().unwrap();
width(max_score)
};

let opening_char: char;

let closing_char: char;

let printed_matrix_inner_width =
(self.width - 1) * (max_score_width + 1) + max_score_width;

if self.height == 1 {
opening_char = '[';
closing_char = ']';
} else {
f.write_char('β”Œ')?;
f.write_str(&" ".repeat(printed_matrix_inner_width))?;
f.write_char('┐')?;
f.write_char('\n')?;
opening_char = 'β”‚';
closing_char = 'β”‚';
}

while let Some(cell) = rows.next(self) {
f.write_char(opening_char)?;

let mut cols = self.cols(cell);

while let Some(cell) = cols.next(self) {
let score = self[cell];

write!(f, "{score}", score = self[cell])?;

let score_width = width(score);

for _ in score_width..max_score_width {
f.write_char(' ')?;
}

if !cell.is_last_col(self) {
f.write_char(' ')?;
}
Expand All @@ -835,6 +872,12 @@ impl core::fmt::Debug for ScoringMatrix<'_> {
f.write_char('\n')?;
}

if self.height > 1 {
f.write_char('β””')?;
f.write_str(&" ".repeat(printed_matrix_inner_width))?;
f.write_char('β”˜')?;
}

Ok(())
}
}
Expand Down

0 comments on commit 245cc0b

Please sign in to comment.