Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: ansi_crossterm #801

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cursive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ required-features = ["toml"]
name = "ansi"
required-features = ["ansi"]

[[example]]
name = "colored_text"
required-features = ["ansi", "crossterm-backend"]

[[example]]
name = "builder"
required-features = ["builder"]
Expand Down
4 changes: 4 additions & 0 deletions cursive/examples/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ This example shows the effect of the `Color::TerminalDefault` setting.

This example draws a colorful square to show off true color support.

## [`colored_text`](./colored_text.rs)

This example showcasing various methods to color and remove text styles, highlighting the limitations of Crossterm's raw ANSI output.

## [`refcell_view`](./refcell_view.rs)

Here we show how to access multiple views concurrently through their name.
Expand Down
85 changes: 85 additions & 0 deletions cursive/examples/colored_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crossterm::style::Stylize;
use cursive_core::style::BaseColor::{Black, Red};
use cursive_core::style::ColorStyle;
use cursive_core::theme::Style;
use cursive_core::utils::markup::{cursup, StyledString};
use cursive_core::views::{Dialog, LinearLayout, TextView};

fn main() {
// Coloring text.
let crossterm_colored = crossterm_coloring_into_ansi("Crossterm colored text.");

let cursive_parsed_ansi = cursive_parse_ansi("Parsed ANSI");

let cursive_single_span = single_style_span("Cursive StyledString::single_span");

let cursup_markup = cursup_markup("/blue+bold{Cursup} /yellow+bold{markup}");

// Removing styles.

let cleared_by_iterating: String = iterating_spans(&cursive_parsed_ansi);

let cleared_by_canonicalize: String = canonicalize_clear(&cursive_single_span);

// Minimal application for text output
let mut siv = cursive::default();
siv.add_layer(
Dialog::new()
.content(
LinearLayout::vertical()
.child(TextView::new(crossterm_colored))
.child(TextView::new(cursive_parsed_ansi))
.child(TextView::new(cursive_single_span))
.child(TextView::new(cursup_markup))
.child(TextView::new(cleared_by_iterating))
.child(TextView::new(cleared_by_canonicalize)),
)
.button("Quit!", |s| s.quit()),
);
siv.run();
}

// Crossterm function for text styling and casting it to a string.
// Results in raw text with ANSI codes.
fn crossterm_coloring_into_ansi(str: &str) -> String {
str.red().to_string()
}

// Parsing ansi into StyledString
// https://docs.rs/cursive/latest/cursive/utils/markup/ansi/fn.parse.html
fn cursive_parse_ansi(str: &str) -> StyledString {
cursive::utils::markup::ansi::parse(str.red().to_string())
}

// Building cursive-native StyledString with StyledString::single_span (single style for entire string)
// https://docs.rs/cursive/latest/cursive/utils/markup/type.StyledString.html#method.single_span
fn single_style_span(str: &str) -> StyledString {
StyledString::single_span(
str,
Style {
effects: Default::default(),
color: ColorStyle::new(Red, Black),
},
)
}

// Cursup, a simple markup language.
// https://docs.rs/cursive/latest/cursive/utils/markup/cursup/index.html
fn cursup_markup(str: &str) -> StyledString {
cursup::parse(str)
}

// Iterating on the spans, and accumulating the span text content, ignoring the styles.
fn iterating_spans(str: &StyledString) -> String {
str.spans().map(|span| span.content).collect()
}

// Use compact or canonicalize, which rebuilds the source to only include visible text (no markup), and then get the source.
// Note that this technically modifies the StyledString, and requires mutable access.
// https://docs.rs/cursive/latest/cursive/utils/span/struct.SpannedString.html#method.canonicalize
// https://docs.rs/cursive/latest/cursive/utils/span/struct.SpannedString.html#method.compact
fn canonicalize_clear(str: &StyledString) -> String {
let mut canonicalize_clear = str.clone();
canonicalize_clear.canonicalize();
canonicalize_clear.source().to_string()
}
Loading