Skip to content

Commit fae1508

Browse files
committed
make i3stat-net and i3stat-acpi line buffered by default
1 parent fae11fc commit fae1508

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

bin/acpi.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::{ColorChoice, Parser};
22
use i3stat::error::Result;
33
use i3stat::util::{local_block_on, netlink_acpi_listen};
4+
use tokio::io::{stdout, AsyncWriteExt};
45

56
#[derive(Debug, Parser)]
67
#[clap(author, version, long_about, name = "i3stat-acpi", color = ColorChoice::Always)]
@@ -16,7 +17,13 @@ fn main() -> Result<()> {
1617
let (output, _) = local_block_on(async {
1718
let mut acpi = netlink_acpi_listen().await?;
1819
while let Some(event) = acpi.recv().await {
19-
println!("{}", serde_json::to_string(&event)?);
20+
let line = format!("{}", serde_json::to_string(&event)?);
21+
22+
// flush output each time to facilitate common usage patterns, such as
23+
// `i3stat-acpi | while read x; do ... done`, etc.
24+
let mut stdout = stdout();
25+
stdout.write_all(line.as_bytes()).await?;
26+
stdout.flush().await?;
2027
}
2128

2229
Err("unexpected end of acpi event stream".into())

bin/net.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use i3stat::error::Result;
44
use i3stat::util::route::InterfaceUpdate;
55
use i3stat::util::{local_block_on, netlink_ipaddr_listen};
66
use serde_json::json;
7+
use tokio::io::{stdout, AsyncWriteExt};
78
use tokio::sync::mpsc;
89

910
#[derive(Debug, Parser)]
@@ -30,21 +31,20 @@ fn main() -> Result<()> {
3031

3132
let (output, _) = local_block_on(async {
3233
let (manual_tx, manual_rx) = mpsc::channel(1);
33-
manual_tx.send(()).await?;
34-
3534
let mut rx = netlink_ipaddr_listen(manual_rx).await?;
35+
manual_tx.send(()).await?;
3636

3737
if let Command::Info = args.command {
3838
match rx.recv().await {
39-
Some(interfaces) => print_interfaces(&interfaces).await,
39+
Some(interfaces) => print_interfaces(&interfaces).await?,
4040
None => println!("null"),
4141
}
4242

4343
return Ok(());
4444
}
4545

4646
while let Some(interfaces) = rx.recv().await {
47-
print_interfaces(&interfaces).await;
47+
print_interfaces(&interfaces).await?;
4848
}
4949

5050
Err("Unexpected end of netlink subscription".into())
@@ -53,9 +53,9 @@ fn main() -> Result<()> {
5353
output
5454
}
5555

56-
async fn print_interfaces(interfaces: &InterfaceUpdate) {
57-
println!(
58-
"{}",
56+
async fn print_interfaces(interfaces: &InterfaceUpdate) -> Result<()> {
57+
let s = format!(
58+
"{}\n",
5959
json!(
6060
join_all(interfaces.values().map(|interface| async {
6161
json!({
@@ -80,6 +80,14 @@ async fn print_interfaces(interfaces: &InterfaceUpdate) {
8080
.await
8181
)
8282
);
83+
84+
// flush output each time to facilitate common usage patterns like
85+
// `i3stat-net watch | while read x; do ... done`, etc.
86+
let mut stdout = stdout();
87+
stdout.write_all(s.as_bytes()).await?;
88+
stdout.flush().await?;
89+
90+
Ok(())
8391
}
8492

8593
#[cfg(test)]

0 commit comments

Comments
 (0)