diff options
author | Canop <cano.petrole@gmail.com> | 2020-11-05 17:09:18 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-11-05 17:09:18 +0100 |
commit | e47ce676b0fe285f782d3a5282b8658b9b3568e5 (patch) | |
tree | 01a4be68305fd1ab85844fe1c5b8d105fb035a3d /src/preview/zero_len_file_view.rs | |
parent | cf6eab4a8ad7f1cd8881a9f4203bb8aaddb20e28 (diff) |
preview now supports opening zero length "files"v1.0.5
(for exemple the ones of /proc)
Diffstat (limited to 'src/preview/zero_len_file_view.rs')
-rw-r--r-- | src/preview/zero_len_file_view.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/preview/zero_len_file_view.rs b/src/preview/zero_len_file_view.rs new file mode 100644 index 0000000..5ec3045 --- /dev/null +++ b/src/preview/zero_len_file_view.rs @@ -0,0 +1,56 @@ +use { + crate::{ + display::{CropWriter, SPACE_FILLING, Screen, W}, + errors::ProgramError, + skin::PanelSkin, + }, + char_reader::CharReader, + crossterm::{ + cursor, + QueueableCommand, + }, + std::{ + fs::File, + path::PathBuf, + }, + termimad::{Area}, +}; + +/// a (light) display for a file declaring a size 0, +/// as happens for many system "files", for example in /proc +pub struct ZeroLenFileView { + path: PathBuf, +} + +impl ZeroLenFileView { + pub fn new(path: PathBuf) -> Self { + Self { + path, + } + } + pub fn display( + &mut self, + w: &mut W, + _screen: Screen, + panel_skin: &PanelSkin, + area: &Area, + ) -> Result<(), ProgramError> { + let styles = &panel_skin.styles; + let line_count = area.height as usize; + let file = File::open(&self.path)?; + let mut reader = CharReader::new(file); + // line_len here is in chars, and we crop in cols, but it's OK because both + // are usually identical for system files and we crop later anyway + let line_len = area.width as usize; + 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); + let cw = &mut cw; + if let Some(line) = reader.next_line(line_len, 15_000)? { + cw.queue_str(&styles.default, &line)?; + } + cw.fill(&styles.default, &SPACE_FILLING)?; + } + Ok(()) + } +} |