forked from skim-rs/skim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.rs
147 lines (125 loc) · 4.16 KB
/
header.rs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
///! header of the items
use crate::ansi::{ANSIParser, AnsiString};
use crate::event::UpdateScreen;
use crate::event::{Event, EventHandler};
use crate::item::ItemPool;
use crate::theme::ColorTheme;
use crate::theme::DEFAULT_THEME;
use crate::util::{print_item, str_lines, LinePrinter};
use crate::{DisplayContext, Matches, SkimOptions};
use defer_drop::DeferDrop;
use std::cmp::max;
use std::sync::Arc;
use tuikit::prelude::*;
pub struct Header {
header: Vec<AnsiString<'static>>,
tabstop: usize,
reverse: bool,
theme: Arc<ColorTheme>,
// for reserved header items
item_pool: Arc<DeferDrop<ItemPool>>,
}
impl Header {
pub fn empty() -> Self {
Self {
header: vec![],
tabstop: 8,
reverse: false,
theme: Arc::new(*DEFAULT_THEME),
item_pool: Arc::new(DeferDrop::new(ItemPool::new())),
}
}
pub fn item_pool(mut self, item_pool: Arc<DeferDrop<ItemPool>>) -> Self {
self.item_pool = item_pool;
self
}
pub fn theme(mut self, theme: Arc<ColorTheme>) -> Self {
self.theme = theme;
self
}
pub fn with_options(mut self, options: &SkimOptions) -> Self {
if let Some(tabstop_str) = options.tabstop {
let tabstop = tabstop_str.parse::<usize>().unwrap_or(8);
self.tabstop = max(1, tabstop);
}
if options.layout.starts_with("reverse") {
self.reverse = true;
}
match options.header {
None => {}
Some("") => {}
Some(header) => {
let mut parser = ANSIParser::default();
self.header = str_lines(header).into_iter().map(|l| parser.parse_ansi(l)).collect();
}
}
self
}
fn lines_of_header(&self) -> usize {
self.header.len() + self.item_pool.reserved().len()
}
fn adjust_row(&self, index: usize, screen_height: usize) -> usize {
if self.reverse {
index
} else {
screen_height - index - 1
}
}
}
impl Draw for Header {
fn draw(&self, canvas: &mut dyn Canvas) -> Result<()> {
let (screen_width, screen_height) = canvas.size()?;
if screen_width < 3 {
return Err("screen width is too small".into());
}
if screen_height < self.lines_of_header() {
return Err("screen height is too small".into());
}
canvas.clear()?;
for (idx, header) in self.header.iter().enumerate() {
// print fixed header(specified by --header)
let mut printer = LinePrinter::builder()
.row(self.adjust_row(idx, screen_height))
.col(2)
.tabstop(self.tabstop)
.container_width(screen_width - 2)
.shift(0)
.text_width(screen_width - 2)
.build();
for (ch, _attr) in header.iter() {
printer.print_char(canvas, ch, self.theme.header(), false);
}
}
let lines_used = self.header.len();
// print "reserved" header lines (--header-lines)
for (idx, item) in self.item_pool.reserved().iter().enumerate() {
let mut printer = LinePrinter::builder()
.row(self.adjust_row(idx + lines_used, screen_height))
.col(2)
.tabstop(self.tabstop)
.container_width(screen_width - 2)
.shift(0)
.text_width(screen_width - 2)
.build();
let context = DisplayContext {
text: &item.text(),
score: 0,
matches: Matches::None,
container_width: screen_width - 2,
highlight_attr: self.theme.header(),
};
print_item(canvas, &mut printer, item.display(context), self.theme.header());
}
Ok(())
}
}
impl Widget<Event> for Header {
fn size_hint(&self) -> (Option<usize>, Option<usize>) {
(None, Some(self.lines_of_header()))
}
}
impl EventHandler for Header {
fn handle(&mut self, _event: &Event) -> UpdateScreen {
UpdateScreen::DONT_REDRAW
}
}