Skip to content

Commit

Permalink
Merge pull request #3 from kymeria/new_field_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Nov 8, 2024
2 parents 8830592 + 2099a44 commit cbbdb7f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "graphex"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
description = "A small librray to help creating command line tools exploring pseudo graph"
authors = ["Matthieu Gautier <[email protected]"]
description = "A small library to help creating command line tools exploring pseudo graph"
authors = ["Matthieu Gautier <[email protected]>"]
repository = "https://github.com/kymeria/graphex"
license = "MIT"

Expand Down
80 changes: 59 additions & 21 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ impl<'a> Output<'a> {

/// Display a value with a prefix.
///
/// Mostly equivalent to `writeln!("- {name}: {value}")` but properly handle:
/// - When `name` is empty: `writeln!("- {value}")`.
/// - `value` displays on several lines: correctly display first line of value in
/// Mostly equivalent to `writeln!("{header} {value}")` but properly handle
/// when `value` displays on several lines: correctly display first line of value in
/// same line of prefix if `start_same_line()` is `true` and apply padding.
///
/// Prefer using this method in [Display::print_content] as
Expand All @@ -157,14 +156,13 @@ impl<'a> Output<'a> {
/// ```
/// instead of
/// ```ignore
/// writeln!(out, "- foo: {}", display_to_string(&self.foo))
/// writeln!(out, "foo {}", display_to_string(&self.foo))
/// ```
pub fn item(&mut self, name: &str, value: &impl Display) -> Result {
let header = if name.is_empty() {
"- ".to_string()
} else {
format!("- {name}:")
};
pub fn item(
&mut self,
header: &(impl std::fmt::Display + ?Sized),
value: &impl Display,
) -> Result {
let value_str = display_to_string(value)?;
let multi_line = value_str.contains('\n');
if multi_line {
Expand All @@ -187,6 +185,46 @@ impl<'a> Output<'a> {
writeln!(self, "{header} {value_str}")
}
}

/// Display a value with a name.
///
/// Mostly equivalent to `writeln!("- {name}: {value}")` but properly handle
/// when `value` displays on several lines: correctly display first line of value in
/// same line of prefix if `start_same_line()` is `true` and apply padding.
///
/// Prefer using this method in [Display::print_content] as
/// ```ignore
/// out.field("foo", &self.foo)
/// ```
/// instead of
/// ```ignore
/// writeln!(out, "- foo: {}", display_to_string(&self.foo))
/// ```
pub fn field(
&mut self,
name: &(impl std::fmt::Display + ?Sized),
value: &impl Display,
) -> Result {
self.item(&format!("- {name}:"), value)
}

/// Display a value.
///
/// Mostly equivalent to `writeln!("- {value}")` but properly handle
/// when `value` displays on several lines: correctly display first line of value in
/// same line of prefix if `start_same_line()` is `true` and apply padding.
///
/// Prefer using this method in [Display::print_content] as
/// ```ignore
/// out.entry(&self.foo)
/// ```
/// instead of
/// ```ignore
/// writeln!(out, "- {}", display_to_string(&self.foo))
/// ```
pub fn entry(&mut self, value: &impl Display) -> Result {
self.item(&"-", value)
}
}

macro_rules! impl_display {
Expand Down Expand Up @@ -237,7 +275,7 @@ where
{
fn print_content(&self, out: &mut Output) -> Result {
for v in self.iter() {
writeln!(out, "- {}", display_to_string(v)?)?;
out.entry(v)?;
}
Ok(())
}
Expand Down Expand Up @@ -280,7 +318,7 @@ where
{
fn print_content(&self, out: &mut Output) -> Result {
for (key, val) in self.iter() {
out.item(key.as_ref(), val)?;
out.field(key.as_ref(), val)?;
}
Ok(())
}
Expand Down Expand Up @@ -394,8 +432,8 @@ mod test {
}

fn print_content(&self, out: &mut Output) -> Result {
out.item("a", &self.a)?;
out.item("b", &self.b)
out.field("a", &self.a)?;
out.field("b", &self.b)
}
}

Expand All @@ -415,8 +453,8 @@ mod test {
Some(("Foo:".to_string(), String::new()))
}
fn print_content(&self, out: &mut Output) -> Result {
out.item("a", &self.a)?;
out.item("b", &self.b)
out.field("a", &self.a)?;
out.field("b", &self.b)
}
}

Expand Down Expand Up @@ -448,8 +486,8 @@ mod test {
Some(("Foo:".to_string(), String::new()))
}
fn print_content(&self, out: &mut Output) -> Result {
out.item("a", &self.a)?;
out.item("b", &self.b)
out.field("a", &self.a)?;
out.field("b", &self.b)
}
}

Expand All @@ -458,9 +496,9 @@ mod test {
Some(("Bar:".to_string(), String::new()))
}
fn print_content(&self, out: &mut Output) -> Result {
out.item("a", &self.a)?;
out.item("b", &self.b)?;
out.item("c", &AsBytes(&self.c))
out.field("a", &self.a)?;
out.field("b", &self.b)?;
out.field("c", &AsBytes(&self.c))
}
}

Expand Down

0 comments on commit cbbdb7f

Please sign in to comment.