diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..a61610b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod network; diff --git a/src/main.rs b/src/main.rs index e8fad5c..0295bf3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,6 @@ -use std::{process::Command, vec}; +use wifi_connector::network::Network; use anyhow::*; - -struct Network { - in_use: bool, - ssid: String, - signal: u8, -} - -impl Network { - fn from_nmcli_stdout(line: String) { - for element in line.split(' ') { - println!("{}", element); - } - } -} +use std::{process::Command, vec}; fn get_available_wifis() -> Result { let output = Command::new("nmcli") @@ -23,26 +10,44 @@ fn get_available_wifis() -> Result { .output() .expect("Failed to execute command"); - Ok(String::from_utf8_lossy(output.stdout.as_slice()).into()) + Ok(String::from_utf8_lossy(output.stdout.as_slice()).into()) } fn get_descriptor_positions(header_line: &String) -> Vec { let mut positions: Vec = vec![]; positions.push(0); - positions.push(header_line.find(" SSID").expect("SSID not found in header string") as u8 +1); - positions.push(header_line.find("SIGNAL").expect("SIGNAL not found in header string") as u8); + positions.push( + header_line + .find(" SSID") + .expect("SSID not found in header string") as u8 + + 1, + ); + positions.push( + header_line + .find("SIGNAL") + .expect("SIGNAL not found in header string") as u8, + ); return positions; } fn main() { - let all_wifis: String = get_available_wifis().expect("Wifi fetching exploded"); - let positions = get_descriptor_positions(&all_wifis); - for position in (positions) { - println!("{}", position); + let nmcli_output: String = get_available_wifis().expect("Wifi fetching exploded"); + let positions = get_descriptor_positions(&nmcli_output); + + let mut all_wifi_lines: Vec = nmcli_output.split('\n').map(|s| String::from(s)).collect(); + let mut all_networks: Vec = vec![]; + all_wifi_lines.remove(0); + + for line in all_wifi_lines { + if line.is_empty() { + continue; + } + all_networks.push(Network::from_nmcli_stdout(line.to_owned(), &positions)); + } + + for network in all_networks { + dbg!("{}", network); } - // for line in all_wifis.split("\n") { - // Network::from_nmcli_stdout(line.to_owned()); - // } } diff --git a/src/network.rs b/src/network.rs new file mode 100644 index 0000000..1cfc4a3 --- /dev/null +++ b/src/network.rs @@ -0,0 +1,73 @@ +#[derive(Debug)] +pub struct Network { + in_use: bool, + ssid: String, + signal: u8, +} + +impl Network { + pub fn from_nmcli_stdout(line: String, header_positions: &Vec) -> Self { + let mut network = Self { + in_use: false, + ssid: "".to_string(), + signal: 0, + }; + + let ssid_pos = header_positions + .get(1) + .expect("header_positions has no index for SSID") + .to_owned() as usize; + let signal_pos = header_positions + .get(2) + .expect("header_positions has no index for SSID") + .to_owned() as usize; + + if line + .chars() + .nth( + header_positions + .get(0) + .expect("header_positions has no index 0") + .to_owned() as usize, + ) + .expect("Given line has no index 0") + == '*' + { + network.in_use = true; + } + + let mut current_spaces = 0; + for char in line.chars().skip(ssid_pos) { + if char == ' ' { + current_spaces += 1; + if current_spaces == 2 { + break; + } + } else { + if current_spaces == 1 { + network.ssid.push(' '); + } + current_spaces = 0; + network.ssid.push(char); + } + } + + let mut signal_as_string: String = line + .chars() + .nth(signal_pos) + .expect("Line has no Signal pos") + .to_string(); + + signal_as_string.push( + line.chars() + .nth(signal_pos + 1) + .expect("Line has no Signal pos"), + ); + + network.signal = signal_as_string + .parse::() + .expect("Could not parse signal"); + + return network; + } +}