]> git.r.bdr.sh - rbdr/blog/commitdiff
Add blog status, correctly shift
authorRuben Beltran del Rio <redacted>
Mon, 26 Feb 2024 11:44:11 +0000 (11:44 +0000)
committerRuben Beltran del Rio <redacted>
Mon, 26 Feb 2024 11:44:11 +0000 (11:44 +0000)
src/command/add.rs
src/command/status/blog_status.rs [new file with mode: 0644]
src/command/status/mod.rs

index dbcc512c5f343199345785eaaf1255f503c0f37b..02da0fb370f74d7b2936089758eae08fe5e73aa6 100644 (file)
@@ -1,4 +1,4 @@
-use std::fs::{create_dir_all, rename};
+use std::fs::{create_dir_all, remove_dir_all, rename};
 use std::io::Result;
 use super::{
     generate::Generate,
@@ -23,10 +23,13 @@ impl super::Command for Add {
 
     fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
         create_dir_all(&configuration.posts_directory)?;
-        for i in (0..configuration.max_posts).rev() {
+        for i in (0..configuration.max_posts - 1).rev() {
             let source = configuration.posts_directory.join(i.to_string());
             let target = configuration.posts_directory.join((i + 1).to_string());
 
+            if target.exists() {
+                remove_dir_all(&target)?;
+            }
             if source.exists() {
                 rename(&source, &target)?;
             }
diff --git a/src/command/status/blog_status.rs b/src/command/status/blog_status.rs
new file mode 100644 (file)
index 0000000..df5256e
--- /dev/null
@@ -0,0 +1,24 @@
+use std::fs::read_dir;
+use std::path::PathBuf;
+use crate::configuration::Configuration;
+
+pub fn status(configuration: &Configuration) -> String {
+    let mut status_message = String::new();
+
+    status_message.push_str("# Blog\n");
+
+    // Main Configuration Locations
+    let blog_count = count_entries(&configuration.posts_directory);
+    status_message.push_str(&format!("Number of posts in blog: {}\n", blog_count));
+
+    let archive_count = count_entries(&configuration.archive_directory);
+    status_message.push_str(&format!("Number of posts in archive: {}\n", archive_count));
+    status_message
+}
+
+fn count_entries(path: &PathBuf) -> String {
+    match read_dir(path) {
+        Ok(entries) => entries.filter_map(Result::ok).count().to_string(),
+        Err(_) => "0".to_string()
+    }
+}
index e8754f59eadb5e334c08c5f39c61fda299043efc..e620f78f8c8615fbe69c651a04552de28bec59dc 100644 (file)
@@ -1,4 +1,5 @@
 mod configuration_status;
+mod blog_status;
 
 use std::io::Result;
 use crate::configuration::Configuration;
@@ -19,7 +20,7 @@ impl super::Command for Status {
     fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
         let status_providers = available_status_providers();
         for status_provider in status_providers {
-            println!("{}", status_provider(configuration));
+            println!("{}\n----\n", status_provider(configuration));
         }
         return Ok(())
     }
@@ -40,5 +41,6 @@ impl super::Command for Status {
 fn available_status_providers() -> Vec<fn(&Configuration) -> String> {
     vec![
         configuration_status::status,
+        blog_status::status,
     ]
 }