summaryrefslogtreecommitdiffstats
path: root/src/layout.rs
diff options
context:
space:
mode:
authorkyoheiu <kyoheiu@outlook.com>2022-08-29 06:09:58 +0900
committerkyoheiu <kyoheiu@outlook.com>2022-08-29 06:09:58 +0900
commitcd232c829df19ac0d9a7b2e5bb986684958936b2 (patch)
tree469da3a1590bc5c757dcc799b880b9aeb21abe9a /src/layout.rs
parentba9fe5def10c372c00f36320aa2962848ac20036 (diff)
Refactor functions around the layout
Diffstat (limited to 'src/layout.rs')
-rw-r--r--src/layout.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/layout.rs b/src/layout.rs
index fba0a09..2f22b50 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -13,6 +13,10 @@ pub const IMAGE_EXTENSION: [&str; 20] = [
pub const CHAFA_WARNING: &str =
"From v1.1.0, the image preview needs chafa. For more details, please see help by `:h` ";
+pub const PROPER_WIDTH: u16 = 28;
+pub const TIME_WIDTH: u16 = 16;
+pub const DEFAULT_NAME_LENGTH: u16 = 30;
+
#[derive(Debug, Clone)]
pub struct Layout {
pub y: u16,
@@ -177,6 +181,55 @@ impl Layout {
}
}
+/// Make app's layout according to terminal width and app's config.
+pub fn make_layout(
+ column: u16,
+ use_full: Option<bool>,
+ name_length: Option<usize>,
+) -> (u16, usize) {
+ let mut time_start: u16;
+ let mut name_max: usize;
+
+ if column < PROPER_WIDTH {
+ time_start = column;
+ name_max = (column - 2).into();
+ (time_start, name_max)
+ } else {
+ match use_full {
+ Some(true) | None => {
+ time_start = column - TIME_WIDTH;
+ name_max = (time_start - SPACES).into();
+ }
+ Some(false) => match name_length {
+ Some(option_max) => {
+ time_start = option_max as u16 + SPACES;
+ name_max = option_max;
+ }
+ None => {
+ time_start = if column >= DEFAULT_NAME_LENGTH + TIME_WIDTH + SPACES {
+ DEFAULT_NAME_LENGTH + SPACES
+ } else {
+ column - TIME_WIDTH
+ };
+ name_max = if column >= DEFAULT_NAME_LENGTH + TIME_WIDTH + SPACES {
+ DEFAULT_NAME_LENGTH.into()
+ } else {
+ (time_start - SPACES).into()
+ };
+ }
+ },
+ }
+ let required = time_start + TIME_WIDTH - 1;
+ if required > column {
+ let diff = required - column;
+ name_max -= diff as usize;
+ time_start -= diff;
+ }
+
+ (time_start, name_max)
+ }
+}
+
/// Check preview type.
fn check_preview_type(item: &ItemInfo) -> PreviewType {
if item.file_size > MAX_SIZE_TO_PREVIEW {