align_text_center as part of utils

This commit is contained in:
ChristianVisintin
2020-12-14 13:33:24 +01:00
parent e9d5093a52
commit c493ba45a8
2 changed files with 30 additions and 19 deletions

View File

@@ -28,7 +28,7 @@ use super::{
Context, DialogYesNoOption, FileExplorerTab, FileTransferActivity, FsEntry, InputField, Context, DialogYesNoOption, FileExplorerTab, FileTransferActivity, FsEntry, InputField,
InputMode, LogLevel, LogRecord, PopupType, InputMode, LogLevel, LogRecord, PopupType,
}; };
use crate::utils::time_to_str; use crate::utils::{align_text_center, time_to_str};
use bytesize::ByteSize; use bytesize::ByteSize;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -335,7 +335,7 @@ impl FileTransferActivity {
let mut lines: Vec<ListItem> = Vec::new(); let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() { for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from( lines.push(ListItem::new(Spans::from(
FileTransferActivity::align_text_center(msg, width), align_text_center(msg, width),
))); )));
} }
List::new(lines) List::new(lines)
@@ -358,7 +358,7 @@ impl FileTransferActivity {
let mut lines: Vec<ListItem> = Vec::new(); let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() { for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from( lines.push(ListItem::new(Spans::from(
FileTransferActivity::align_text_center(msg, width), align_text_center(msg, width),
))); )));
} }
List::new(lines) List::new(lines)
@@ -416,7 +416,7 @@ impl FileTransferActivity {
let mut lines: Vec<ListItem> = Vec::new(); let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() { for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from( lines.push(ListItem::new(Spans::from(
FileTransferActivity::align_text_center(msg, width), align_text_center(msg, width),
))); )));
} }
List::new(lines) List::new(lines)
@@ -797,21 +797,6 @@ impl FileTransferActivity {
.start_corner(Corner::TopLeft) .start_corner(Corner::TopLeft)
} }
/// align_text_center
///
/// Align text to center for a given width
fn align_text_center(text: &str, width: u16) -> String {
let indent_size: usize = match (width as usize) >= text.len() {
// NOTE: The check prevents underflow
true => (width as usize - text.len()) / 2,
false => 0,
};
textwrap::indent(
text,
(0..indent_size).map(|_| " ").collect::<String>().as_str(),
)
}
/// ### elide_wrkdir_path /// ### elide_wrkdir_path
/// ///
/// Elide working directory path if longer than width + host.len /// Elide working directory path if longer than width + host.len

View File

@@ -25,6 +25,7 @@
// Dependencies // Dependencies
extern crate chrono; extern crate chrono;
extern crate textwrap;
extern crate whoami; extern crate whoami;
use crate::filetransfer::FileTransferProtocol; use crate::filetransfer::FileTransferProtocol;
@@ -241,6 +242,23 @@ pub fn lstime_to_systime(
.unwrap_or(SystemTime::UNIX_EPOCH)) .unwrap_or(SystemTime::UNIX_EPOCH))
} }
/// align_text_center
///
/// Align text to center for a given width
pub fn align_text_center(text: &str, width: u16) -> String {
let indent_size: usize = match (width as usize) >= text.len() {
// NOTE: The check prevents underflow
true => (width as usize - text.len()) / 2,
false => 0,
};
textwrap::indent(
text,
(0..indent_size).map(|_| " ").collect::<String>().as_str(),
)
.trim_end()
.to_string()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@@ -392,4 +410,12 @@ mod tests {
assert!(lstime_to_systime("Feb 31 2018", "%b %d %Y", "%b %d %H:%M").is_err()); assert!(lstime_to_systime("Feb 31 2018", "%b %d %Y", "%b %d %H:%M").is_err());
assert!(lstime_to_systime("Feb 15 25:32", "%b %d %Y", "%b %d %H:%M").is_err()); assert!(lstime_to_systime("Feb 15 25:32", "%b %d %Y", "%b %d %H:%M").is_err());
} }
#[test]
fn test_utils_align_text_center() {
assert_eq!(
align_text_center("hello world!", 24),
String::from(" hello world!")
);
}
} }