commit 968314685113fc34b68b47f85e662b21444071ab
parent bd7b542048e0a52900b838782b2a95bbddedd7d6
Author: Matt Brubeck <mbrubeck@limpet.net>
Date: Wed, 30 Dec 2020 20:54:55 -0800
Print directory listings in alphabetical order
Diffstat:
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -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"))?;
@@ -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(())
}