summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2021-12-22 21:50:21 +0100
committerCanop <cano.petrole@gmail.com>2021-12-22 21:50:21 +0100
commit5ba477c1e0e9ffe7ff43873ab8b1b49b0a08c123 (patch)
tree44a1c76855a3170d6360335e6a2414ffe40eb772 /src
parentf7520dc392fa3a38bac94e491c6fd1321266f971 (diff)
text previews switch to hex when there are non printable chars
Diffstat (limited to 'src')
-rw-r--r--src/errors.rs1
-rw-r--r--src/preview/preview.rs1
-rw-r--r--src/syntactic/syntactic_view.rs16
3 files changed, 17 insertions, 1 deletions
diff --git a/src/errors.rs b/src/errors.rs
index 9103ded..bf76509 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -28,6 +28,7 @@ custom_error! {pub ProgramError
Lfs {details: String} = "Failed to fetch mounts: {}",
ZeroLenFile = "File seems empty",
UnmappableFile = "File can't be mapped",
+ UnprintableFile = "File can't be printed", // has characters that can't be printed without escaping
}
custom_error! {pub TreeBuildError
diff --git a/src/preview/preview.rs b/src/preview/preview.rs
index f76012b..a80c4e3 100644
--- a/src/preview/preview.rs
+++ b/src/preview/preview.rs
@@ -97,6 +97,7 @@ impl Preview {
}
// not previewable as UTF8 text
// we'll try reading it as binary
+ Err(ProgramError::UnprintableFile) => Self::hex(path),
_ => Self::hex(path),
}
}
diff --git a/src/syntactic/syntactic_view.rs b/src/syntactic/syntactic_view.rs
index ba0063e..2e48c7c 100644
--- a/src/syntactic/syntactic_view.rs
+++ b/src/syntactic/syntactic_view.rs
@@ -138,6 +138,12 @@ impl SyntacticView {
while line.ends_with('\n') || line.ends_with('\r') {
line.pop();
}
+ for c in line.chars() {
+ if !is_char_printable(c) {
+ debug!("unprintable char: {:?}", c);
+ return Err(ProgramError::UnprintableFile);
+ }
+ }
if pattern.is_empty() || pattern.score_of_string(&line).is_some() {
let name_match = pattern.search_string(&line);
let regions = if let Some(highlighter) = highlighter.as_mut() {
@@ -173,7 +179,9 @@ impl SyntacticView {
}
fn ensure_selection_is_visible(&mut self) {
- if let Some(idx) = self.selection_idx {
+ if self.page_height >= self.lines.len() {
+ self.scroll = 0;
+ } else if let Some(idx) = self.selection_idx {
let padding = self.padding();
if idx < self.scroll + padding || idx + padding > self.scroll + self.page_height {
if idx <= padding {
@@ -443,3 +451,9 @@ fn is_thumb(y: usize, scrollbar: Option<(u16, u16)>) -> bool {
})
}
+/// tell whether the character is normal enough to be displayed by the
+/// syntactic view (if not we'll use a hex view)
+fn is_char_printable(c: char) -> bool {
+ // the tab is printable because it's replaced by spaces
+ c == '\t' || !c.is_control()
+}