summaryrefslogtreecommitdiffstats
path: root/src/display
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2021-08-29 21:21:47 +0200
committerCanop <cano.petrole@gmail.com>2021-08-29 21:21:47 +0200
commitd6ceba05642982674ee80bd8bccd25980dab9e8c (patch)
treefd0626430a4a2fb25d33ae56f8ec6b4923ae200a /src/display
parentee354f9a7a2684783456b10707462f71045b1c3e (diff)
move some fit/crop utilities from broot's code to termimad
Diffstat (limited to 'src/display')
-rw-r--r--src/display/crop_writer.rs140
-rw-r--r--src/display/filling.rs63
-rw-r--r--src/display/matched_string.rs1
-rw-r--r--src/display/mod.rs28
4 files changed, 2 insertions, 230 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(())
-}