moltbook_cli/display/
utils.rs1use chrono::{DateTime, Utc};
2use colored::*;
3use terminal_size::{Width, terminal_size};
4
5pub fn get_term_width() -> usize {
12 if let Some(width) = std::env::var("COLUMNS")
13 .ok()
14 .and_then(|c| c.parse::<usize>().ok())
15 {
16 return width.saturating_sub(2).max(40);
17 }
18
19 if let Some((Width(w), _)) = terminal_size() {
20 (w as usize).saturating_sub(2).max(40)
21 } else {
22 80
23 }
24}
25
26pub fn relative_time(timestamp: &str) -> String {
30 if let Ok(dt) = DateTime::parse_from_rfc3339(timestamp) {
31 let now = Utc::now();
32 let diff = now.signed_duration_since(dt);
33
34 if diff.num_seconds() < 60 {
35 "just now".to_string()
36 } else if diff.num_minutes() < 60 {
37 format!("{}m ago", diff.num_minutes())
38 } else if diff.num_hours() < 24 {
39 format!("{}h ago", diff.num_hours())
40 } else if diff.num_days() < 7 {
41 format!("{}d ago", diff.num_days())
42 } else {
43 dt.format("%Y-%m-%d").to_string()
44 }
45 } else {
46 timestamp.to_string()
47 }
48}
49
50pub fn success(msg: &str) {
52 println!("{} {}", "✅".green(), msg.bright_green());
53}
54
55pub fn error(msg: &str) {
57 eprintln!("{} {}", "❌".red().bold(), msg.bright_red());
58}
59
60pub fn info(msg: &str) {
62 println!("{} {}", "ℹ️ ".cyan(), msg.bright_cyan());
63}
64
65pub fn warn(msg: &str) {
67 println!("{} {}", "⚠️ ".yellow(), msg.bright_yellow());
68}