Skip to content

Commit

Permalink
feat(termion): implement from termion Bg and Fg
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 committed Dec 15, 2023
1 parent 5e1cad9 commit 5bead48
Showing 1 changed file with 211 additions and 13 deletions.
224 changes: 211 additions & 13 deletions src/backend/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use std::{
io::{self, Write},
};

use termion::{color as tcolor, style as tstyle};

use crate::{
backend::{Backend, ClearType, WindowSize},
buffer::Cell,
prelude::Rect,
style::{Color, Modifier},
style::{Color, Modifier, Style},
};

/// A [`Backend`] implementation that uses [Termion] to render to the terminal.
Expand Down Expand Up @@ -276,11 +278,23 @@ impl fmt::Display for Bg {

macro_rules! from_termion_for_color {
($termion_color:ident, $color: ident) => {
impl From<termion::color::$termion_color> for Color {
fn from(_: termion::color::$termion_color) -> Self {
impl From<tcolor::$termion_color> for Color {
fn from(_: tcolor::$termion_color) -> Self {
Color::$color
}
}

impl From<tcolor::Bg<tcolor::$termion_color>> for Style {
fn from(_: tcolor::Bg<tcolor::$termion_color>) -> Self {
Style::default().bg(Color::$color)
}
}

impl From<tcolor::Fg<tcolor::$termion_color>> for Style {
fn from(_: tcolor::Fg<tcolor::$termion_color>) -> Self {
Style::default().fg(Color::$color)
}
}
};
}

Expand All @@ -302,18 +316,42 @@ from_termion_for_color!(LightMagenta, LightMagenta);
from_termion_for_color!(LightCyan, LightCyan);
from_termion_for_color!(LightWhite, White);

impl From<termion::color::AnsiValue> for Color {
fn from(value: termion::color::AnsiValue) -> Self {
impl From<tcolor::AnsiValue> for Color {
fn from(value: tcolor::AnsiValue) -> Self {
Color::Indexed(value.0)
}
}

impl From<termion::color::Rgb> for Color {
fn from(value: termion::color::Rgb) -> Self {
impl From<tcolor::Bg<tcolor::AnsiValue>> for Style {
fn from(value: tcolor::Bg<tcolor::AnsiValue>) -> Self {
Style::default().bg(Color::Indexed(value.0 .0))
}
}

impl From<tcolor::Fg<tcolor::AnsiValue>> for Style {
fn from(value: tcolor::Fg<tcolor::AnsiValue>) -> Self {
Style::default().fg(Color::Indexed(value.0 .0))
}
}

impl From<tcolor::Rgb> for Color {
fn from(value: tcolor::Rgb) -> Self {
Color::Rgb(value.0, value.1, value.2)
}
}

impl From<tcolor::Bg<tcolor::Rgb>> for Style {
fn from(value: tcolor::Bg<tcolor::Rgb>) -> Self {
Style::default().bg(Color::Rgb(value.0 .0, value.0 .1, value.0 .2))
}
}

impl From<tcolor::Fg<tcolor::Rgb>> for Style {
fn from(value: tcolor::Fg<tcolor::Rgb>) -> Self {
Style::default().fg(Color::Rgb(value.0 .0, value.0 .1, value.0 .2))
}
}

impl fmt::Display for ModifierDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let remove = self.from - self.to;
Expand Down Expand Up @@ -381,8 +419,8 @@ impl fmt::Display for ModifierDiff {

macro_rules! from_termion_for_modifier {
($termion_modifier:ident, $modifier: ident) => {
impl From<termion::style::$termion_modifier> for Modifier {
fn from(_: termion::style::$termion_modifier) -> Self {
impl From<tstyle::$termion_modifier> for Modifier {
fn from(_: tstyle::$termion_modifier) -> Self {
Modifier::$modifier
}
}
Expand All @@ -409,8 +447,6 @@ mod tests {

#[test]
fn from_termion_color() {
use termion::color as tcolor;

assert_eq!(Color::from(tcolor::Reset), Color::Reset);
assert_eq!(Color::from(tcolor::Black), Color::Black);
assert_eq!(Color::from(tcolor::Red), Color::Red);
Expand All @@ -433,9 +469,171 @@ mod tests {
}

#[test]
fn from_termion_style() {
use termion::style as tstyle;
fn from_termion_bg() {
use tcolor::Bg;

assert_eq!(
Style::from(Bg(tcolor::Reset)),
Style::default().bg(Color::Reset)
);
assert_eq!(
Style::from(Bg(tcolor::Black)),
Style::default().bg(Color::Black)
);
assert_eq!(
Style::from(Bg(tcolor::Red)),
Style::default().bg(Color::Red)
);
assert_eq!(
Style::from(Bg(tcolor::Green)),
Style::default().bg(Color::Green)
);
assert_eq!(
Style::from(Bg(tcolor::Yellow)),
Style::default().bg(Color::Yellow)
);
assert_eq!(
Style::from(Bg(tcolor::Blue)),
Style::default().bg(Color::Blue)
);
assert_eq!(
Style::from(Bg(tcolor::Magenta)),
Style::default().bg(Color::Magenta)
);
assert_eq!(
Style::from(Bg(tcolor::Cyan)),
Style::default().bg(Color::Cyan)
);
assert_eq!(
Style::from(Bg(tcolor::White)),
Style::default().bg(Color::Gray)
);
assert_eq!(
Style::from(Bg(tcolor::LightBlack)),
Style::default().bg(Color::DarkGray)
);
assert_eq!(
Style::from(Bg(tcolor::LightRed)),
Style::default().bg(Color::LightRed)
);
assert_eq!(
Style::from(Bg(tcolor::LightGreen)),
Style::default().bg(Color::LightGreen)
);
assert_eq!(
Style::from(Bg(tcolor::LightBlue)),
Style::default().bg(Color::LightBlue)
);
assert_eq!(
Style::from(Bg(tcolor::LightYellow)),
Style::default().bg(Color::LightYellow)
);
assert_eq!(
Style::from(Bg(tcolor::LightMagenta)),
Style::default().bg(Color::LightMagenta)
);
assert_eq!(
Style::from(Bg(tcolor::LightCyan)),
Style::default().bg(Color::LightCyan)
);
assert_eq!(
Style::from(Bg(tcolor::LightWhite)),
Style::default().bg(Color::White)
);
assert_eq!(
Style::from(Bg(tcolor::AnsiValue(31))),
Style::default().bg(Color::Indexed(31))
);
assert_eq!(
Style::from(Bg(tcolor::Rgb(1, 2, 3))),
Style::default().bg(Color::Rgb(1, 2, 3))
);
}

#[test]
fn from_termion_fg() {
use tcolor::Fg;

assert_eq!(
Style::from(Fg(tcolor::Reset)),
Style::default().fg(Color::Reset)
);
assert_eq!(
Style::from(Fg(tcolor::Black)),
Style::default().fg(Color::Black)
);
assert_eq!(
Style::from(Fg(tcolor::Red)),
Style::default().fg(Color::Red)
);
assert_eq!(
Style::from(Fg(tcolor::Green)),
Style::default().fg(Color::Green)
);
assert_eq!(
Style::from(Fg(tcolor::Yellow)),
Style::default().fg(Color::Yellow)
);
assert_eq!(
Style::from(Fg(tcolor::Blue)),
Style::default().fg(Color::Blue)
);
assert_eq!(
Style::from(Fg(tcolor::Magenta)),
Style::default().fg(Color::Magenta)
);
assert_eq!(
Style::from(Fg(tcolor::Cyan)),
Style::default().fg(Color::Cyan)
);
assert_eq!(
Style::from(Fg(tcolor::White)),
Style::default().fg(Color::Gray)
);
assert_eq!(
Style::from(Fg(tcolor::LightBlack)),
Style::default().fg(Color::DarkGray)
);
assert_eq!(
Style::from(Fg(tcolor::LightRed)),
Style::default().fg(Color::LightRed)
);
assert_eq!(
Style::from(Fg(tcolor::LightGreen)),
Style::default().fg(Color::LightGreen)
);
assert_eq!(
Style::from(Fg(tcolor::LightBlue)),
Style::default().fg(Color::LightBlue)
);
assert_eq!(
Style::from(Fg(tcolor::LightYellow)),
Style::default().fg(Color::LightYellow)
);
assert_eq!(
Style::from(Fg(tcolor::LightMagenta)),
Style::default().fg(Color::LightMagenta)
);
assert_eq!(
Style::from(Fg(tcolor::LightCyan)),
Style::default().fg(Color::LightCyan)
);
assert_eq!(
Style::from(Fg(tcolor::LightWhite)),
Style::default().fg(Color::White)
);
assert_eq!(
Style::from(Fg(tcolor::AnsiValue(31))),
Style::default().fg(Color::Indexed(31))
);
assert_eq!(
Style::from(Fg(tcolor::Rgb(1, 2, 3))),
Style::default().fg(Color::Rgb(1, 2, 3))
);
}

#[test]
fn from_termion_style() {
assert_eq!(Modifier::from(tstyle::Invert), Modifier::REVERSED);
assert_eq!(Modifier::from(tstyle::Bold), Modifier::BOLD);
assert_eq!(Modifier::from(tstyle::Italic), Modifier::ITALIC);
Expand Down

0 comments on commit 5bead48

Please sign in to comment.