pub struct HexView { /* private fields */ }
Expand description
a preview showing the content of a file in hexa
Implementations§
source§impl HexView
impl HexView
sourcepub fn new(path: PathBuf) -> Result<Self>
pub fn new(path: PathBuf) -> Result<Self>
Examples found in repository?
src/preview/preview.rs (line 60)
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
pub fn with_mode(
path: &Path,
mode: PreviewMode,
con: &AppContext,
) -> Result<Self, ProgramError> {
match mode {
PreviewMode::Hex => {
Ok(HexView::new(path.to_path_buf()).map(Self::Hex)?)
}
PreviewMode::Image => {
ImageView::new(path).map(Self::Image)
}
PreviewMode::Text => {
Ok(
SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false)
.transpose()
.expect("syntactic view without pattern shouldn't be none")
.map(Self::Syntactic)?,
)
}
}
}
/// build an image view, unless the file can't be interpreted
/// as an image, in which case a hex view is used
pub fn image(path: &Path) -> Self {
ImageView::new(path)
.ok()
.map(Self::Image)
.unwrap_or_else(|| Self::hex(path))
}
/// build a text preview (maybe with syntaxic coloring) if possible,
/// a hex (binary) view if content isnt't UTF8, a ZeroLen file if there's
/// no length (it's probably a linux pseudofile) or a IOError when
/// there's a IO problem
pub fn unfiltered_text(
path: &Path,
con: &AppContext,
) -> Self {
match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false) {
Ok(Some(sv)) => Self::Syntactic(sv),
Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => {
debug!("zero len or unmappable file - check if system file");
Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf()))
}
Err(ProgramError::SyntectCrashed { details }) => {
warn!("syntect crashed with message : {details:?}");
Self::unstyled_text(path, con)
}
// not previewable as UTF8 text
// we'll try reading it as binary
Err(ProgramError::UnprintableFile) => Self::hex(path),
_ => Self::hex(path),
}
}
/// build a text preview with no syntax highlighting, if possible
pub fn unstyled_text(
path: &Path,
con: &AppContext,
) -> Self {
match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, true) {
Ok(Some(sv)) => Self::Syntactic(sv),
Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => {
debug!("zero len or unmappable file - check if system file");
Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf()))
}
// not previewable as UTF8 text - we'll try reading it as binary
Err(ProgramError::UnprintableFile) => Self::hex(path),
_ => Self::hex(path),
}
}
/// try to build a filtered text view. Will return None if
/// the dam gets an event before it's built
pub fn filtered(
&self,
path: &Path,
pattern: InputPattern,
dam: &mut Dam,
con: &AppContext,
) -> Option<Self> {
match self {
Self::Syntactic(_) => {
match SyntacticView::new(path, pattern, dam, con, false) {
// normal finished loading
Ok(Some(sv)) => Some(Self::Syntactic(sv)),
// interrupted search
Ok(None) => None,
// not previewable as UTF8 text
// we'll try reading it as binary
Err(_) => Some(Self::hex(path)), // FIXME try as unstyled if syntect crashed
}
}
_ => None, // not filterable
}
}
/// return a hex_view, suitable for binary, or Self::IOError
/// if there was an error
pub fn hex(path: &Path) -> Self {
match HexView::new(path.to_path_buf()) {
Ok(reader) => Self::Hex(reader),
Err(e) => {
// it's unlikely as the file isn't open at this point
warn!("error while previewing {:?} : {:?}", path, e);
Self::IoError(e)
}
}
}
sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Examples found in repository?
src/hex/hex_view.rs (line 54)
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
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 < 0xff_ffff {
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;
const MIN: i32 =
1 // margin
+ 32 // 32 hex
+ 1; // scrollbar
let mut rem = area.width as i32 - MIN;
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)?;
if is_thumb(y as u16 + area.top, scrollbar) {
w.queue(SetForegroundColor(scrollbar_fg))?;
w.queue(Print('▐'))?;
} else {
w.queue(Print(' '))?;
}
}
Ok(())
}
sourcepub fn try_scroll(&mut self, cmd: ScrollCommand) -> bool
pub fn try_scroll(&mut self, cmd: ScrollCommand) -> bool
Examples found in repository?
src/preview/preview.rs (line 185)
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
pub fn try_scroll(
&mut self,
cmd: ScrollCommand,
) -> bool {
match self {
Self::Syntactic(sv) => sv.try_scroll(cmd),
Self::Hex(hv) => hv.try_scroll(cmd),
_ => false,
}
}
pub fn is_filterable(&self) -> bool {
matches!(self, Self::Syntactic(_))
}
pub fn get_selected_line(&self) -> Option<String> {
match self {
Self::Syntactic(sv) => sv.get_selected_line(),
_ => None,
}
}
pub fn get_selected_line_number(&self) -> Option<LineNumber> {
match self {
Self::Syntactic(sv) => sv.get_selected_line_number(),
_ => None,
}
}
pub fn try_select_line_number(&mut self, number: usize) -> bool {
match self {
Self::Syntactic(sv) => sv.try_select_line_number(number),
_ => false,
}
}
pub fn unselect(&mut self) {
if let Self::Syntactic(sv) = self {
sv.unselect();
}
}
pub fn try_select_y(&mut self, y: u16) -> bool {
match self {
Self::Syntactic(sv) => sv.try_select_y(y),
_ => false,
}
}
pub fn move_selection(&mut self, dy: i32, cycle: bool) {
match self {
Self::Syntactic(sv) => sv.move_selection(dy, cycle),
Self::Hex(hv) => {
hv.try_scroll(ScrollCommand::Lines(dy));
}
_ => {}
}
}
sourcepub fn select_first(&mut self)
pub fn select_first(&mut self)
sourcepub fn select_last(&mut self)
pub fn select_last(&mut self)
sourcepub fn get_page(
&mut self,
start_line_idx: usize,
line_count: usize
) -> Result<Vec<HexLine>>
pub fn get_page(
&mut self,
start_line_idx: usize,
line_count: usize
) -> Result<Vec<HexLine>>
Examples found in repository?
src/hex/hex_view.rs (line 103)
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
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 < 0xff_ffff {
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;
const MIN: i32 =
1 // margin
+ 32 // 32 hex
+ 1; // scrollbar
let mut rem = area.width as i32 - MIN;
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)?;
if is_thumb(y as u16 + area.top, scrollbar) {
w.queue(SetForegroundColor(scrollbar_fg))?;
w.queue(Print('▐'))?;
} else {
w.queue(Print(' '))?;
}
}
Ok(())
}
sourcepub fn display(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
pub fn display(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
Examples found in repository?
src/preview/preview.rs (line 258)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
pub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext,
area: &Area,
) -> Result<(), ProgramError> {
let panel_skin = &disc.panel_skin;
let screen = disc.screen;
let con = &disc.con;
match self {
Self::Image(iv) => iv.display(w, disc, area),
Self::Syntactic(sv) => sv.display(w, screen, panel_skin, area, con),
Self::ZeroLen(zlv) => zlv.display(w, screen, panel_skin, area),
Self::Hex(hv) => hv.display(w, screen, panel_skin, area),
Self::IoError(err) => {
let mut y = area.top;
w.queue(cursor::MoveTo(area.left, y))?;
let mut cw = CropWriter::new(w, area.width as usize);
cw.queue_str(&panel_skin.styles.default, "An error prevents the preview:")?;
cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
y += 1;
w.queue(cursor::MoveTo(area.left, y))?;
let mut cw = CropWriter::new(w, area.width as usize);
cw.queue_g_string(&panel_skin.styles.status_error, err.to_string())?;
cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
y += 1;
while y < area.top + area.height {
w.queue(cursor::MoveTo(area.left, y))?;
let mut cw = CropWriter::new(w, area.width as usize);
cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
y += 1;
}
Ok(())
}
}
}
sourcepub fn display_info(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
pub fn display_info(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
Examples found in repository?
src/preview/preview.rs (line 291)
281 282 283 284 285 286 287 288 289 290 291 292 293 294
pub fn display_info(
&mut self,
w: &mut W,
screen: Screen,
panel_skin: &PanelSkin,
area: &Area,
) -> Result<(), ProgramError> {
match self {
Self::Image(iv) => iv.display_info(w, screen, panel_skin, area),
Self::Syntactic(sv) => sv.display_info(w, screen, panel_skin, area),
Self::Hex(hv) => hv.display_info(w, screen, panel_skin, area),
_ => Ok(()),
}
}