summaryrefslogtreecommitdiffstats
path: root/packages/svgbob/src/buffer/fragment_buffer/fragment_span.rs
blob: 0db3f13d578592e3a8b0686728a555b4450a5442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{buffer::Span, Cell, Fragment, Merge, Settings};
use std::{cmp::Ordering, fmt};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FragmentSpan {
    pub span: Span,
    pub fragment: Fragment,
}

impl fmt::Display for FragmentSpan {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{}", self.fragment)
    }
}

impl Ord for FragmentSpan {
    fn cmp(&self, other: &Self) -> Ordering {
        self.fragment.cmp(&other.fragment)
    }
}

impl PartialOrd for FragmentSpan {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl FragmentSpan {
    pub fn new(span: Span, fragment: Fragment) -> Self {
        Self { span, fragment }
    }

    pub fn cells(&self) -> Vec<Cell> {
        self.span.0.iter().map(|(cell, _ch)| *cell).collect()
    }

    pub fn scale(&self, scale: f32) -> Self {
        Self {
            span: self.span.clone(),
            fragment: self.fragment.scale(scale),
        }
    }

    pub(crate) fn is_contacting(&self, other: &Self) -> bool {
        self.fragment.is_contacting(&other.fragment)
    }

    pub fn absolute_position(&self, cell: Cell) -> Self {
        Self {
            span: self.span.clone(),
            fragment: self.fragment.absolute_position(cell),
        }
    }

    pub fn is_bounded(&self, bound1: Cell, bound2: Cell) -> bool {
        self.span.is_bounded(bound1, bound2)
    }

    pub fn hit_cell(&self, needle: Cell) -> bool {
        self.span.hit_cell(needle)
    }
}

impl Merge for FragmentSpan {
    fn merge(&self, other: &Self) -> Option<Self> {
        if let Some(new_merge) = self.fragment.merge(&other.fragment) {
            let new_span = self.span.merge_no_check(&other.span);
            Some(Self {
                span: new_span,
                fragment: new_merge,
            })
        } else {
            None
        }
    }
}