svgbob/buffer/fragment_buffer/
fragment_span.rs

1use crate::{buffer::Span, Cell, Fragment, Merge, Settings};
2use std::{cmp::Ordering, fmt};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct FragmentSpan {
6    pub span: Span,
7    pub fragment: Fragment,
8}
9
10impl fmt::Display for FragmentSpan {
11    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12        writeln!(f, "{}", self.fragment)
13    }
14}
15
16impl Ord for FragmentSpan {
17    fn cmp(&self, other: &Self) -> Ordering {
18        self.fragment.cmp(&other.fragment)
19    }
20}
21
22impl PartialOrd for FragmentSpan {
23    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
24        Some(self.cmp(other))
25    }
26}
27
28impl FragmentSpan {
29    pub fn new(span: Span, fragment: Fragment) -> Self {
30        Self { span, fragment }
31    }
32
33    pub fn cells(&self) -> Vec<Cell> {
34        self.span.0.iter().map(|(cell, _ch)| *cell).collect()
35    }
36
37    pub fn scale(&self, scale: f32) -> Self {
38        Self {
39            span: self.span.clone(),
40            fragment: self.fragment.scale(scale),
41        }
42    }
43
44    pub(crate) fn is_contacting(&self, other: &Self) -> bool {
45        self.fragment.is_contacting(&other.fragment)
46    }
47
48    pub fn absolute_position(&self, cell: Cell) -> Self {
49        Self {
50            span: self.span.clone(),
51            fragment: self.fragment.absolute_position(cell),
52        }
53    }
54
55    pub fn is_bounded(&self, bound1: Cell, bound2: Cell) -> bool {
56        self.span.is_bounded(bound1, bound2)
57    }
58
59    pub fn hit_cell(&self, needle: Cell) -> bool {
60        self.span.hit_cell(needle)
61    }
62}
63
64impl Merge for FragmentSpan {
65    fn merge(&self, other: &Self) -> Option<Self> {
66        if let Some(new_merge) = self.fragment.merge(&other.fragment) {
67            let new_span = self.span.merge_no_check(&other.span);
68            Some(Self {
69                span: new_span,
70                fragment: new_merge,
71            })
72        } else {
73            None
74        }
75    }
76}