-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from acheronfail/next
0.12.0
- Loading branch information
Showing
6 changed files
with
136 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "i3stat" | ||
version = "0.11.0" | ||
version = "0.12.0" | ||
edition = "2021" | ||
authors = ["acheronfail <[email protected]>"] | ||
description = "A lightweight and batteries-included status_command for i3 and sway" | ||
|
@@ -11,6 +11,10 @@ keywords = ["i3", "sway", "status_command", "i3stat", "status"] | |
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[[bin]] | ||
name = "i3stat-net" | ||
path = "bin/net.rs" | ||
|
||
[[bin]] | ||
name = "i3stat-acpi" | ||
path = "bin/acpi.rs" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use clap::{ColorChoice, Parser, Subcommand}; | ||
use futures::future::join_all; | ||
use i3stat::error::Result; | ||
use i3stat::util::route::InterfaceUpdate; | ||
use i3stat::util::{local_block_on, netlink_ipaddr_listen}; | ||
use serde_json::json; | ||
use tokio::io::{stdout, AsyncWriteExt}; | ||
use tokio::sync::mpsc; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(author, version, long_about, name = "i3stat-net", color = ColorChoice::Always)] | ||
/// A command which prints network/80211 information gathered from netlink. | ||
/// | ||
/// Each line is a JSON array which contains a list of all interfaces reported | ||
/// by netlink. Wireless interfaces also print 80211 information. | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Command, | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, Subcommand)] | ||
enum Command { | ||
/// Print current network interfaces | ||
Info, | ||
/// Watch and print network interfaces whenever a network address change is detected. | ||
Watch, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let args = Cli::parse(); | ||
|
||
let (output, _) = local_block_on(async { | ||
let (manual_tx, manual_rx) = mpsc::channel(1); | ||
let mut rx = netlink_ipaddr_listen(manual_rx).await?; | ||
manual_tx.send(()).await?; | ||
|
||
if let Command::Info = args.command { | ||
match rx.recv().await { | ||
Some(interfaces) => print_interfaces(&interfaces).await?, | ||
None => println!("null"), | ||
} | ||
|
||
return Ok(()); | ||
} | ||
|
||
while let Some(interfaces) = rx.recv().await { | ||
print_interfaces(&interfaces).await?; | ||
} | ||
|
||
Err("Unexpected end of netlink subscription".into()) | ||
})?; | ||
|
||
output | ||
} | ||
|
||
async fn print_interfaces(interfaces: &InterfaceUpdate) -> Result<()> { | ||
let s = format!( | ||
"{}\n", | ||
json!( | ||
join_all(interfaces.values().map(|interface| async { | ||
json!({ | ||
"index": interface.index, | ||
"name": interface.name, | ||
"mac": interface.mac_address.as_ref().map(|m| m.to_string()), | ||
"ips": interface.ip_addresses.iter().collect::<Vec<_>>(), | ||
"wireless": interface.wireless_info().await.map(|info| json!({ | ||
"index": info.index, | ||
"interface": info.interface, | ||
"mac": info.mac_addr.to_string(), | ||
"ssid": info.ssid, | ||
"bssid": info.bssid.as_ref().map(|m| m.to_string()), | ||
"signal": info.signal.as_ref().map(|s| json!({ | ||
"dbm": s.dbm, | ||
"link": s.link, | ||
"quality": s.quality() | ||
})) | ||
})) | ||
}) | ||
})) | ||
.await | ||
) | ||
); | ||
|
||
// flush output each time to facilitate common usage patterns like | ||
// `i3stat-net watch | while read x; do ... done`, etc. | ||
let mut stdout = stdout(); | ||
stdout.write_all(s.as_bytes()).await?; | ||
stdout.flush().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
#[path = "../src/test_utils.rs"] | ||
mod test_utils; | ||
|
||
#[cfg(test)] | ||
crate::gen_manpage!(Cli); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters