feat: Moved Network to its own file
This commit is contained in:
parent
0dfd17cc94
commit
e30ecd06e6
3 changed files with 104 additions and 25 deletions
55
src/main.rs
55
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<String> {
|
||||
let output = Command::new("nmcli")
|
||||
|
|
@ -23,26 +10,44 @@ fn get_available_wifis() -> Result<String> {
|
|||
.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<u8> {
|
||||
let mut positions: Vec<u8> = 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<String> = nmcli_output.split('\n').map(|s| String::from(s)).collect();
|
||||
let mut all_networks: Vec<Network> = 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());
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue