mirror of
https://github.com/imsnif/bandwhich.git
synced 2026-02-09 01:59:18 +08:00
Make write_to_stdout take a &str instead of a String
This commit is contained in:
@@ -45,17 +45,17 @@ where
|
||||
opts: opts.render_opts,
|
||||
}
|
||||
}
|
||||
pub fn output_text(&mut self, write_to_stdout: &mut (dyn FnMut(String) + Send)) {
|
||||
pub fn output_text(&mut self, write_to_stdout: &mut (dyn FnMut(&str) + Send)) {
|
||||
let state = &self.state;
|
||||
let ip_to_host = &self.ip_to_host;
|
||||
let local_time: DateTime<Local> = Local::now();
|
||||
let timestamp = local_time.timestamp();
|
||||
let mut no_traffic = true;
|
||||
|
||||
let output_process_data = |write_to_stdout: &mut (dyn FnMut(String) + Send),
|
||||
let output_process_data = |write_to_stdout: &mut (dyn FnMut(&str) + Send),
|
||||
no_traffic: &mut bool| {
|
||||
for (proc_info, process_network_data) in &state.processes {
|
||||
write_to_stdout(format!(
|
||||
write_to_stdout(&format!(
|
||||
"process: <{timestamp}> \"{}\" up/down Bps: {}/{} connections: {}",
|
||||
proc_info.name,
|
||||
process_network_data.total_bytes_uploaded,
|
||||
@@ -67,9 +67,9 @@ where
|
||||
};
|
||||
|
||||
let output_connections_data =
|
||||
|write_to_stdout: &mut (dyn FnMut(String) + Send), no_traffic: &mut bool| {
|
||||
|write_to_stdout: &mut (dyn FnMut(&str) + Send), no_traffic: &mut bool| {
|
||||
for (connection, connection_network_data) in &state.connections {
|
||||
write_to_stdout(format!(
|
||||
write_to_stdout(&format!(
|
||||
"connection: <{timestamp}> {} up/down Bps: {}/{} process: \"{}\"",
|
||||
display_connection_string(
|
||||
connection,
|
||||
@@ -84,10 +84,10 @@ where
|
||||
}
|
||||
};
|
||||
|
||||
let output_adressess_data = |write_to_stdout: &mut (dyn FnMut(String) + Send),
|
||||
let output_adressess_data = |write_to_stdout: &mut (dyn FnMut(&str) + Send),
|
||||
no_traffic: &mut bool| {
|
||||
for (remote_address, remote_address_network_data) in &state.remote_addresses {
|
||||
write_to_stdout(format!(
|
||||
write_to_stdout(&format!(
|
||||
"remote_address: <{timestamp}> {} up/down Bps: {}/{} connections: {}",
|
||||
display_ip_or_host(*remote_address, ip_to_host),
|
||||
remote_address_network_data.total_bytes_uploaded,
|
||||
@@ -99,7 +99,7 @@ where
|
||||
};
|
||||
|
||||
// header
|
||||
write_to_stdout("Refreshing:".into());
|
||||
write_to_stdout("Refreshing:");
|
||||
|
||||
// body1
|
||||
if self.opts.processes {
|
||||
@@ -119,11 +119,11 @@ where
|
||||
|
||||
// body2: In case no traffic is detected
|
||||
if no_traffic {
|
||||
write_to_stdout("<NO TRAFFIC>".into());
|
||||
write_to_stdout("<NO TRAFFIC>");
|
||||
}
|
||||
|
||||
// footer
|
||||
write_to_stdout("".into());
|
||||
write_to_stdout("");
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, paused: bool, show_dns: bool, elapsed_time: Duration, ui_offset: usize) {
|
||||
|
||||
@@ -82,7 +82,7 @@ pub struct OsInputOutput {
|
||||
pub get_open_sockets: fn() -> OpenSockets,
|
||||
pub terminal_events: Box<dyn Iterator<Item = Event> + Send>,
|
||||
pub dns_client: Option<dns::Client>,
|
||||
pub write_to_stdout: Box<dyn FnMut(String) + Send>,
|
||||
pub write_to_stdout: Box<dyn FnMut(&str) + Send>,
|
||||
}
|
||||
|
||||
pub fn start<B>(terminal_backend: B, os_input: OsInputOutput, opts: Opt)
|
||||
@@ -123,7 +123,7 @@ where
|
||||
move || {
|
||||
while running.load(Ordering::Acquire) {
|
||||
let render_start_time = Instant::now();
|
||||
let utilization = { network_utilization.lock().unwrap().clone_and_reset() };
|
||||
let utilization = network_utilization.lock().unwrap().clone_and_reset();
|
||||
let OpenSockets { sockets_to_procs } = get_open_sockets();
|
||||
let mut ip_to_host = IpTable::new();
|
||||
if let Some(dns_client) = dns_client.as_mut() {
|
||||
|
||||
@@ -80,10 +80,10 @@ fn get_interface(interface_name: &str) -> Option<NetworkInterface> {
|
||||
.find(|iface| iface.name == interface_name)
|
||||
}
|
||||
|
||||
fn create_write_to_stdout() -> Box<dyn FnMut(String) + Send> {
|
||||
fn create_write_to_stdout() -> Box<dyn FnMut(&str) + Send> {
|
||||
let mut stdout = io::stdout();
|
||||
Box::new({
|
||||
move |output: String| match writeln!(stdout, "{}", output) {
|
||||
move |output| match writeln!(stdout, "{}", output) {
|
||||
Ok(_) => (),
|
||||
Err(e) if e.kind() == ErrorKind::BrokenPipe => {
|
||||
// A process that was listening to bandwhich stdout has exited
|
||||
|
||||
@@ -257,14 +257,14 @@ pub fn os_input_output_factory(
|
||||
) -> OsInputOutput {
|
||||
let interfaces_with_frames = get_interfaces_with_frames(network_frames);
|
||||
|
||||
let write_to_stdout: Box<dyn FnMut(String) + Send> = match stdout {
|
||||
let write_to_stdout: Box<dyn FnMut(&str) + Send> = match stdout {
|
||||
Some(stdout) => Box::new({
|
||||
move |output: String| {
|
||||
move |output| {
|
||||
let mut stdout = stdout.lock().unwrap();
|
||||
writeln!(&mut stdout, "{}", output).unwrap();
|
||||
}
|
||||
}),
|
||||
None => Box::new(move |_output: String| {}),
|
||||
None => Box::new(|_output| {}),
|
||||
};
|
||||
|
||||
OsInputOutput {
|
||||
|
||||
@@ -657,7 +657,7 @@ fn traffic_with_host_names(network_frames: Vec<Box<dyn DataLinkReceiver>>) {
|
||||
String::from("i-like-cheese.com"),
|
||||
);
|
||||
let dns_client = create_fake_dns_client(ips_to_hostnames);
|
||||
let write_to_stdout = Box::new(move |_output: String| {});
|
||||
let write_to_stdout = Box::new(|_output: &_| {});
|
||||
|
||||
let os_input = OsInputOutput {
|
||||
interfaces_with_frames,
|
||||
@@ -696,7 +696,7 @@ fn truncate_long_hostnames(network_frames: Vec<Box<dyn DataLinkReceiver>>) {
|
||||
String::from("i-like-cheese.com"),
|
||||
);
|
||||
let dns_client = create_fake_dns_client(ips_to_hostnames);
|
||||
let write_to_stdout = Box::new(move |_output: String| {});
|
||||
let write_to_stdout = Box::new(|_output: &_| {});
|
||||
|
||||
let os_input = OsInputOutput {
|
||||
interfaces_with_frames,
|
||||
@@ -735,7 +735,7 @@ fn no_resolve_mode(network_frames: Vec<Box<dyn DataLinkReceiver>>) {
|
||||
String::from("i-like-cheese.com"),
|
||||
);
|
||||
let dns_client = None;
|
||||
let write_to_stdout = Box::new(move |_output: String| {});
|
||||
let write_to_stdout = Box::new(|_output: &_| {});
|
||||
|
||||
let os_input = OsInputOutput {
|
||||
interfaces_with_frames,
|
||||
@@ -768,7 +768,7 @@ fn traffic_with_winch_event() {
|
||||
let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50);
|
||||
|
||||
let dns_client = create_fake_dns_client(HashMap::new());
|
||||
let write_to_stdout = Box::new(move |_output: String| {});
|
||||
let write_to_stdout = Box::new(|_output: &_| {});
|
||||
|
||||
let os_input = OsInputOutput {
|
||||
interfaces_with_frames,
|
||||
|
||||
Reference in New Issue
Block a user