-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a new `build_output` module containing a `BuildOutput` interface, whose goal is to provide a standard format for outputting build information to the end user while a buildpack is running. This implementation is an evolution of the approach prototyped in the Ruby CNB: https://github.com/heroku/buildpacks-ruby/tree/dda4ede413fc3fe4d6d2f2f63f039c7c1e5cc5fd/commons/src/output
- Loading branch information
Showing
10 changed files
with
1,316 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/// Wraps each line in an ANSI escape sequence while preserving prior ANSI escape sequences. | ||
/// | ||
/// ## Why does this exist? | ||
/// | ||
/// When buildpack output is streamed to the user, each line is prefixed with `remote: ` by Git. | ||
/// Any colorization of text will apply to those prefixes which is not the desired behavior. This | ||
/// function colors lines of text while ensuring that styles are disabled at the end of each line. | ||
/// | ||
/// ## Supports recursive colorization | ||
/// | ||
/// Strings that are previously colorized will not be overridden by this function. For example, | ||
/// if a word is already colored yellow, that word will continue to be yellow. | ||
pub(crate) fn wrap_ansi_escape_each_line(ansi: &ANSI, body: impl AsRef<str>) -> String { | ||
let ansi_escape = ansi.to_str(); | ||
body.as_ref() | ||
.split('\n') | ||
// If sub contents are colorized it will contain SUBCOLOR ... RESET. After the reset, | ||
// ensure we change back to the current color | ||
.map(|line| line.replace(RESET, &format!("{RESET}{ansi_escape}"))) // Handles nested color | ||
// Set the main color for each line and reset after so we don't colorize `remote:` by accident | ||
.map(|line| format!("{ansi_escape}{line}{RESET}")) | ||
// The above logic causes redundant colors and resets, clean them up | ||
.map(|line| line.replace(&format!("{ansi_escape}{ansi_escape}"), ansi_escape)) // Reduce useless color | ||
.map(|line| line.replace(&format!("{ansi_escape}{RESET}"), "")) // Empty lines or where the nested color is at the end of the line | ||
.collect::<Vec<String>>() | ||
.join("\n") | ||
} | ||
|
||
const RESET: &str = "\x1B[0m"; | ||
const RED: &str = "\x1B[0;31m"; | ||
const YELLOW: &str = "\x1B[0;33m"; | ||
const BOLD_CYAN: &str = "\x1B[1;36m"; | ||
const BOLD_PURPLE: &str = "\x1B[1;35m"; | ||
|
||
#[derive(Debug)] | ||
#[allow(clippy::upper_case_acronyms)] | ||
pub(crate) enum ANSI { | ||
Red, | ||
Yellow, | ||
BoldCyan, | ||
BoldPurple, | ||
} | ||
|
||
impl ANSI { | ||
fn to_str(&self) -> &'static str { | ||
match self { | ||
ANSI::Red => RED, | ||
ANSI::Yellow => YELLOW, | ||
ANSI::BoldCyan => BOLD_CYAN, | ||
ANSI::BoldPurple => BOLD_PURPLE, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn empty_line() { | ||
let actual = wrap_ansi_escape_each_line(&ANSI::Red, "\n"); | ||
let expected = String::from("\n"); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn handles_nested_color_at_start() { | ||
let start = wrap_ansi_escape_each_line(&ANSI::BoldCyan, "hello"); | ||
let out = wrap_ansi_escape_each_line(&ANSI::Red, format!("{start} world")); | ||
let expected = format!("{RED}{BOLD_CYAN}hello{RESET}{RED} world{RESET}"); | ||
|
||
assert_eq!(expected, out); | ||
} | ||
|
||
#[test] | ||
fn handles_nested_color_in_middle() { | ||
let middle = wrap_ansi_escape_each_line(&ANSI::BoldCyan, "middle"); | ||
let out = wrap_ansi_escape_each_line(&ANSI::Red, format!("hello {middle} color")); | ||
let expected = format!("{RED}hello {BOLD_CYAN}middle{RESET}{RED} color{RESET}"); | ||
assert_eq!(expected, out); | ||
} | ||
|
||
#[test] | ||
fn handles_nested_color_at_end() { | ||
let end = wrap_ansi_escape_each_line(&ANSI::BoldCyan, "world"); | ||
let out = wrap_ansi_escape_each_line(&ANSI::Red, format!("hello {end}")); | ||
let expected = format!("{RED}hello {BOLD_CYAN}world{RESET}"); | ||
|
||
assert_eq!(expected, out); | ||
} | ||
|
||
#[test] | ||
fn handles_double_nested_color() { | ||
let inner = wrap_ansi_escape_each_line(&ANSI::BoldCyan, "inner"); | ||
let outer = wrap_ansi_escape_each_line(&ANSI::Red, format!("outer {inner}")); | ||
let out = wrap_ansi_escape_each_line(&ANSI::Yellow, format!("hello {outer}")); | ||
let expected = format!("{YELLOW}hello {RED}outer {BOLD_CYAN}inner{RESET}"); | ||
|
||
assert_eq!(expected, out); | ||
} | ||
|
||
#[test] | ||
fn splits_newlines() { | ||
let actual = wrap_ansi_escape_each_line(&ANSI::Red, "hello\nworld"); | ||
let expected = format!("{RED}hello{RESET}\n{RED}world{RESET}"); | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn simple_case() { | ||
let actual = wrap_ansi_escape_each_line(&ANSI::Red, "hello world"); | ||
assert_eq!(format!("{RED}hello world{RESET}"), actual); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
libherokubuildpack/src/buildpack_output/duration_format.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::time::Duration; | ||
|
||
pub(crate) fn human(duration: &Duration) -> String { | ||
let hours = (duration.as_secs() / 3600) % 60; | ||
let minutes = (duration.as_secs() / 60) % 60; | ||
let seconds = duration.as_secs() % 60; | ||
let milliseconds = duration.subsec_millis(); | ||
let tenths = milliseconds / 100; | ||
|
||
if hours > 0 { | ||
format!("{hours}h {minutes}m {seconds}s") | ||
} else if minutes > 0 { | ||
format!("{minutes}m {seconds}s") | ||
} else if seconds > 0 || milliseconds >= 100 { | ||
format!("{seconds}.{tenths}s") | ||
} else { | ||
String::from("< 0.1s") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_display_duration() { | ||
let duration = Duration::ZERO; | ||
assert_eq!(human(&duration), "< 0.1s"); | ||
|
||
let duration = Duration::from_millis(99); | ||
assert_eq!(human(&duration), "< 0.1s"); | ||
|
||
let duration = Duration::from_millis(100); | ||
assert_eq!(human(&duration), "0.1s"); | ||
|
||
let duration = Duration::from_millis(210); | ||
assert_eq!(human(&duration), "0.2s"); | ||
|
||
let duration = Duration::from_millis(1100); | ||
assert_eq!(human(&duration), "1.1s"); | ||
|
||
let duration = Duration::from_millis(9100); | ||
assert_eq!(human(&duration), "9.1s"); | ||
|
||
let duration = Duration::from_millis(10100); | ||
assert_eq!(human(&duration), "10.1s"); | ||
|
||
let duration = Duration::from_millis(52100); | ||
assert_eq!(human(&duration), "52.1s"); | ||
|
||
let duration = Duration::from_millis(60 * 1000); | ||
assert_eq!(human(&duration), "1m 0s"); | ||
|
||
let duration = Duration::from_millis(60 * 1000 + 2000); | ||
assert_eq!(human(&duration), "1m 2s"); | ||
|
||
let duration = Duration::from_millis(60 * 60 * 1000 - 1); | ||
assert_eq!(human(&duration), "59m 59s"); | ||
|
||
let duration = Duration::from_millis(60 * 60 * 1000); | ||
assert_eq!(human(&duration), "1h 0m 0s"); | ||
|
||
let duration = Duration::from_millis(75 * 60 * 1000 - 1); | ||
assert_eq!(human(&duration), "1h 14m 59s"); | ||
} | ||
} |
Oops, something went wrong.