Skip to content

Commit

Permalink
add path description
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Dec 20, 2023
1 parent 4a48651 commit 9587993
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ itertools = "0.12.0"
reqwest = "0.11.22"
tokio = { version = "1", features = ["full"] }
walkdir = "2.4.0"
once_cell = "1.19.0"
41 changes: 31 additions & 10 deletions src/utils/directory2md.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
use rust_i18n::t;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{Result, Write};
use std::path::Path;
use walkdir::WalkDir;

// 使用 once_cell 创建静态的 HashMap,存储全路径和描述
static PATH_DESCRIPTIONS: Lazy<HashMap<String, &'static str>> = Lazy::new(|| {
let mut m = HashMap::new();
m.insert("/Cargo.toml".to_string(), "这是 Cargo.toml 的描述");
m.insert(
"/migrations".to_string(),
"这是 migrations 目录的描述",
);
m
});

pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result<()> {
let mut file = File::create(output_file)?;
let project_name = output_file
Expand All @@ -13,25 +25,34 @@ pub fn write_directory_contents_to_markdown(output_file: &Path) -> Result<()> {
.unwrap_or_default()
.to_string_lossy();
writeln!(file, "# {}", project_name)?;

for entry in WalkDir::new(output_file.parent().unwrap())
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path() != Path::new("./READEME.md"))
// Exclude the output file itself
.filter(|e| e.file_name().to_string_lossy() != "README.md")
{
let depth = entry.depth();
let indent = " ".repeat(depth.saturating_sub(1)); // Markdown uses 4 spaces for each list level
let indent = " ".repeat(depth.saturating_sub(1));
let path = entry.path();
let metadata = fs::metadata(path)?;
dbg!(&path);
if let Some(file_name) = path.file_name() {
let file_name = file_name.to_string_lossy();
let file_name_str = file_name.to_string_lossy();
let full_path = path.to_string_lossy().into_owned();
let full_path = full_path.trim_start_matches(&*project_name);
dbg!(&full_path);
let description = PATH_DESCRIPTIONS.get(full_path).unwrap_or(&"");
if metadata.is_dir() {
// Use `**Dir:**` to denote directories
writeln!(file, "{}- **{}:** {}", indent, t!("dir"), file_name)?;
writeln!(
file,
"{}- **Dir:** {} {}",
indent, file_name_str, description
)?;
} else {
// Use `*File:*` to denote files
writeln!(file, "{}- *{}:* {}", indent, t!("file"), file_name)?;
writeln!(
file,
"{}- *File:* {} {}",
indent, file_name_str, description
)?;
}
}
}
Expand Down

0 comments on commit 9587993

Please sign in to comment.