Skip to content

Commit

Permalink
Print directory listings in alphabetical order
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Dec 31, 2020
1 parent bd7b542 commit 9683146
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ async fn list_directory<W: Write + Unpin>(stream: &mut W, path: &Path) -> Result
log::info!("Listing directory {:?}", path);
send_text_gemini_header(stream).await?;
let mut entries = async_std::fs::read_dir(path).await?;
let mut lines = vec![];
while let Some(entry) = entries.next().await {
let entry = entry?;
let mut name = entry.file_name().into_string().or(Err("Non-Unicode filename"))?;
Expand All @@ -238,9 +239,11 @@ async fn list_directory<W: Write + Unpin>(stream: &mut W, path: &Path) -> Result
if entry.file_type().await?.is_dir() {
name += "/";
}
stream.write_all(b"=> ").await?;
stream.write_all(name.as_bytes()).await?;
stream.write_all(b"\n").await?;
lines.push(format!("=> {}\n", name));
}
lines.sort();
for line in lines {
stream.write_all(line.as_bytes()).await?;
}
Ok(())
}
Expand Down

0 comments on commit 9683146

Please sign in to comment.