From 75b9b52e25c81e72acbd1093064bbde471216bf1 Mon Sep 17 00:00:00 2001 From: Tilo K Date: Fri, 16 May 2025 21:56:47 +0200 Subject: [PATCH] feat: improve list command --- .idea/.gitignore | 8 +++++++ .idea/encodings.xml | 4 ++++ .idea/inspectionProfiles/Project_Default.xml | 5 ++++ .idea/modules.xml | 8 +++++++ .idea/pastebin-cli.iml | 11 +++++++++ .idea/vcs.xml | 6 +++++ src/main.rs | 25 ++++++++++++++++---- 7 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/pastebin-cli.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..7e5b7d7 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8d66637 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c5a757b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pastebin-cli.iml b/.idea/pastebin-cli.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/pastebin-cli.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 93d1e41..77d3c5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use clap::{Parser, Subcommand}; use std::fs; use std::path::PathBuf; - mod keys; mod pastebin; @@ -79,17 +78,33 @@ async fn list(max_results: Option) { let line = "-".repeat(5); println!("{}Top {} pastes{}", line, max_results.unwrap_or(10), line); + let mut max_length = 8; + let max = resp.iter().map(|paste| paste.paste_title.len()).max(); + if max.is_some() { + if max.unwrap() > max_length { + max_length = max.unwrap(); + } + } + for paste in resp { let mut title = paste.paste_title; if title == "" { title = "Untitled".to_owned(); } - let naive = NaiveDateTime::from_timestamp_opt(paste.paste_date.into(), 0).unwrap(); + title = pad_string(&title, max_length); - let datetime: DateTime = DateTime::from_utc(naive, Utc); - let newdate = datetime.format("%Y-%m-%d %H:%M:%S"); + let datetime = DateTime::from_timestamp(paste.paste_date.into(), 0).unwrap(); + let formatted_date = datetime.format("%Y-%m-%d %H:%M:%S"); - println!("{}\t{}\t{}", title, newdate, paste.paste_url); + println!("{}\t{}\t{}", title, formatted_date, paste.paste_url); } } + +fn pad_string(s: &str, length: usize) -> String { + let mut s = s.to_owned(); + while s.len() < length { + s.push(' '); + } + s +}