Skip to content

Commit

Permalink
Merge pull request #1 from schneems/schneems/style
Browse files Browse the repository at this point in the history
Add fn important style
  • Loading branch information
schneems authored Jun 3, 2024
2 parents a1c48db + 371f296 commit 7ad6b0b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 40 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CHANGELOG

## Unreleased

## v0.1.0 - 2024/06/03

- First
15 changes: 5 additions & 10 deletions examples/style_guide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,11 @@ fn main() {
"style::details(\"extra information\")".to_string(),
"Add specific information at the end of a line i.e. 'Cache cleared (ruby version changed)'".to_string()
],
// vec![
// fmt::HELP.to_string(),
// "fmt::HELP.to_string()".to_string(),
// "A help prefix, use it in a step or section title".to_string()
// ],
// vec![
// fmt::DEBUG_INFO.to_string(),
// "fmt::DEBUG_INFO.to_string()".to_string(),
// "A debug prefix, use it in a step or section title".to_string()
// ]
vec![
style::important("HELP:").to_string(),
"style::important(\"HELP:\").to_string()".to_string(),
"Call attention to individual words, useful when you want to emphasize a prefix but not the whole line.".to_string()
],
];

table.print(data);
Expand Down
3 changes: 3 additions & 0 deletions src/ansi_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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";
const BOLD_UNDERLINE_CYAN: &str = "\x1B[1;4;36m";
const DIM: &str = "\x1B[2;1m"; // Default color but softer/less vibrant

#[derive(Debug)]
Expand All @@ -40,6 +41,7 @@ pub(crate) enum ANSI {
Red,
Yellow,
BoldCyan,
BoldUnderlineCyan,
BoldPurple,
}

Expand All @@ -51,6 +53,7 @@ impl ANSI {
ANSI::Yellow => YELLOW,
ANSI::BoldCyan => BOLD_CYAN,
ANSI::BoldPurple => BOLD_PURPLE,
ANSI::BoldUnderlineCyan => BOLD_UNDERLINE_CYAN,
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub mod state {
/// This state is intended for long-running tasks that do not stream but wish to convey progress
/// to the end user. For example, while downloading a file.
///
/// This state is started from a [`state::SubBullet`] and finished back to a [`state::SubBullet`].
/// This state is started from a [`SubBullet`] and finished back to a [`SubBullet`].
///
/// ```rust
/// use bullet_stream::{Output, state::{Bullet, SubBullet}};
Expand Down Expand Up @@ -277,7 +277,7 @@ where
/// [`Output::error`] instead.
///
/// Warnings will be output in a multi-line paragraph style. A warning can be emitted from any
/// state except for [`state::NotStarted`].
/// state except for [`state::Header`].
#[must_use]
pub fn warning(mut self, s: impl AsRef<str>) -> Output<S> {
self.write_paragraph(&ANSI::Yellow, s);
Expand Down Expand Up @@ -334,7 +334,7 @@ where
{
/// Create a buildpack output struct, but do not announce the buildpack's start.
///
/// See the [`Output::start`] method for more details.
/// See the [`Output::h1`] and [`Output::h2`] methods for more details.
#[must_use]
pub fn new(io: W) -> Self {
Self {
Expand Down Expand Up @@ -478,7 +478,7 @@ where
/// Finalize a timer's output.
///
/// Once you're finished with your long running task, calling this function
/// finalizes the timer's output and transitions back to a [`state::Section`].
/// finalizes the timer's output and transitions back to a [`state::SubBullet`].
#[must_use]
pub fn done(self) -> Output<state::SubBullet<W>> {
let duration = self.state.started.elapsed();
Expand Down Expand Up @@ -531,7 +531,7 @@ where
/// observable by the user through the buildpack output. For example, if a cache needs to be
/// cleared, emit that your buildpack is clearing it and why.
///
/// Multiple steps are allowed within a section. This function returns to the same [`state::Section`].
/// Multiple steps are allowed within a section. This function returns to the same [`state::SubBullet`].
#[must_use]
pub fn sub_bullet(mut self, s: impl AsRef<str>) -> Output<state::SubBullet<W>> {
writeln_now(&mut self.state.write, Self::style(s));
Expand Down
14 changes: 13 additions & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::ansi_escape::{self, ANSI};

/// Decorate a URL for the build output.
pub fn url(contents: impl AsRef<str>) -> String {
ansi_escape::wrap_ansi_escape_each_line(&ANSI::BoldCyan, contents)
ansi_escape::wrap_ansi_escape_each_line(&ANSI::BoldUnderlineCyan, contents)
}

/// Decorate the name of a command being run i.e. `bundle install`.
Expand All @@ -26,3 +26,15 @@ pub fn details(contents: impl AsRef<str>) -> String {
let contents = contents.as_ref();
format!("({contents})")
}

/// Decorate important information.
///
/// ```
/// use bullet_stream::style;
///
/// let help = style::important("HELP:");
/// format!("{help} review the logs");
/// ```
pub fn important(contents: impl AsRef<str>) -> String {
ansi_escape::wrap_ansi_escape_each_line(&ANSI::BoldCyan, contents)
}
24 changes: 0 additions & 24 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::sync::Arc;
/// Constructs a writer that buffers written data until given marker byte is encountered and
/// then applies the given mapping function to the data before passing the result to the wrapped
/// writer.
///
/// See the [`mappers`] module for a collection of commonly used mappers.
pub fn mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>(
w: W,
marker_byte: u8,
Expand All @@ -19,8 +17,6 @@ pub fn mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>
/// Constructs a writer that buffers written data until an ASCII/UTF-8 newline byte (`b'\n'`) is
/// encountered and then applies the given mapping function to the data before passing the result to
/// the wrapped writer.
///
/// See the [`mappers`] module for a collection of commonly used mappers.
pub fn line_mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>(
w: W,
f: F,
Expand All @@ -47,13 +43,6 @@ pub struct MappedWrite<W: io::Write> {
mapping_fn: Arc<dyn (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send>,
}

/// A tee writer that was created with the [`tee`] function.
#[derive(Debug, Clone)]
pub struct TeeWrite<A: io::Write, B: io::Write> {
inner_a: A,
inner_b: B,
}

impl<W> MappedWrite<W>
where
W: io::Write,
Expand Down Expand Up @@ -134,19 +123,6 @@ impl<W: io::Write + Debug> Debug for MappedWrite<W> {
}
}

impl<A: io::Write, B: io::Write> io::Write for TeeWrite<A, B> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner_a.write_all(buf)?;
self.inner_b.write_all(buf)?;
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
self.inner_a.flush()?;
self.inner_b.flush()
}
}

#[cfg(test)]
mod test {
use crate::write::line_mapped;
Expand Down

0 comments on commit 7ad6b0b

Please sign in to comment.