Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nload-rs"
version = "0.1.4"
version = "0.1.5"
edition = "2024"
description = "A Linux single-interface network traffic monitor inspired by nload"
license = "GPL-2.0-only"
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ requested one when it first appears. New interfaces become available as they
appear; disconnected interfaces stay in the list and are marked unavailable.

Use `-i VALUE` and `-o VALUE` to set the incoming and outgoing graph ceilings.
Both default to 10,240 kBit/s (10 MBit/s in binary units). Run
`nload-rs --help` for all options.
Both default to 10,240 kBit/s (10 MBit/s in binary units).

Use `--auto-scale` to adjust each graph independently. The displayed ceiling
rises immediately with spikes and falls in steps after traffic stays low.
Older spikes may clip as the ceiling falls, but statistics are unchanged.

An explicit `-i` or `-o` keeps that direction fixed, regardless of option order.
For example, `nload-rs --auto-scale -o 0 lo` auto-scales incoming traffic and
keeps outgoing traffic at the default ceiling. Run `nload-rs --help` for all options.

Use a terminal at least 60×16 with UTF-8 and a font that renders Braille.
`TERM` must be nonempty and neither `dumb` nor `linux`. `--color` requires
Expand Down
4 changes: 2 additions & 2 deletions src/application/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ fn observed_wire(selected: usize, width: usize, height: usize, rounds: usize) ->
&first,
None,
arguments::Ceilings {
incoming: 128,
outgoing: 128,
incoming: arguments::CeilingPolicy::Fixed(128),
outgoing: arguments::CeilingPolicy::Fixed(128),
},
0,
)
Expand Down
147 changes: 109 additions & 38 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ pub(crate) const DEFAULT_CEILING_KBIT: u64 = 10240;
pub(crate) const DEFAULT_CEILING_BYTES: u64 = DEFAULT_CEILING_KBIT * 128;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Ceilings {
pub(crate) incoming: u64,
pub(crate) outgoing: u64,
pub(crate) enum CeilingPolicy {
Fixed(u64),
Auto,
}

impl Default for Ceilings {
impl Default for CeilingPolicy {
fn default() -> Self {
Self {
incoming: DEFAULT_CEILING_BYTES,
outgoing: DEFAULT_CEILING_BYTES,
}
Self::Fixed(DEFAULT_CEILING_BYTES)
}
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct Ceilings {
pub(crate) incoming: CeilingPolicy,
pub(crate) outgoing: CeilingPolicy,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RunConfig {
pub(crate) interface: Option<Vec<u8>>,
Expand Down Expand Up @@ -59,11 +62,11 @@ pub(crate) fn parse(
}
}

let mut config = RunConfig {
interface: None,
ceilings: Ceilings::default(),
color: false,
};
let mut interface = None;
let mut color = false;
let mut auto_scale = false;
let mut incoming = None;
let mut outgoing = None;
let mut options = true;
let mut tokens = tokens.iter();
while let Some(token) = tokens.next() {
Expand All @@ -75,7 +78,11 @@ pub(crate) fn parse(
continue;
}
b"--color" => {
config.color = true;
color = true;
continue;
}
b"--auto-scale" => {
auto_scale = true;
continue;
}
b"-i" | b"-o" => {
Expand All @@ -84,9 +91,9 @@ pub(crate) fn parse(
.ok_or(ArgumentError("ceiling option requires a separate value"))?;
let ceiling = parse_ceiling(value.as_bytes())?;
if bytes == b"-i" {
config.ceilings.incoming = ceiling;
incoming = Some(ceiling);
} else {
config.ceilings.outgoing = ceiling;
outgoing = Some(ceiling);
}
continue;
}
Expand All @@ -101,12 +108,24 @@ pub(crate) fn parse(
_ => {}
}
}
if config.interface.is_some() {
if interface.is_some() {
return Err(ArgumentError("at most one interface name is allowed"));
}
config.interface = Some(bytes.to_vec());
interface = Some(bytes.to_vec());
}
Ok(Invocation::Run(config))
let default = if auto_scale {
CeilingPolicy::Auto
} else {
CeilingPolicy::default()
};
Ok(Invocation::Run(RunConfig {
interface,
ceilings: Ceilings {
incoming: incoming.map_or(default, CeilingPolicy::Fixed),
outgoing: outgoing.map_or(default, CeilingPolicy::Fixed),
},
color,
}))
}

fn parse_ceiling(bytes: &[u8]) -> Result<u64, ArgumentError> {
Expand Down Expand Up @@ -134,7 +153,7 @@ pub(crate) fn version_text() -> String {

pub(crate) fn help_text() -> String {
format!(
"Usage: {name} [--color] [-i VALUE] [-o VALUE] [--] [INTERFACE]\n\
"Usage: {name} [--color] [--auto-scale] [-i VALUE] [-o VALUE] [--] [INTERFACE]\n\
\x20 {name} --help\n\
\x20 {name} --version\n\n\
Monitor one interface; arrows navigate the naturally sorted collection.\n\
Expand All @@ -147,10 +166,12 @@ pub(crate) fn help_text() -> String {
VALUE is ASCII decimal binary-scaled kBit/s (1 kBit/s = 1024 Bit/s\n\
= 128 bytes/s), from 1 through 144115188075855871.\n\
Each direction defaults independently to {default} kBit/s (10.00 MBit/s).\n\
Zero restores that direction's default; repeated options use the last value.\n\
Ceiling values must be separate tokens; options may precede or follow a name.\n\
--color Enable colored Braille graphs (off by default; ignores NO_COLOR).\n\
--help and --version are standalone; no short aliases are accepted.\n",
Zero restores that direction's fixed default, even with --auto-scale.\n\
Repeated options use the last value.\n\
Ceiling values must be separate tokens; options may precede or follow a name.\n\
--auto-scale Adapt each graph's ceiling to traffic unless -i or -o fixes it.\n\
--color Enable colored Braille graphs (off by default; ignores NO_COLOR).\n\
--help and --version are standalone; no short aliases are accepted.\n",
name = env!("CARGO_PKG_NAME"),
default = DEFAULT_CEILING_KBIT,
)
Expand All @@ -159,6 +180,7 @@ pub(crate) fn help_text() -> String {
#[cfg(test)]
mod tests {
use super::*;
use CeilingPolicy::{Auto, Fixed};
use std::os::unix::ffi::OsStringExt;

fn invoke(tokens: &[&str]) -> Result<Invocation, ArgumentError> {
Expand Down Expand Up @@ -186,6 +208,9 @@ mod tests {
"INTERFACE",
"-i VALUE",
"-o VALUE",
"--auto-scale",
"unless -i or -o fixes it",
"fixed default, even with --auto-scale",
"--color",
"off by default",
"ignores NO_COLOR",
Expand Down Expand Up @@ -222,6 +247,8 @@ mod tests {
vec!["--color", "--help"],
vec!["--version", "--color"],
vec!["--color=always"],
vec!["--auto-scale=always"],
vec!["--auto-scale", "--help"],
vec!["--", "eth0", "eth1"],
] {
let error = invoke(&tokens).expect_err(&format!("accepted {tokens:?}"));
Expand All @@ -236,7 +263,15 @@ mod tests {
panic!("expected run");
};
assert_eq!(config.interface, Some(name));
for name in ["-eth0", "--help", "--version", "--color", "-i", "--"] {
for name in [
"-eth0",
"--help",
"--version",
"--color",
"--auto-scale",
"-i",
"--",
] {
assert_eq!(run(&["--", name]).interface, Some(name.as_bytes().to_vec()));
}
assert_eq!(run(&["ETH0"]).interface, Some(b"ETH0".to_vec()));
Expand All @@ -245,6 +280,36 @@ mod tests {
assert!(invoke(&["--", "eth0", "-i", "1"]).is_err());
}

#[test]
fn automatic_scaling_yields_to_explicit_ceilings_in_either_order() {
assert_eq!(
run(&["--auto-scale", "eth0"]).ceilings,
Ceilings {
incoming: Auto,
outgoing: Auto,
}
);
assert_eq!(run(&["--", "--auto-scale"]).ceilings, Ceilings::default());
for (flag, value, incoming, outgoing) in [
("-i", "10", Fixed(1280), Auto),
("-o", "20", Auto, Fixed(2560)),
("-i", "0", Fixed(DEFAULT_CEILING_BYTES), Auto),
("-o", "0", Auto, Fixed(DEFAULT_CEILING_BYTES)),
] {
let expected = Ceilings { incoming, outgoing };
assert_eq!(run(&["--auto-scale", flag, value]).ceilings, expected);
assert_eq!(
run(&[flag, value, "eth0", "--auto-scale"]).ceilings,
expected
);
}
let fixed = ["-i", "1", "-o", "0", "-i", "2"];
assert_eq!(
run(&["-i", "1", "--auto-scale", "-o", "0", "-i", "2"]).ceilings,
run(&fixed).ceilings,
);
}

#[test]
fn graph_colors_are_opt_in_and_follow_option_grammar() {
for tokens in [vec![], vec!["eth0"], vec!["--", "--color"]] {
Expand Down Expand Up @@ -272,33 +337,39 @@ mod tests {
assert_eq!(
default.ceilings,
Ceilings {
incoming: 1_310_720,
outgoing: 1_310_720
incoming: Fixed(1_310_720),
outgoing: Fixed(1_310_720),
}
);
let before = run(&["-i", "20480", "-o", "5120", "eth0"]);
assert_eq!(before, run(&["eth0", "-i", "20480", "-o", "5120"]));
assert_eq!(
before.ceilings,
Ceilings {
incoming: 2_621_440,
outgoing: 655_360
incoming: Fixed(2_621_440),
outgoing: Fixed(655_360),
}
);
assert_eq!(run(&["-i", "10", "-i", "00020"]).ceilings.incoming, 2560);
assert_eq!(run(&["-o", "10", "-o", "20"]).ceilings.outgoing, 2560);
assert_eq!(
run(&["-i", "10", "-i", "00020"]).ceilings.incoming,
Fixed(2560)
);
assert_eq!(
run(&["-o", "10", "-o", "20"]).ceilings.outgoing,
Fixed(2560)
);
assert_eq!(
run(&["-i", "10", "-o", "20", "-i", "0000"]).ceilings,
Ceilings {
incoming: 1_310_720,
outgoing: 2560
incoming: Fixed(1_310_720),
outgoing: Fixed(2560),
}
);
assert_eq!(
run(&["-i", "10", "-o", "20", "-o", "0"]).ceilings,
Ceilings {
incoming: 1280,
outgoing: 1_310_720
incoming: Fixed(1280),
outgoing: Fixed(1_310_720),
}
);
}
Expand All @@ -317,7 +388,7 @@ mod tests {
} else {
ceilings.outgoing
},
expected
Fixed(expected)
);
}
for value in [
Expand Down Expand Up @@ -347,10 +418,10 @@ mod tests {
let long_zero = "0".repeat(1000);
assert_eq!(
run(&["-i", &long_zero]).ceilings.incoming,
DEFAULT_CEILING_BYTES
Fixed(DEFAULT_CEILING_BYTES)
);
let long_one = format!("{long_zero}1");
assert_eq!(run(&["-i", &long_one]).ceilings.incoming, 128);
assert_eq!(run(&["-i", &long_one]).ceilings.incoming, Fixed(128));
assert!(parse([OsString::from("-i"), OsString::from_vec(vec![0xff])]).is_err());
assert!(invoke(&["-i", "bad", "-i", "1"]).is_err());
}
Expand Down
Loading