From: Ruben Beltran del Rio Date: Mon, 26 Feb 2024 11:44:11 +0000 (+0000) Subject: Add blog status, correctly shift X-Git-Tag: 7.0.0~46 X-Git-Url: https://git.r.bdr.sh/rbdr/blog/commitdiff_plain/0e276d039b8613de0cbd302bf328bb660ab063b6?hp=606f82c76540a9d80366fdb943c06abe525f662e Add blog status, correctly shift --- diff --git a/src/command/add.rs b/src/command/add.rs index dbcc512..02da0fb 100644 --- a/src/command/add.rs +++ b/src/command/add.rs @@ -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 index 0000000..df5256e --- /dev/null +++ b/src/command/status/blog_status.rs @@ -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() + } +} diff --git a/src/command/status/mod.rs b/src/command/status/mod.rs index e8754f5..e620f78 100644 --- a/src/command/status/mod.rs +++ b/src/command/status/mod.rs @@ -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 String> { vec![ configuration_status::status, + blog_status::status, ] }