diff options
author | Canop <cano.petrole@gmail.com> | 2021-08-29 21:21:47 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-08-29 21:21:47 +0200 |
commit | d6ceba05642982674ee80bd8bccd25980dab9e8c (patch) | |
tree | fd0626430a4a2fb25d33ae56f8ec6b4923ae200a /src | |
parent | ee354f9a7a2684783456b10707462f71045b1c3e (diff) |
move some fit/crop utilities from broot's code to termimad
Diffstat (limited to 'src')
-rw-r--r-- | src/display/crop_writer.rs | 140 | ||||
-rw-r--r-- | src/display/filling.rs | 63 | ||||
-rw-r--r-- | src/display/matched_string.rs | 1 | ||||
-rw-r--r-- | src/display/mod.rs | 28 | ||||
-rw-r--r-- | src/filesystems/filesystems_state.rs | 2 | ||||
-rw-r--r-- | src/filesystems/mount_space_display.rs | 4 | ||||
-rw-r--r-- | src/hex/hex_view.rs | 4 | ||||
-rw-r--r-- | src/image/double_line.rs | 3 | ||||
-rw-r--r-- | src/image/image_view.rs | 4 | ||||
-rw-r--r-- | src/preview/preview.rs | 2 | ||||
-rw-r--r-- | src/preview/preview_state.rs | 4 | ||||
-rw-r--r-- | src/preview/zero_len_file_view.rs | 4 | ||||
-rw-r--r-- | src/stage/stage_state.rs | 4 | ||||
-rw-r--r-- | src/syntactic/syntactic_view.rs | 4 |
14 files changed, 20 insertions, 247 deletions
diff --git a/src/display/crop_writer.rs b/src/display/crop_writer.rs deleted file mode 100644 index 8295c50..0000000 --- a/src/display/crop_writer.rs +++ /dev/null @@ -1,140 +0,0 @@ -use { - super::{Filling, TAB_REPLACEMENT}, - crossterm::{style::Print, QueueableCommand}, - std::borrow::Cow, - termimad::{ - CompoundStyle, - Result, - StrFit, - }, - unicode_width::UnicodeWidthChar, -}; - -/// wrap a writer to ensure that at most `allowed` columns are -/// written. -pub struct CropWriter<'w, W> -where - W: std::io::Write, -{ - pub w: &'w mut W, - /// number of screen columns which may be covered - pub allowed: usize, -} - -impl<'w, W> CropWriter<'w, W> -where - W: std::io::Write, -{ - pub fn new(w: &'w mut W, limit: usize) -> Self { - Self { w, allowed: limit } - } - pub fn is_full(&self) -> bool { - self.allowed == 0 - } - /// return a tuple containing a string containing either the given &str - /// or the part fitting the remaining width, and the width of this string) - pub fn cropped_str<'a>(&self, s: &'a str) -> (Cow<'a, str>, usize) { - StrFit::make_cow(s, self.allowed) - } - pub fn queue_unstyled_str(&mut self, s: &str) -> Result<()> { - if self.is_full() { - return Ok(()); - } - let (string, len) = self.cropped_str(s); - self.allowed -= len; - self.w.queue(Print(string))?; - Ok(()) - } - pub fn queue_str(&mut self, cs: &CompoundStyle, s: &str) -> Result<()> { - if self.is_full() { - return Ok(()); - } - let (string, len) = self.cropped_str(s); - self.allowed -= len; - cs.queue(self.w, string) - } - pub fn queue_char(&mut self, cs: &CompoundStyle, c: char) -> Result<()> { - let width = UnicodeWidthChar::width(c).unwrap_or(0); - if width < self.allowed { - self.allowed -= width; - cs.queue(self.w, c)?; - } - Ok(()) - } - pub fn queue_unstyled_char(&mut self, c: char) -> Result<()> { - if c == '\t' { - return self.queue_unstyled_str(TAB_REPLACEMENT); - } - let width = UnicodeWidthChar::width(c).unwrap_or(0); - if width < self.allowed { - self.allowed -= width; - self.w.queue(Print(c))?; - } - Ok(()) - } - /// a "g_string" is a "gentle" one: each char takes one column on screen. - /// This function must thus not be used for unknown strings. - pub fn queue_unstyled_g_string(&mut self, mut s: String) -> Result<()> { - if self.is_full() { - return Ok(()); - } - let mut len = 0; - for (idx, _) in s.char_indices() { - len += 1; - if len > self.allowed { - s.truncate(idx); - self.allowed = 0; - self.w.queue(Print(s))?; - return Ok(()); - } - } - self.allowed -= len; - self.w.queue(Print(s))?; - Ok(()) - } - /// a "g_string" is a "gentle" one: each char takes one column on screen. - /// This function must thus not be used for unknown strings. - pub fn queue_g_string(&mut self, cs: &CompoundStyle, mut s: String) -> Result<()> { - if self.is_full() { - return Ok(()); - } - let mut len = 0; - for (idx, _) in s.char_indices() { - len += 1; - if len > self.allowed { - s.truncate(idx); - self.allowed = 0; - return cs.queue(self.w, s); - } - } - self.allowed -= len; - cs.queue(self.w, s) - } - pub fn queue_fg(&mut self, cs: &CompoundStyle) -> Result<()> { - cs.queue_fg(self.w) - } - pub fn queue_bg(&mut self, cs: &CompoundStyle) -> Result<()> { - cs.queue_bg(self.w) - } - pub fn fill(&mut self, cs: &CompoundStyle, filling: &'static Filling) -> Result<()> { - self.repeat(cs, filling, self.allowed) - } - pub fn fill_unstyled(&mut self, filling: &'static Filling) -> Result<()> { - self.repeat_unstyled(filling, self.allowed) - } - pub fn repeat( - &mut self, - cs: &CompoundStyle, - filling: &'static Filling, - mut len: usize, - ) -> Result<()> { - len = len.min(self.allowed); - self.allowed -= len; - filling.queue_styled(self.w, cs, len) - } - pub fn repeat_unstyled(&mut self, filling: &'static Filling, mut len: usize) -> Result<()> { - len = len.min(self.allowed); - self.allowed -= len; - filling.queue_unstyled(self.w, len) - } -} diff --git a/src/display/filling.rs b/src/display/filling.rs deleted file mode 100644 index 4939879..0000000 --- a/src/display/filling.rs +++ /dev/null @@ -1,63 +0,0 @@ - -use { - crossterm::{ - QueueableCommand, - style::Print, - }, - termimad::{ - CompoundStyle, - Result, - }, -}; - -const FILLING_STRING_CHAR_LEN: usize = 1000; - -pub struct Filling { - filling_string: String, - char_size: usize, -} - -impl Filling { - // TODO as soon as const fn are capable enough, this should be const - // to allow normal static fillings - pub fn from_char(filling_char: char) -> Self { - let char_size = String::from(filling_char).len(); - let mut filling_string = String::with_capacity(char_size * FILLING_STRING_CHAR_LEN); - for _ in 0..FILLING_STRING_CHAR_LEN { - filling_string.push(filling_char); - } - Self { - filling_string, - char_size, - } - } - pub fn queue_unstyled<W>( - &self, - w: &mut W, - mut len: usize, - ) -> Result<()> - where W: std::io::Write - { - while len > 0 { - let sl = len.min(FILLING_STRING_CHAR_LEN); - w.queue(Print(&self.filling_string[0..sl * self.char_size]))?; - len -= sl; - } - Ok(()) - } - pub fn queue_styled<W>( - &self, - w: &mut W, - cs: &CompoundStyle, - mut len: usize, - ) -> Result<()> - where W: std::io::Write - { - while len > 0 { - let sl = len.min(FILLING_STRING_CHAR_LEN); - cs.queue_str(w, &self.filling_string[0..sl * self.char_size])?; - len -= sl; - } - Ok(()) - } -} diff --git a/src/display/matched_string.rs b/src/display/matched_string.rs index d37b366..8f962a0 100644 --- a/src/display/matched_string.rs +++ b/src/display/matched_string.rs @@ -15,6 +15,7 @@ pub struct MatchedString<'a> { } impl<'a, 'w> MatchedString<'a> { + pub fn new( name_match: Option<NameMatch>, string: &'a str, diff --git a/src/display/mod.rs b/src/display/mod.rs index 4042da7..0f8f3c1 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -23,9 +23,7 @@ macro_rules! cond_bg { mod areas; mod col; -mod crop_writer; mod displayable_tree; -mod filling; mod git_status_display; pub mod flags_display; pub mod status_line; @@ -40,26 +38,15 @@ pub use { areas::Areas, col::*, cond_bg, - crop_writer::CropWriter, displayable_tree::DisplayableTree, - filling::*, git_status_display::GitStatusDisplay, matched_string::MatchedString, screen::Screen, cell_size::*, }; use { - crate::{ - errors::ProgramError, - }, - crossterm::{ - style::{ - Color, - SetBackgroundColor, - }, - QueueableCommand, - }, once_cell::sync::Lazy, + termimad::*, }; #[cfg(not(any(target_family="windows",target_os="android")))] @@ -67,9 +54,6 @@ pub use { permissions::PermWriter, }; -pub static TAB_REPLACEMENT: &str = " "; - -pub static SPACE_FILLING: Lazy<Filling> = Lazy::new(|| { Filling::from_char(' ') }); pub static BRANCH_FILLING: Lazy<Filling> = Lazy::new(|| { Filling::from_char('─') }); /// if true then the status of a panel covers the whole width @@ -83,13 +67,3 @@ pub type W = std::io::BufWriter<std::io::Stderr>; pub fn writer() -> W { std::io::BufWriter::new(std::io::stderr()) } - -pub fn fill_bg( - w: &mut W, - len: usize, - bg: Color, -) -> Result<(), ProgramError> { - w.queue(SetBackgroundColor(bg))?; - SPACE_FILLING.queue_unstyled(w, len)?; - Ok(()) -} diff --git a/src/filesystems/filesystems_state.rs b/src/filesystems/filesystems_state.rs index fdd201c..328b87f 100644 --- a/src/filesystems/filesystems_state.rs +++ b/src/filesystems/filesystems_state.rs @@ -25,7 +25,7 @@ use { path::Path, }, strict::NonEmptyVec, - termimad::ProgressBar, + termimad::*, }; struct FilteredContent { diff --git a/src/filesystems/mount_space_display.rs b/src/filesystems/mount_space_display.rs index 0b7f748..13871d7 100644 --- a/src/filesystems/mount_space_display.rs +++ b/src/filesystems/mount_space_display.rs @@ -1,6 +1,6 @@ use { crate::{ - display::{cond_bg, CropWriter}, + display::cond_bg, errors::ProgramError, filesystems::share_color, skin::StyleMap, @@ -10,7 +10,7 @@ use { QueueableCommand, }, lfs_core::Mount, - termimad::ProgressBar, + termimad::*, }; /// an abstract of the space info relative to a block device. diff --git a/src/hex/hex_view.rs b/src/hex/hex_view.rs index 07ebba0..8a7141a 100644 --- a/src/hex/hex_view.rs +++ b/src/hex/hex_view.rs @@ -2,7 +2,7 @@ use { super::byte::Byte, crate::{ command::ScrollCommand, - display::{CropWriter, Screen, SPACE_FILLING, W}, + display::{Screen, W}, errors::ProgramError, skin::PanelSkin, }, @@ -17,7 +17,7 @@ use { io, path::PathBuf, }, - termimad::{Area}, + termimad::{Area, CropWriter, SPACE_FILLING}, }; diff --git a/src/image/double_line.rs b/src/image/double_line.rs index 0a9aaeb..2b28af6 100644 --- a/src/image/double_line.rs +++ b/src/image/double_line.rs @@ -1,6 +1,6 @@ use { crate::{ - display::{fill_bg, W}, + display::W, errors::ProgramError, }, ansi_colours, @@ -14,6 +14,7 @@ use { QueueableCommand, }, image::Rgba, + termimad::fill_bg, }; const UPPER_HALF_BLOCK: char = '▀'; diff --git a/src/image/image_view.rs b/src/image/image_view.rs index f6a3f10..583e2a7 100644 --- a/src/image/image_view.rs +++ b/src/image/image_view.rs @@ -2,7 +2,7 @@ use { super::double_line::DoubleLine, crate::{ app::AppContext, - display::{fill_bg, Screen, W}, + display::{Screen, W}, errors::ProgramError, skin::PanelSkin, }, @@ -21,7 +21,7 @@ use { imageops::FilterType, }, std::path::{Path, PathBuf}, - termimad::Area, + termimad::{fill_bg, Area}, }; /// an already resized image, with the dimensions it diff --git a/src/preview/preview.rs b/src/preview/preview.rs index 47a6812..f76012b 100644 --- a/src/preview/preview.rs +++ b/src/preview/preview.rs @@ -17,7 +17,7 @@ use { io, path::Path, }, - termimad::Area, + termimad::{Area, CropWriter, SPACE_FILLING}, }; pub enum Preview { diff --git a/src/preview/preview_state.rs b/src/preview/preview_state.rs index aa47316..34a5aae 100644 --- a/src/preview/preview_state.rs +++ b/src/preview/preview_state.rs @@ -3,7 +3,7 @@ use { crate::{ app::*, command::{Command, ScrollCommand, TriggerType}, - display::{CropWriter, Screen, SPACE_FILLING, W}, + display::{Screen, W}, errors::ProgramError, flag::Flag, pattern::InputPattern, @@ -17,7 +17,7 @@ use { QueueableCommand, }, std::path::{Path, PathBuf}, - termimad::Area, + termimad::{Area, CropWriter, SPACE_FILLING}, }; /// an application state dedicated to previewing files. diff --git a/src/preview/zero_len_file_view.rs b/src/preview/zero_len_file_view.rs index d150eab..212a256 100644 --- a/src/preview/zero_len_file_view.rs +++ b/src/preview/zero_len_file_view.rs @@ -1,6 +1,6 @@ use { crate::{ - display::{CropWriter, Screen, SPACE_FILLING, W}, + display::{Screen, W}, errors::ProgramError, skin::PanelSkin, }, @@ -13,7 +13,7 @@ use { fs::File, path::PathBuf, }, - termimad::{Area}, + termimad::{Area, CropWriter, SPACE_FILLING}, }; /// a (light) display for a file declaring a size 0, diff --git a/src/stage/stage_state.rs b/src/stage/stage_state.rs index 4dc5ed4..b734f85 100644 --- a/src/stage/stage_state.rs +++ b/src/stage/stage_state.rs @@ -3,7 +3,7 @@ use { crate::{ app::*, command::*, - display::{CropWriter, MatchedString, Screen, SPACE_FILLING, W}, + display::{MatchedString, Screen, W}, errors::ProgramError, pattern::*, skin::*, @@ -16,7 +16,7 @@ use { QueueableCommand, }, std::path::{Path}, - termimad::Area, + termimad::{Area, CropWriter, SPACE_FILLING}, unicode_width::{UnicodeWidthChar, UnicodeWidthStr}, }; diff --git a/src/syntactic/syntactic_view.rs b/src/syntactic/syntactic_view.rs index d5854f5..2a38466 100644 --- a/src/syntactic/syntactic_view.rs +++ b/src/syntactic/syntactic_view.rs @@ -3,7 +3,7 @@ use { crate::{ app::{AppContext, LineNumber}, command::{ScrollCommand, move_sel}, - display::{CropWriter, Screen, SPACE_FILLING, W}, + display::{Screen, W}, errors::*, pattern::{InputPattern, NameMatch}, skin::PanelSkin, @@ -23,7 +23,7 @@ use { str, }, syntect::highlighting::Style, - termimad::Area, + termimad::{Area, CropWriter, SPACE_FILLING}, }; /// a homogeneously colored piece of a line |