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 | |
parent | ee354f9a7a2684783456b10707462f71045b1c3e (diff) |
move some fit/crop utilities from broot's code to termimad
-rw-r--r-- | Cargo.lock | 14 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rwxr-xr-x | compile-all-targets.sh | 2 | ||||
-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 |
17 files changed, 30 insertions, 257 deletions
@@ -151,7 +151,7 @@ dependencies = [ "clap", "cli-log 2.0.0", "crossbeam", - "crossterm 0.20.0", + "crossterm 0.21.0", "custom_error", "deser-hjson", "directories", @@ -181,7 +181,7 @@ dependencies = [ "strict", "syntect", "tempfile", - "termimad 0.14.0", + "termimad 0.15.0", "terminal-clipboard", "toml", "umask", @@ -397,9 +397,9 @@ dependencies = [ [[package]] name = "crossterm" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0ebde6a9dd5e331cd6c6f48253254d117642c31653baa475e394657c59c1f7d" +checksum = "486d44227f71a1ef39554c0dc47e44b9f4139927c75043312690c3f476d1d788" dependencies = [ "bitflags", "crossterm_winapi 0.8.0", @@ -1612,12 +1612,12 @@ dependencies = [ [[package]] name = "termimad" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72cc07d71829847df3a04fe2341f0598676b1faf377d0965c48cb922241273d7" +checksum = "513d1d28d91be635bdca58c59270dd54ae2280a7bf8a66c49a9963d2b6f112cd" dependencies = [ "crossbeam", - "crossterm 0.20.0", + "crossterm 0.21.0", "minimad 0.8.0", "thiserror", "unicode-width", @@ -27,7 +27,7 @@ clap = { version="2.33", default-features=false, features=["suggestions", "color cli-log = "2.0" chrono = "0.4" crossbeam = "0.8" -crossterm = "0.20.0" +crossterm = "0.21.0" custom_error = "1.6" deser-hjson = "1.0.2" directories = "3.0" @@ -54,7 +54,7 @@ splitty = "0.1" strict = "0.1.4" syntect = "4.5" tempfile = "3.2" -termimad = "0.14.0" +termimad = "0.15.0" terminal-clipboard = { version = "0.2.1", optional = true } toml = "0.5" umask = "1.0" diff --git a/compile-all-targets.sh b/compile-all-targets.sh index 0705648..eb79004 100755 --- a/compile-all-targets.sh +++ b/compile-all-targets.sh @@ -61,7 +61,7 @@ cp target/armv7-unknown-linux-gnueabihf/release/broot build/armv7-unknown-linux- # build the Android version # use cargo cross echo -e "${H2}Compiling the Android version${EH}" -cross build ---features "clipboard" --target aarch64-linux-android --release +cross build --features "clipboard" --target aarch64-linux-android --release mkdir build/aarch64-linux-android cp target/aarch64-linux-android/release/broot build/aarch64-linux-android/ 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 |