From aa9bb64b0e16a8698d088cd91d18fa36b8493519 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Mon, 20 Nov 2023 00:17:08 -0700 Subject: [PATCH] Sort channel list output --- src/cli/list.rs | 11 +++++++++-- src/cli/search.rs | 6 +++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/cli/list.rs b/src/cli/list.rs index b2b6d89..2d05533 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -16,15 +16,22 @@ impl Cmd { let lookup_tree = db.open_tree("lookup")?; let channel_tree = db.open_tree("channel")?; + let mut results = vec![]; for entry in lookup_tree.iter() { let (key, value) = entry?; - let key_str = String::from_utf8(key.to_vec())?; + let channel_name = String::from_utf8(key.to_vec())?; let channel_index = String::from_utf8(value.to_vec())?; if let Ok(Some(_channel_url)) = channel_tree.get(&channel_index) { - println!("{} \t {}", channel_index, key_str); + results.push((channel_index.parse::()?, channel_name)); } } + results.sort_by(|a, b| a.0.cmp(&b.0)); + + for (channel_index, channel_name) in results { + println!("{} \t {}", channel_index, channel_name); + } + Ok(()) } } diff --git a/src/cli/search.rs b/src/cli/search.rs index 660c704..36ca3f5 100644 --- a/src/cli/search.rs +++ b/src/cli/search.rs @@ -22,11 +22,11 @@ impl Cmd { for entry in lookup_tree.iter() { let (key, value) = entry?; - let key_str = String::from_utf8(key.to_vec())?; - if key_str.contains(channel_name.trim()) { + let db_channel_name = String::from_utf8(key.to_vec())?; + if db_channel_name.contains(channel_name.trim()) { let channel_index = String::from_utf8(value.to_vec())?; if let Ok(Some(_channel_url)) = channel_tree.get(&channel_index) { - println!("{} \t {}", channel_index, key_str); + println!("{} \t {}", channel_index, db_channel_name); } } }