From e5cb2d05f9dff4b0b35cba0836973a03086ba57b Mon Sep 17 00:00:00 2001 From: s-prechtl Date: Fri, 23 Jun 2023 19:45:49 +0200 Subject: [PATCH] feat: List now shows only unique SSIDs with the strongest corresponding signal --- src/main.rs | 21 +++++++++++++++++++-- src/network.rs | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 71e23e2..e2a8658 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use anyhow::*; -use std::{io::Write, mem::align_of_val, process::Command}; +use std::{collections::HashMap, io::Write, process::Command, vec}; use wifi_connector::network::Network; fn get_available_wifis() -> Result { @@ -113,8 +113,25 @@ fn print_header() { println!("Index - In use - SSID - Signal"); } +fn uniquify_networks(networks: Vec) -> Vec { + let mut unique_networks: HashMap = HashMap::new(); + + for network in &networks { + let current_ssid = &network.ssid; + if unique_networks.keys().any(|key| key == current_ssid) { + if unique_networks[current_ssid].signal < network.signal { + unique_networks.insert(current_ssid.to_owned(), network.to_owned()); + } + } else { + unique_networks.insert(current_ssid.to_owned(), network.to_owned()); + } + } + + return unique_networks.into_values().collect(); +} + fn main() { - let all_networks = get_all_networks(); + let all_networks = uniquify_networks(get_all_networks()); print_header(); print_networks(&all_networks); diff --git a/src/network.rs b/src/network.rs index 59f342d..757813a 100644 --- a/src/network.rs +++ b/src/network.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Network { pub in_use: bool, pub ssid: String,