mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
align_text_center as part of utils
This commit is contained in:
@@ -28,7 +28,7 @@ use super::{
|
||||
Context, DialogYesNoOption, FileExplorerTab, FileTransferActivity, FsEntry, InputField,
|
||||
InputMode, LogLevel, LogRecord, PopupType,
|
||||
};
|
||||
use crate::utils::time_to_str;
|
||||
use crate::utils::{align_text_center, time_to_str};
|
||||
|
||||
use bytesize::ByteSize;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -335,7 +335,7 @@ impl FileTransferActivity {
|
||||
let mut lines: Vec<ListItem> = Vec::new();
|
||||
for msg in message_rows.iter() {
|
||||
lines.push(ListItem::new(Spans::from(
|
||||
FileTransferActivity::align_text_center(msg, width),
|
||||
align_text_center(msg, width),
|
||||
)));
|
||||
}
|
||||
List::new(lines)
|
||||
@@ -358,7 +358,7 @@ impl FileTransferActivity {
|
||||
let mut lines: Vec<ListItem> = Vec::new();
|
||||
for msg in message_rows.iter() {
|
||||
lines.push(ListItem::new(Spans::from(
|
||||
FileTransferActivity::align_text_center(msg, width),
|
||||
align_text_center(msg, width),
|
||||
)));
|
||||
}
|
||||
List::new(lines)
|
||||
@@ -416,7 +416,7 @@ impl FileTransferActivity {
|
||||
let mut lines: Vec<ListItem> = Vec::new();
|
||||
for msg in message_rows.iter() {
|
||||
lines.push(ListItem::new(Spans::from(
|
||||
FileTransferActivity::align_text_center(msg, width),
|
||||
align_text_center(msg, width),
|
||||
)));
|
||||
}
|
||||
List::new(lines)
|
||||
@@ -797,21 +797,6 @@ impl FileTransferActivity {
|
||||
.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 working directory path if longer than width + host.len
|
||||
|
||||
26
src/utils.rs
26
src/utils.rs
@@ -25,6 +25,7 @@
|
||||
|
||||
// Dependencies
|
||||
extern crate chrono;
|
||||
extern crate textwrap;
|
||||
extern crate whoami;
|
||||
|
||||
use crate::filetransfer::FileTransferProtocol;
|
||||
@@ -241,6 +242,23 @@ pub fn lstime_to_systime(
|
||||
.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)]
|
||||
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 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!")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user