Skip to content

Commit

Permalink
feat: add ansi to ratatui feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham committed Nov 2, 2024
1 parent 34d98a0 commit 6c0e790
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 15 additions & 1 deletion examples/ratatui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> io::Result<()> {
},
)?;
for flavor in &PALETTE {
terminal.insert_before(4, |buf| {
terminal.insert_before(8, |buf| {
let analogous: Vec<Span> = flavor
.colors
.into_iter()
Expand All @@ -28,11 +28,25 @@ fn main() -> io::Result<()> {
.filter(|c| !c.accent)
.map(|c| "██".fg(*c)) // fg accepts any type that implements Into<Color>
.collect();
let ansi_normals: Vec<Span> = flavor
.ansi_colors
.into_iter()
.map(|c| "██".fg(c.normal)) // fg accepts any type that implements Into<Color>
.collect::<Vec<Span>>();
let ansi_brights: Vec<Span> = flavor
.ansi_colors
.into_iter()
.map(|c| "██".fg(c.bright)) // fg accepts any type that implements Into<Color>
.collect::<Vec<Span>>();

let width = buf.area.width;
Paragraph::new(flavor.name.to_string()).render(Rect::new(0, 0, width, 1), buf);
Paragraph::new(Line::from(analogous)).render(Rect::new(0, 1, width, 1), buf);
Paragraph::new(Line::from(monochromatic)).render(Rect::new(0, 2, width, 1), buf);
Paragraph::new(format!("{} ANSI", flavor.name.to_string()))
.render(Rect::new(0, 4, width, 1), buf);
Paragraph::new(Line::from(ansi_normals)).render(Rect::new(0, 5, width, 1), buf);
Paragraph::new(Line::from(ansi_brights)).render(Rect::new(0, 6, width, 1), buf);
})?;
}

Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,18 @@ impl Color {
}

#[cfg(feature = "ratatui")]
impl From<Color> for ratatui::style::Color {
fn from(value: Color) -> Self {
Self::Rgb(value.rgb.r, value.rgb.g, value.rgb.b)
mod ratatui {
use crate::{AnsiColor, Color};

impl From<Color> for ratatui::style::Color {
fn from(value: Color) -> Self {
Self::Rgb(value.rgb.r, value.rgb.g, value.rgb.b)
}
}

impl From<AnsiColor> for ratatui::style::Color {
fn from(value: AnsiColor) -> Self {
Self::Rgb(value.rgb.r, value.rgb.g, value.rgb.b)
}
}
}

0 comments on commit 6c0e790

Please sign in to comment.