Skip to content

Commit

Permalink
2024/10/27-16:54:44 (Linux VDI0092.zit.bam.de x86_64)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbenner committed Oct 27, 2024
1 parent 79b2b37 commit b9f40a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/granges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ impl PartialEq for GRanges {
/* -------------------------------------------------------------------------- */

impl fmt::Display for GRanges {
/// Formats the `GRanges` instance as a string for display.
///
/// This method implements the `Display` trait, allowing the `GRanges` instance to be easily printed using
/// formatting macros like `println!`. It calls `format_pretty` to obtain the formatted string representation
/// and pads it for display.
///
/// # Arguments
/// - `f`: A mutable reference to a `Formatter`, which is used to write the formatted string.
///
/// # Returns
/// A `fmt::Result`, which indicates the success or failure of the formatting operation.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&format!("{}", self.format_pretty(10).unwrap()))
}
Expand Down
22 changes: 22 additions & 0 deletions src/granges_pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ use crate::granges::GRanges;

impl GRanges {

/// Prints the contents of the `GRanges` instance in a pretty-printed format to the provided writer.
///
/// This method formats the `GRanges` data into a human-readable string and writes it to the specified
/// output stream. It calculates the maximum widths of each column to ensure proper alignment in the output.
///
/// # Arguments
/// - `writer`: A mutable reference to a writer that implements the `Write` trait, where the formatted output will be written.
/// - `n`: The maximum number of rows to print. This allows for controlling the output size and is useful for
/// displaying a limited number of rows.
///
/// # Returns
/// An `io::Result<()>`, which will be `Ok(())` if the operation succeeds or an error if the writing fails.
fn print_pretty<W: Write>(&self, writer: &mut W, n: usize) -> io::Result<()> {
let meta_str = format!("{}", self.meta);
let mut meta_reader = BufReader::new(meta_str.as_bytes());
Expand All @@ -45,6 +57,16 @@ impl GRanges {
Ok(())
}

/// Formats the contents of the `GRanges` instance into a pretty-printed string.
///
/// This method collects the formatted representation of the `GRanges` data into a string, which can be
/// useful for displaying or logging purposes. It internally calls `print_pretty` to handle the formatting.
///
/// # Arguments
/// - `n`: The maximum number of rows to include in the formatted output.
///
/// # Returns
/// An `io::Result<String>` containing the formatted string if successful, or an error if the formatting fails.
pub fn format_pretty(&self, n: usize) -> io::Result<String> {
let mut buffer = Vec::new();
{
Expand Down

0 comments on commit b9f40a4

Please sign in to comment.