Mastui/src/utils.rs

54 lines
1.4 KiB
Rust

use chrono::{DateTime, Utc};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
pub fn format_time(time: &DateTime<Utc>) -> String {
let now = Utc::now();
let duration = now.signed_duration_since(*time);
if duration.num_days() > 0 {
format!("{}d", duration.num_days())
} else if duration.num_hours() > 0 {
format!("{}h", duration.num_hours())
} else if duration.num_minutes() > 0 {
format!("{}m", duration.num_minutes())
} else {
format!("{}s", duration.num_seconds().max(0))
}
}
/// Common key patterns for navigation
pub fn is_exit_key(key: KeyEvent) -> bool {
matches!((key.code, key.modifiers),
(KeyCode::Esc, _) |
(KeyCode::Char('q'), _) |
(KeyCode::Char('c'), KeyModifiers::CONTROL)
)
}
pub fn is_up_key(key: KeyEvent) -> bool {
matches!((key.code, key.modifiers),
(KeyCode::Up, _) |
(KeyCode::Char('k'), _)
)
}
pub fn is_down_key(key: KeyEvent) -> bool {
matches!((key.code, key.modifiers),
(KeyCode::Down, _) |
(KeyCode::Char('j'), _)
)
}
pub fn is_home_key(key: KeyEvent) -> bool {
matches!((key.code, key.modifiers),
(KeyCode::Home, _) |
(KeyCode::Char('g'), _)
)
}
pub fn is_end_key(key: KeyEvent) -> bool {
matches!((key.code, key.modifiers),
(KeyCode::End, _) |
(KeyCode::Char('G'), _)
)
}