From e839bb827497a60856489825df78eceef91c7e4e Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 4 Mar 2019 12:34:50 -0800 Subject: [PATCH] Add Printer::{shrinked,cropped}_centered --- CHANGELOG.md | 1 + src/printer.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68cccca..38aeae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ `Cursive::cb_sink()` - Add `LinearLayout::{insert_child, swap_children, set_weight}` for more in-place modifications. +- Add `Printer::{cropped_centered,shrinked_centered}` ### Improvements diff --git a/src/printer.rs b/src/printer.rs index 1015327..1862f01 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -461,7 +461,7 @@ impl<'a, 'b> Printer<'a, 'b> { /// Returns a sub-printer with the given offset. /// /// It will print in an area slightly to the bottom/right. - pub fn offset(&self, offset: S) -> Printer<'_, '_> + pub fn offset(&self, offset: S) -> Self where S: Into, { @@ -517,6 +517,24 @@ impl<'a, 'b> Printer<'a, 'b> { }) } + /// Returns a new sub-printer with a cropped area. + /// + /// The new printer size will be the minimum of `size` and its current size. + /// + /// The view will stay centered. + /// + /// Note that if shrinking by an odd number, the view will round to the top-left. + pub fn cropped_centered(&self, size: S) -> Self + where + S: Into, + { + let size = size.into(); + let borders = self.size.saturating_sub(size); + let half_borders = borders / 2; + + self.cropped(size).offset(half_borders) + } + /// Returns a new sub-printer with a shrinked area. /// /// The printer size will be reduced by the given border from the bottom-right. @@ -527,6 +545,21 @@ impl<'a, 'b> Printer<'a, 'b> { self.cropped(self.size.saturating_sub(borders)) } + /// Returns a new sub-printer with a shrinked area. + /// + /// The printer size will be reduced by the given border, and will stay centered. + /// + /// Note that if shrinking by an odd number, the view will round to the top-left. + pub fn shrinked_centered(&self, borders: S) -> Self + where + S: Into, + { + let borders = borders.into(); + let half_borders = borders / 2; + + self.shrinked(borders).offset(half_borders) + } + /// Returns a new sub-printer with a content offset. pub fn content_offset(&self, offset: S) -> Self where