diff options
author | qkzk <qu3nt1n@gmail.com> | 2024-01-21 22:03:00 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2024-01-21 22:03:00 +0100 |
commit | de24e10998c66e3bdedb29a18cca04f9410267ea (patch) | |
tree | 3f5441dfa8f5f639760b5022c9bef2bf3c21b6a1 | |
parent | 3de0b2d851be10adb9ffc6ebda194a5e6319edbd (diff) |
doc a header improvement
-rw-r--r-- | src/app/header_footer.rs | 42 | ||||
-rw-r--r-- | src/io/display.rs | 7 |
2 files changed, 34 insertions, 15 deletions
diff --git a/src/app/header_footer.rs b/src/app/header_footer.rs index 6f05865..0eb430f 100644 --- a/src/app/header_footer.rs +++ b/src/app/header_footer.rs @@ -12,6 +12,12 @@ mod inner { Selectable, TextKind, }; + #[derive(Clone, Copy)] + pub enum HorizontalAlign { + Left, + Right, + } + /// A footer or header element that can be clicked /// /// Holds a text and an action. @@ -25,6 +31,10 @@ mod inner { } impl ClickableString { + /// Creates a new `ClickableString`. + /// It calculates its position with `col` and `align`. + /// If left aligned, the text size will be added to `col` and the text will span from col to col + width. + /// otherwise, the text will spawn from col - width to col. fn new(text: String, align: HorizontalAlign, action: ActionMap, col: usize) -> Self { let size = Self::size(&text); let left: usize; @@ -58,10 +68,12 @@ mod inner { self.right - self.left } + /// Text content of the element. pub fn text(&self) -> &str { self.text.as_str() } + /// Starting col where the text should be drawn on the canvas. pub fn col(&self) -> usize { self.left } @@ -69,6 +81,7 @@ mod inner { /// A line of element that can be clicked on. pub trait ClickableLine { + /// Reference to the elements fn elems(&self) -> &Vec<ClickableString>; /// Action for each associated file. fn action(&self, col: usize, is_right: bool) -> &ActionMap { @@ -83,8 +96,11 @@ mod inner { crate::log_info!("no action found"); &ActionMap::Nothing } + /// full width of the terminal fn width(&self) -> usize; - + /// used offset. + /// 1 if the text is on left tab, + /// width / 2 + 2 otherwise. fn offset(&self, is_right: bool) -> usize { if is_right { self.width() / 2 + 2 @@ -101,6 +117,7 @@ mod inner { } impl Header { + /// Creates a new header pub fn new(status: &Status, tab: &Tab) -> Result<Self> { let (width, _) = status.internal_settings.term_size()?; let elems = Self::make_elems(tab, width)?; @@ -193,6 +210,7 @@ mod inner { } } + /// Default footer for display directory & tree. pub struct Footer { elems: Vec<ClickableString>, width: usize, @@ -217,6 +235,7 @@ mod inner { ActionMap::Sort, ]; + /// Creates a new footer pub fn new(status: &Status, tab: &Tab) -> Result<Self> { let (width, _) = status.internal_settings.term_size()?; let elems = Self::make_elems(status, tab, width)?; @@ -267,7 +286,6 @@ mod inner { let available_width = total_width.checked_sub(used_width).unwrap_or_default(); let margin_width = available_width / (2 * raw_strings.len()); let margin = " ".repeat(margin_width); - let rest = total_width - (used_width + margin_width * 2 * raw_strings.len()); raw_strings .iter() .map(|content| format!("{margin}{content}{margin}")) @@ -310,6 +328,7 @@ mod inner { } } + /// Header for the display of flagged files pub struct FlaggedHeader { elems: Vec<ClickableString>, width: usize, @@ -323,10 +342,12 @@ mod inner { self.width } } + impl FlaggedHeader { const ACTIONS: [ActionMap; 3] = [ActionMap::ResetMode, ActionMap::OpenFile, ActionMap::Search]; + /// Creates a new header. pub fn new(status: &Status) -> Result<Self> { let (width, _) = status.internal_settings.term.term_size()?; let elems = Self::make_elems(status, width); @@ -360,6 +381,7 @@ mod inner { } } + /// Footer for the flagged files display pub struct FlaggedFooter { elems: Vec<ClickableString>, width: usize, @@ -377,6 +399,7 @@ mod inner { impl FlaggedFooter { const ACTIONS: [ActionMap; 2] = [ActionMap::Nothing, ActionMap::DisplayFlagged]; + /// Creates a new footer pub fn new(status: &Status) -> Result<Self> { let (width, _) = status.internal_settings.term.term_size()?; let used_width = if status.display_settings.use_dual_tab(width) { @@ -420,12 +443,6 @@ mod inner { } } - #[derive(Clone, Copy)] - pub enum HorizontalAlign { - Left, - Right, - } - pub struct PreviewHeader; impl PreviewHeader { @@ -434,7 +451,7 @@ mod inner { Self::pair_to_clickable(&pairs, width) } - pub fn pair_to_clickable( + fn pair_to_clickable( pairs: &[(String, HorizontalAlign)], width: usize, ) -> Vec<ClickableString> { @@ -514,7 +531,7 @@ mod inner { } } - pub fn make_default_preview(status: &Status, tab: &Tab) -> Vec<(String, HorizontalAlign)> { + fn make_default_preview(status: &Status, tab: &Tab) -> Vec<(String, HorizontalAlign)> { if let Ok(fileinfo) = Self::_pick_previewed_fileinfo(status) { let mut strings = vec![(" Preview ".to_owned(), HorizontalAlign::Left)]; if !tab.preview.is_empty() { @@ -536,6 +553,11 @@ mod inner { vec![("".to_owned(), HorizontalAlign::Left)] } } + + /// Make a default preview header + pub fn default_preview(status: &Status, tab: &Tab, width: usize) -> Vec<ClickableString> { + Self::pair_to_clickable(&Self::make_default_preview(status, tab), width) + } } } diff --git a/src/io/display.rs b/src/io/display.rs index dd280e8..eb8ee95 100644 --- a/src/io/display.rs +++ b/src/io/display.rs @@ -7,10 +7,10 @@ use tuikit::attr::{Attr, Color}; use tuikit::prelude::*; use tuikit::term::Term; +use crate::app::Footer; use crate::app::Status; use crate::app::Tab; use crate::app::{ClickableLine, ClickableString, FlaggedFooter, FlaggedHeader}; -use crate::app::{Focus, Footer}; use crate::app::{Header, PreviewHeader}; use crate::common::path_to_string; use crate::common::ENCRYPTED_DEVICE_BINDS; @@ -511,10 +511,7 @@ impl<'a> WinMain<'a> { draw_clickable_strings( 0, 0, - &PreviewHeader::pair_to_clickable( - &PreviewHeader::make_default_preview(self.status, tab), - width, - ), + &PreviewHeader::default_preview(self.status, tab, width), canvas, false, )?; |