diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-10-28 11:03:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-28 11:03:33 -0400 |
commit | 56d5cb21b01c1f99be37856a6d73aded230fa487 (patch) | |
tree | 1fba3c1fca72469af390579e24aef9c911a8a1c9 /cli/display.rs | |
parent | ec09134d8aab3d84db28d6469789c5581d99fec9 (diff) |
refactor: move `deno info` functionality from deno_graph to CLI (#16434)
Closes #16423
Diffstat (limited to 'cli/display.rs')
-rw-r--r-- | cli/display.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/cli/display.rs b/cli/display.rs index bcd23f47c..f13965e28 100644 --- a/cli/display.rs +++ b/cli/display.rs @@ -1,5 +1,9 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use deno_core::error::AnyError; +use deno_core::serde_json; +use std::io::Write; + /// A function that converts a float to a string the represents a human /// readable version of that number. pub fn human_size(size: f64) -> String { @@ -38,6 +42,30 @@ pub fn human_elapsed(elapsed: u128) -> String { format!("{}m{}s", minutes, seconds_remainder) } +pub fn write_to_stdout_ignore_sigpipe( + bytes: &[u8], +) -> Result<(), std::io::Error> { + use std::io::ErrorKind; + + match std::io::stdout().write_all(bytes) { + Ok(()) => Ok(()), + Err(e) => match e.kind() { + ErrorKind::BrokenPipe => Ok(()), + _ => Err(e), + }, + } +} + +pub fn write_json_to_stdout<T>(value: &T) -> Result<(), AnyError> +where + T: ?Sized + serde::ser::Serialize, +{ + let mut writer = std::io::BufWriter::new(std::io::stdout()); + serde_json::to_writer_pretty(&mut writer, value)?; + writeln!(&mut writer)?; + Ok(()) +} + #[cfg(test)] mod tests { use super::*; |