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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
use {
super::byte::Byte,
crate::{
command::ScrollCommand,
display::{Screen, W},
errors::ProgramError,
skin::PanelSkin,
},
crossterm::{
cursor,
style::{Color, Print, SetForegroundColor},
QueueableCommand,
},
memmap::Mmap,
std::{
fs::File,
io,
path::PathBuf,
},
termimad::{Area, CropWriter, SPACE_FILLING},
};
pub struct HexLine {
pub bytes: Vec<u8>, // from 1 to 16 bytes
}
/// a preview showing the content of a file in hexa
pub struct HexView {
path: PathBuf,
len: usize,
scroll: usize,
page_height: usize,
}
impl HexView {
pub fn new(path: PathBuf) -> io::Result<Self> {
let len = path.metadata()?.len() as usize;
Ok(Self {
path,
len,
scroll: 0,
page_height: 0,
})
}
pub fn line_count(&self) -> usize {
self.len / 16 + if self.len % 16 != 0 { 1 } else { 0 }
}
pub fn try_scroll(
&mut self,
cmd: ScrollCommand,
) -> bool {
let old_scroll = self.scroll;
self.scroll = cmd.apply(self.scroll, self.line_count(), self.page_height);
self.scroll != old_scroll
}
pub fn select_first(&mut self) {
self.scroll = 0;
}
pub fn select_last(&mut self) {
if self.page_height < self.line_count() {
self.scroll = self.line_count() - self.page_height;
}
}
pub fn get_page(
&mut self,
start_line_idx: usize,
line_count: usize,
) -> io::Result<Vec<HexLine>> {
let file = File::open(&self.path)?;
let mut lines = Vec::new();
if self.len == 0 {
return Ok(lines);
}
let mmap = unsafe { Mmap::map(&file)? };
let new_len = mmap.len();
if new_len != self.len {
warn!("previewed file len changed from {} to {}", self.len, new_len);
self.len = new_len;
}
let mut start_idx = 16 * start_line_idx;
while start_idx < self.len {
let line_len = 16.min(self.len - start_idx);
let mut bytes: Vec<u8> = vec![0; line_len];
bytes[0..line_len].copy_from_slice(&mmap[start_idx..start_idx + line_len]);
lines.push(HexLine { bytes });
if lines.len() >= line_count {
break;
}
start_idx += line_len;
}
Ok(lines)
}
pub fn display(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area,
) -> Result<(), ProgramError> {
let line_count = area.height as usize;
self.page_height = area.height as usize;
let page = self.get_page(self.scroll, line_count)?;
let addresses_len = if self.len < 0xffff {
4
} else if self.len < 0xffffff {
6
} else {
8
};
let mut margin_around_adresses = false;
let styles = &panel_skin.styles;
let mut left_margin = false;
let mut addresses = false;
let mut hex_middle_space = false;
let mut chars_middle_space = false;
let mut inter_hex = false;
let mut chars = false;
let mut rem = area.width as i32 - 32; // 32: minimum, tight
if rem > 17 {
chars = true;
rem -= 17;
}
if rem > 16 {
inter_hex = true;
rem -= 16;
}
if rem > addresses_len {
addresses = true;
rem -= addresses_len;
}
if rem > 1 {
hex_middle_space = true;
rem -= 1;
}
if rem > 1 {
left_margin = true;
rem -= 1;
}
if rem > 1 {
chars_middle_space = true;
rem -= 1;
}
if addresses && rem >= 2 {
margin_around_adresses = true;
//rem -= 2;
}
let scrollbar = area.scrollbar(self.scroll, self.line_count());
let scrollbar_fg = styles.scrollbar_thumb.get_fg()
.or_else(|| styles.preview.get_fg())
.unwrap_or(Color::White);
for y in 0..line_count {
w.queue(cursor::MoveTo(area.left, y as u16 + area.top))?;
let mut cw = CropWriter::new(w, area.width as usize - 1); // -1 for scrollbar
let cw = &mut cw;
if y < page.len() {
if addresses {
let addr = (self.scroll + y) * 16;
cw.queue_g_string(
&styles.preview_line_number,
match (addresses_len, margin_around_adresses) {
(4, false) => format!("{:04x}", addr),
(6, false) => format!("{:06x}", addr),
(_, false) => format!("{:08x}", addr),
(4, true) => format!(" {:04x} ", addr),
(6, true) => format!(" {:06x} ", addr),
(_, true) => format!(" {:08x} ", addr),
},
)?;
}
if left_margin {
cw.queue_char(&styles.default, ' ')?;
}
let line = &page[y];
for x in 0..16 {
if x == 8 && hex_middle_space {
cw.queue_char(&styles.default, ' ')?;
}
if let Some(b) = line.bytes.get(x) {
let byte = Byte::from(*b);
if inter_hex {
cw.queue_g_string(byte.style(styles), format!("{:02x} ", b))?;
} else {
cw.queue_g_string(byte.style(styles), format!("{:02x}", b))?;
}
} else {
cw.queue_str(&styles.default, if inter_hex { " " } else { " " })?;
}
}
if chars {
cw.queue_char(&styles.default, ' ')?;
for x in 0..16 {
if x == 8 && chars_middle_space {
cw.queue_char(&styles.default, ' ')?;
}
if let Some(b) = line.bytes.get(x) {
let byte = Byte::from(*b);
cw.queue_char(byte.style(styles), byte.as_char())?;
}
}
}
}
cw.fill(&styles.default, &SPACE_FILLING)?;
|