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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use unsegen::base::basic_types::*;
use unsegen::base::{Cursor, Window};
use unsegen::widget::{text_width, ColDemand, Demand};
use super::PagerLine;
pub trait LineDecorator {
type Line: PagerLine;
fn horizontal_space_demand<'a, 'b: 'a>(
&'a self,
lines: impl DoubleEndedIterator<Item = (LineIndex, &'b Self::Line)> + 'b,
) -> ColDemand
where
Self::Line: 'b;
fn decorate(
&self,
line: &Self::Line,
line_to_decorate_index: LineIndex,
active_line_index: LineIndex,
window: Window,
);
}
pub struct NoDecorator<L> {
_dummy: ::std::marker::PhantomData<L>,
}
impl<L> Default for NoDecorator<L> {
fn default() -> Self {
NoDecorator {
_dummy: Default::default(),
}
}
}
impl<L: PagerLine> LineDecorator for NoDecorator<L> {
type Line = L;
fn horizontal_space_demand<'a, 'b: 'a>(
&'a self,
_: impl DoubleEndedIterator<Item = (LineIndex, &'b Self::Line)> + 'b,
) -> ColDemand
where
Self::Line: 'b,
{
Demand::exact(0)
}
fn decorate(&self, _: &L, _: LineIndex, _: LineIndex, _: Window) {}
}
pub struct LineNumberDecorator<L> {
_dummy: ::std::marker::PhantomData<L>,
}
impl<L> Default for LineNumberDecorator<L> {
fn default() -> Self {
LineNumberDecorator {
_dummy: Default::default(),
}
}
}
impl<L: PagerLine> LineDecorator for LineNumberDecorator<L> {
type Line = L;
fn horizontal_space_demand<'a, 'b: 'a>(
&'a self,
lines: impl DoubleEndedIterator<Item = (LineIndex, &'b Self::Line)> + 'b,
) -> ColDemand
where
Self::Line: 'b,
{
let max_space = lines
.last()
.map(|(i, _)| text_width(format!(" {} ", i).as_str()))
.unwrap_or_else(|| Width::new(0).unwrap());
Demand::from_to(0, max_space.into())
}
fn decorate(&self, _: &L, line_to_decorate_index: LineIndex, _: LineIndex, mut window: Window) {
let width = (window.get_width() - 2).positive_or_zero();
let line_number = LineNumber::from(line_to_decorate_index);
let mut cursor = Cursor::new(&mut window).position(ColIndex::new(0), RowIndex::new(0));
use std::fmt::Write;
write!(cursor, " {:width$} ", line_number, width = width.into()).unwrap();
}
}