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

feat: allow configuration to be read from stdin #5

Open
wants to merge 3 commits into
base: ubuntu/v0.32.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
40 changes: 37 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Read;

use clap::Parser;

use i3status_rs::config::Config;
Expand All @@ -6,6 +8,8 @@ use i3status_rs::escape::Escaped;
use i3status_rs::widget::{State, Widget};
use i3status_rs::{protocol, util, BarState};

const STDIN_FILE_DESIGNATOR: &str = "-";

fn main() {
env_logger::init();

Expand All @@ -22,9 +26,39 @@ fn main() {
.build()
.unwrap()
.block_on(async move {
let config_path = util::find_file(&args.config, None, Some("toml"))
.or_error(|| format!("Configuration file '{}' not found", args.config))?;
let mut config: Config = util::deserialize_toml_file(&config_path)?;
let mut config: Config = match args.config.as_str() {
STDIN_FILE_DESIGNATOR => {
// read from stdin
let mut config_str = String::new();

let size = std::io::stdin()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to use the tokio version of stdin. https://docs.rs/tokio/latest/tokio/io/fn.stdin.html

.read_to_string(&mut config_str)
.or_error(|| {
"Configuration file could not be read from stdin".to_string()
})?;

if size == 0 {
return Err(i3status_rs::errors::Error {
kind: ErrorKind::Config,
message: None,
cause: None,
block: None,
});
}

util::deserialize_toml(&config_str, None)?
}
_ => {
// read from file path
let config_path = util::find_file(&args.config, None, Some("toml"))
.or_error(|| format!("Configuration file '{}' not found", args.config))?;
let config_str = util::read_file(&config_path)
.await
.or_error(|| format!("Configuration file '{}' not found", args.config))?;
util::deserialize_toml(&config_str, Some(&config_path))?
}
};

let blocks = std::mem::take(&mut config.blocks);
let mut bar = BarState::new(config);
for block_config in blocks {
Expand Down
16 changes: 14 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,19 @@ where
let contents = std::fs::read_to_string(path)
.or_error(|| format!("Failed to read file: {}", path.display()))?;

toml::from_str(&contents).map_err(|err| {
self::deserialize_toml(&contents, Some(path))
}

pub fn deserialize_toml<T>(contents: &String, file_path: Option<&Path>) -> Result<T>
where
T: DeserializeOwned,
{
let source = match file_path {
Some(msg) => msg.display().to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible a if let else construct is more readable than a match to check an Option. Something like

if let Some(path) = file_path { path} else { "(from stdin)"} 

_ => String::from("(from stdin)"),
};

toml::from_str(contents).map_err(|err| {
#[allow(deprecated)]
let location_msg = err
.span()
Expand All @@ -109,7 +121,7 @@ where
.unwrap_or_default();
Error::new(format!(
"Failed to deserialize TOML file {}{}: {}",
path.display(),
source,
location_msg,
err.message()
))
Expand Down
Loading