From acda290c4b0b04ca1eea862bcf8eb518d626e998 Mon Sep 17 00:00:00 2001 From: gil Date: Thu, 30 May 2024 14:04:17 -0500 Subject: [PATCH] Alphabetize list output --- src/manip.rs | 23 +++++++++++++++++++---- src/tests/manip.rs | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/manip.rs b/src/manip.rs index ed930bc..c55dc28 100644 --- a/src/manip.rs +++ b/src/manip.rs @@ -103,10 +103,15 @@ impl ModMap { self } - pub fn export_file(self, block_path: &str, mute_path: &str, heat: (u16, u16)) -> std::io::Result<()> { + pub fn export_file( + self, + block_path: &str, + mute_path: &str, + heat: (u16, u16), + ) -> std::io::Result<()> { if self.0.is_empty() { error!("Nothing to export!"); - return Ok(()) + return Ok(()); } let (block_thresh, mute_thresh) = heat; @@ -125,9 +130,19 @@ impl ModMap { } } - fs::write(block_path, block_output)?; - fs::write(mute_path, mute_output)?; + fs::write(block_path, alphabetize_lines(block_output))?; + fs::write(mute_path, alphabetize_lines(mute_output))?; Ok(()) } } + +pub fn alphabetize_lines(string: String) -> String { + let mut v = string + .lines() + .map(|literal| literal.to_string()) + .collect::>(); + v.sort_by_key(|a| a.to_lowercase()); + + v.join("\n") + "\n" +} diff --git a/src/tests/manip.rs b/src/tests/manip.rs index b58978a..f51f725 100644 --- a/src/tests/manip.rs +++ b/src/tests/manip.rs @@ -196,5 +196,5 @@ fn modmap_export_txt() { let mutes: String = fs::read_to_string("test_mutes.txt").unwrap(); assert_eq!(blocks, "example.com\n"); - assert!(mutes == "example.org\nexample.net\n" || mutes == "example.net\nexample.org\n"); + assert_eq!(mutes, "example.net\nexample.org\n"); }