Skip to content

Commit

Permalink
feat: support Rust regex engine, change help styling, many small impr…
Browse files Browse the repository at this point in the history
…ovements
  • Loading branch information
Aloso committed Dec 12, 2024
1 parent 9dfa7fd commit ecbf6ad
Show file tree
Hide file tree
Showing 20 changed files with 850 additions and 484 deletions.
32 changes: 3 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 0 additions & 69 deletions helptext/src/color.rs

This file was deleted.

44 changes: 22 additions & 22 deletions helptext/src/help.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{self, Write};

use crate::Color;
use crate::Style;

/// A structured help message.
///
Expand All @@ -9,9 +9,9 @@ use crate::Color;
pub struct Help<'a>(pub &'a [HelpSection<'a>]);

impl Help<'_> {
pub fn write(&self, buf: &mut impl Write, long: bool, colored: bool) -> io::Result<()> {
pub fn write(&self, buf: &mut impl Write, long: bool, styled: bool) -> io::Result<()> {
for section in self.0 {
section.write(buf, long, colored, 0, false)?;
section.write(buf, long, styled, 0, false)?;
}
Ok(())
}
Expand All @@ -35,19 +35,19 @@ impl HelpSection<'_> {
&self,
buf: &mut impl Write,
long: bool,
colored: bool,
styled: bool,
indent: usize,
same_line: bool,
) -> io::Result<bool> {
match *self {
HelpSection::Short(section) => {
if !long {
return section.write(buf, long, colored, indent, same_line);
return section.write(buf, long, styled, indent, same_line);
}
}
HelpSection::Long(section) => {
if long {
return section.write(buf, long, colored, indent, same_line);
return section.write(buf, long, styled, indent, same_line);
}
}
HelpSection::Text(segments) => {
Expand All @@ -57,7 +57,7 @@ impl HelpSection<'_> {
)?;
}
for segment in segments {
segment.write(&mut *buf, colored, indent)?;
segment.write(&mut *buf, styled, indent)?;
}
buf.write_all(b"\n")?;
return Ok(true);
Expand All @@ -67,10 +67,10 @@ impl HelpSection<'_> {
&b"\n "[..indent + 1],
)?;

if colored {
buf.write_all(Color::ANSI_Y.as_bytes())?;
if styled {
buf.write_all(Style::ANSI_UB.as_bytes())?;
buf.write_all(name.as_bytes())?;
buf.write_all(Color::ANSI_RESET.as_bytes())?;
buf.write_all(Style::ANSI_RESET.as_bytes())?;
} else {
buf.write_all(name.as_bytes())?;
}
Expand All @@ -84,7 +84,7 @@ impl HelpSection<'_> {
&b" "[..new_indent],
)?;
}
line_written |= section.write(buf, long, colored, new_indent, false)?;
line_written |= section.write(buf, long, styled, new_indent, false)?;
}
return Ok(line_written);
}
Expand Down Expand Up @@ -114,10 +114,10 @@ impl HelpSection<'_> {
&b" "[..indent],
)?;

if colored {
buf.write_all(Color::ANSI_G.as_bytes())?;
if styled {
buf.write_all(Style::ANSI_G.as_bytes())?;
buf.write_all(key.as_bytes())?;
buf.write_all(Color::ANSI_RESET.as_bytes())?;
buf.write_all(Style::ANSI_RESET.as_bytes())?;
} else {
buf.write_all(key.as_bytes())?;
}
Expand All @@ -136,7 +136,7 @@ impl HelpSection<'_> {
line_written |= section.write(
buf,
long,
colored,
styled,
new_indent,
is_small && !line_written,
)?;
Expand All @@ -162,7 +162,7 @@ pub enum TableMode {

#[derive(Debug, Clone)]
pub struct Segment<'a> {
pub style: Option<Color>,
pub style: Option<Style>,
pub text: &'a str,
pub ticks: bool,
}
Expand All @@ -172,10 +172,10 @@ impl<'a> Segment<'a> {
Segment { style: None, text, ticks: false }
}

pub fn write(&self, buf: &mut impl Write, colored: bool, indent: usize) -> io::Result<()> {
if let Some(color) = &self.style {
if colored {
buf.write_all(color.ansi_code().as_bytes())?;
pub fn write(&self, buf: &mut impl Write, styled: bool, indent: usize) -> io::Result<()> {
if let Some(style) = &self.style {
if styled {
buf.write_all(style.ansi_code().as_bytes())?;
} else if self.ticks {
buf.write_all(b"`")?;
}
Expand All @@ -193,8 +193,8 @@ impl<'a> Segment<'a> {
}

if self.style.is_some() {
if colored {
buf.write_all(Color::ANSI_RESET.as_bytes())?;
if styled {
buf.write_all(Style::ANSI_RESET.as_bytes())?;
} else if self.ticks {
buf.write_all(b"`")?;
}
Expand Down
18 changes: 9 additions & 9 deletions helptext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! The syntax of the help message is explained in the [`sections`] macro.
//!
//! The syntax for colorizing text is explained in the [`text`] macro.
//! The syntax for styling text is explained in the [`text`] macro.
//!
//! ```
//! use helptext::{Help, sections};
Expand All @@ -11,10 +11,10 @@
//! ["my-cool-program " {env!("CARGO_PKG_VERSION")}]
//! ["Use " c:"-h" " for short descriptions and " c:"--help" " for more details."]
//! []
//! "USAGE" {
//! "Usage" {
//! ["my-cool-program [OPTIONS] <INPUT>"]
//! }
//! "OPTIONS" {
//! "Options" {
//! table Auto {
//! "-h, --help" => {
//! ["Print help information"]
Expand Down Expand Up @@ -51,19 +51,19 @@
//! }
//! ));
//!
//! fn print_short_help(use_colors: bool) {
//! fn print_short_help(use_styles: bool) {
//! HELP.write(
//! &mut std::io::stdout().lock(),
//! false, // don't show long help
//! use_colors,
//! use_styles,
//! );
//! }
//!
//! fn print_long_help(use_colors: bool) {
//! fn print_long_help(use_styles: bool) {
//! HELP.write(
//! &mut std::io::stdout().lock(),
//! true, // show long help
//! use_colors,
//! use_styles,
//! );
//! }
//! ```
Expand All @@ -74,9 +74,9 @@
//!
//! ![Long help](https://raw.githubusercontent.com/pomsky-lang/pomsky/main/helptext/docs/long_help.png)
mod color;
mod help;
mod macros;
mod style;

pub use color::Color;
pub use help::*;
pub use style::Style;
31 changes: 16 additions & 15 deletions helptext/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
#[macro_export]
macro_rules! text_impl {
// c:"text"
([$color_id:ident : $lit:literal $($rest:tt)*] $($done:tt)*) => {
([$style_id:ident : $lit:literal $($rest:tt)*] $($done:tt)*) => {
$crate::text_impl!([$($rest)*] $($done)*, $crate::Segment {
style: Some($crate::Color::$color_id), text: $lit, ticks: true
style: Some($crate::Style::$style_id), text: $lit, ticks: true
})
};
// c:{expr}
([$color_id:ident : {$ex:expr} $($rest:tt)*] $($done:tt)*) => {
([$style_id:ident : {$ex:expr} $($rest:tt)*] $($done:tt)*) => {
$crate::text_impl!([$($rest)*] $($done)*, $crate::Segment {
style: Some($crate::Color::$color_id), text: $ex, ticks: true
style: Some($crate::Style::$style_id), text: $ex, ticks: true
})
};
// c!"text"
([$color_id:ident ! $lit:literal $($rest:tt)*] $($done:tt)*) => {
([$style_id:ident ! $lit:literal $($rest:tt)*] $($done:tt)*) => {
$crate::text_impl!([$($rest)*] $($done)*, $crate::Segment {
style: Some($crate::Color::$color_id), text: $lit, ticks: false
style: Some($crate::Style::$style_id), text: $lit, ticks: false
})
};
// c!{expr}
([$color_id:ident ! {$ex:expr} $($rest:tt)*] $($done:tt)*) => {
([$style_id:ident ! {$ex:expr} $($rest:tt)*] $($done:tt)*) => {
$crate::text_impl!([$($rest)*] $($done)*, $crate::Segment {
style: Some($crate::Color::$color_id), text: $ex, ticks: false
style: Some($crate::Style::$style_id), text: $ex, ticks: false
})
};
// "text"
Expand Down Expand Up @@ -131,14 +131,15 @@ macro_rules! sections_impl {
///
/// Each segment can be preceded by one of
///
/// - `c:`, where `c` is a [`Color`](crate::Color) variant; the segment is
/// printed in color if supported, otherwise it is wrapped in backticks
/// - `c!`, where `c` is a [`Color`](crate::Color) variant; the segment is
/// printed in color if supported, otherwise no formatting is applied
/// - `c:`, where `c` is a [`Style`](crate::Style) variant; the segment is
/// styled if supported, otherwise it is wrapped in backticks
/// - `c!`, where `c` is a [`Style`](crate::Style) variant; the segment is
/// styled if supported, otherwise no formatting is applied
///
/// Each color can be abbreviated with its first letter (cyan ➔ c, green ➔ g,
/// Each color style can be abbreviated with its first letter (cyan ➔ c, green ➔ g,
/// magenta ➔ m, red ➔ r, yellow ➔ y); use an uppercase letter to make it
/// bold (bold cyan ➔ C, etc.)
/// bold (bold cyan ➔ C, etc.). The `Underline` and `UnderlineBold` styles
/// are abbreviated as `u` and `U`, respectively.
///
/// Segments are _not_ separated with commas, for example:
///
Expand Down Expand Up @@ -180,7 +181,7 @@ macro_rules! text {
///
/// ```
/// # helptext::sections!(
/// "USAGE" {
/// "Usage" {
/// ["section 1"]
/// ["section 2"]
/// }
Expand Down
Loading

0 comments on commit ecbf6ad

Please sign in to comment.