Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken pipe #92

Merged
merged 2 commits into from
Nov 4, 2024
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
49 changes: 17 additions & 32 deletions popgetter_cli/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::collections::HashMap;
use std::io::Write;
use std::sync::OnceLock;

use comfy_table::{presets::NOTHING, *};
use itertools::izip;
use polars::{
frame::{DataFrame, UniqueKeepStrategy},
prelude::SortMultipleOptions,
};
use polars::{frame::DataFrame, prelude::SortMultipleOptions};
use popgetter::{metadata::ExpandedMetadata, search::SearchResults, COL};

static LOOKUP: OnceLock<HashMap<&'static str, &'static str>> = OnceLock::new();
Expand Down Expand Up @@ -108,8 +106,8 @@ pub fn display_countries(countries: DataFrame, max_results: Option<usize>) -> an
country_iso3116_2.unwrap_or_default(),
]);
}
println!("\n{}", table);
Ok(())

Ok(writeln!(&mut std::io::stdout(), "\n{}", table)?)
}

pub fn display_search_results(
Expand Down Expand Up @@ -192,7 +190,7 @@ pub fn display_search_results(
}
}
}
println!("\n{}", table);
writeln!(&mut std::io::stdout(), "{}", table)?;
}
Ok(())
}
Expand Down Expand Up @@ -227,40 +225,29 @@ pub fn display_summary(results: SearchResults) -> anyhow::Result<()> {
let column = table.column_mut(1).unwrap();
column.set_cell_alignment(CellAlignment::Right);

println!("\n{}", table);
Ok(())
Ok(writeln!(&mut std::io::stdout(), "\n{}", table)?)
}

/// Display a given column from the search results
pub fn display_column(search_results: SearchResults, column: &str) -> anyhow::Result<()> {
search_results
Ok(search_results
.0
.select([column])?
.column(column)?
.rechunk()
.iter()
.for_each(|series| {
series
.rechunk()
.iter()
.map(|el| el.get_str().map(|s| s.to_string()).unwrap())
.for_each(|el| println!("{el}"))
});
Ok(())
.map(|el| el.get_str().map(|s| s.to_string()).unwrap())
.try_for_each(|el| writeln!(&mut std::io::stdout(), "{el}"))?)
}

/// Display the unique values of a given column from the search results
pub fn display_column_unique(search_results: SearchResults, column: &str) -> anyhow::Result<()> {
search_results
Ok(search_results
.0
.select([column])?
.unique(None, UniqueKeepStrategy::Any, None)?
.column(column)?
.unique()?
.iter()
.for_each(|series| {
series
.iter()
.map(|el| el.get_str().map(|s| s.to_string()).unwrap())
.for_each(|el| println!("{el}"))
});
Ok(())
.map(|el| el.get_str().map(|s| s.to_string()).unwrap())
.try_for_each(|el| writeln!(&mut std::io::stdout(), "{el}"))?)
}

/// Display the columns of the expanded metadata that can be used for displaying metrics results
Expand All @@ -271,7 +258,5 @@ pub fn display_metdata_columns(expanded_metadata: &ExpandedMetadata) -> anyhow::
.collect()?
.get_column_names()
.into_iter()
.collect::<Vec<_>>()
.into_iter()
.for_each(|val| println!("{val}")))
.try_for_each(|el| writeln!(&mut std::io::stdout(), "{el}"))?)
}
13 changes: 12 additions & 1 deletion popgetter_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod cli;
mod display;

use std::io::prelude::*;

use anyhow::Result;
use clap::Parser;
use cli::{Cli, RunCommand};
Expand All @@ -21,7 +23,16 @@ async fn main() -> Result<()> {
debug!("config: {config:?}");

if let Some(command) = args.command {
command.run(config).await?;
// Return ok if pipe is closed instead of error, otherwise return error
// See: https://stackoverflow.com/a/65760807, https://github.com/rust-lang/rust/issues/62569
if let Err(err) = command.run(config).await {
if let Some(err) = err.downcast_ref::<std::io::Error>() {
if err.kind() == std::io::ErrorKind::BrokenPipe {
return Ok(());
}
}
Err(err)?;
}
}
Ok(())
}
Expand Down
Loading