use std::{cmp::Ordering, fmt};
use rustik_tui as tui;
use tui::{Line, Paint, Style};
use crate::{FileId, FileStats, HunkId};
pub trait DiffDecorator: fmt::Debug + Send + Sync {
fn file(&self, file: FileId) -> FileDecoration {
let _ = file;
FileDecoration::default()
}
fn hunk(&self, file: FileId, hunk: HunkId) -> HunkDecoration {
let _ = (file, hunk);
HunkDecoration::default()
}
}
#[derive(Clone, Debug, Default)]
pub struct FileDecoration {
pub status: Option<Paint<char>>,
pub annotations: Vec<Line>,
pub progress: Option<ReviewProgress>,
pub stats: Option<FileStats>,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ReviewProgress {
pub accepted: usize,
pub total: usize,
}
impl ReviewProgress {
pub fn is_complete(self) -> bool {
self.total > 0 && self.accepted >= self.total
}
pub fn filled(self, width: usize) -> usize {
if self.total == 0 {
0
} else {
self.accepted
.saturating_mul(width)
.div_ceil(self.total)
.min(width)
}
}
}
impl Ord for ReviewProgress {
fn cmp(&self, other: &Self) -> Ordering {
match (self.total == 0, other.total == 0) {
(true, true) => self.accepted.cmp(&other.accepted),
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
(false, false) => {
let left = (self.accepted as u128) * (other.total as u128);
let right = (other.accepted as u128) * (self.total as u128);
left.cmp(&right)
.then_with(|| self.accepted.cmp(&other.accepted))
.then_with(|| self.total.cmp(&other.total))
}
}
}
}
impl PartialOrd for ReviewProgress {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Clone, Debug, Default)]
pub struct HunkDecoration {
pub gutter: Option<Paint<char>>,
pub corner: Line,
pub style: Option<Style>,
}