Commit 65c16ca8 authored by Moritz Sokoll's avatar Moritz Sokoll 🦀
Browse files

0.3.2: added special effects, removed deprecated code, reworked structure

parent 84d4e2f2
[package]
name = "btui"
version = "0.2.1"
version = "0.3.2"
edition = "2018"
authors = ["Moritz Sokoll <moritz@sokoll.com>"]
license-file = "COPYING"
......
......@@ -5,5 +5,5 @@
## Usage
Add this to your `Cargo.toml` dependency list:
```toml
btui = "0.2.1"
btui = "0.3.2"
```
pub mod colors {
/// enum to represent colored text
#[deprecated(
since = "0.2.1",
note = "older bulkier way of writing colored text. Please use `Color` instead"
)]
pub enum ColoredText {
Black(String),
Red(String),
Green(String),
Yellow(String),
Blue(String),
Magenta(String),
Cyan(String),
White(String),
}
/// enum to represent a background color
#[deprecated(
since = "0.2.1",
note = "older bulkier way of writing colored text. Please use `Color` instead"
)]
pub enum ColoredBg {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
}
/// module containing enums to represent terminal effects
pub mod effects {
/// enum to represent any color
pub enum Color {
Black,
......@@ -41,118 +10,98 @@ pub mod colors {
Magenta,
Cyan,
White,
}
/// enum to represent special printings other than colors
pub enum Special {
Bold,
Underline,
Stroke,
Italic,
Reset,
}
}
pub mod formatting {
use crate::ft::colors::*;
use crate::Variant;
/// module for giving printable escape sequences
pub mod print {
use crate::effects::Color;
use crate::effects::Special;
/// format text with colors to be printed
/// change foreground color
///
/// # Arguments
///
/// * `text`: the text in form of a vector of either colored text or colored background
/// *`col`: color to use
///
/// # Returns
/// the color code to be printed to the terminal
///
/// the formatted text with escape characters to be printed to the terminal
#[deprecated(
since = "0.2.1",
note = "older bulkier way for coloring. Use `print`'s functions instead"
)]
pub fn format(text: Vec<Variant<ColoredText, ColoredBg>>) -> String {
let mut out: String = String::new();
for elm in text {
let add: String = match elm {
Variant::One(n) => match n {
ColoredText::Black(p) => format!("\x1b[30m{}", p),
ColoredText::Red(p) => format!("\x1b[31m{}", p),
ColoredText::Green(p) => format!("\x1b[32m{}", p),
ColoredText::Yellow(p) => format!("\x1b[33m{}", p),
ColoredText::Blue(p) => format!("\x1b[34m{}", p),
ColoredText::Magenta(p) => format!("\x1b[35m{}", p),
ColoredText::Cyan(p) => format!("\x1b[36m{}", p),
ColoredText::White(p) => format!("\x1b[37m{}", p),
},
Variant::Two(n) => match n {
ColoredBg::Black => String::from("\x1b[40m"),
ColoredBg::Red => String::from("\x1b[41m"),
ColoredBg::Green => String::from("\x1b[42m"),
ColoredBg::Yellow => String::from("\x1b[43m"),
ColoredBg::Blue => String::from("\x1b[44m"),
ColoredBg::Magenta => String::from("\x1b[45m"),
ColoredBg::Cyan => String::from("\x1b[46m"),
ColoredBg::White => String::from("\x1b[47m"),
},
};
out.push_str(add.as_str());
/// # Example
/// ```rust
/// use btui::print::fg;
/// use btui::effects::Color;
/// println!("{}test", fg(Color::Red));
/// ```
pub fn fg(col: Color) -> String {
match col {
Color::Black => String::from("\x1b[30m"),
Color::Red => String::from("\x1b[31m"),
Color::Green => String::from("\x1b[32m"),
Color::Yellow => String::from("\x1b[33m"),
Color::Blue => String::from("\x1b[34m"),
Color::Magenta => String::from("\x1b[35m"),
Color::Cyan => String::from("\x1b[36m"),
Color::White => String::from("\x1b[37m"),
}
out.push_str("\x1b[0m");
out
}
/// new module for colored output
pub mod print {
use crate::ft::colors::Color;
/// change foreground color
///
/// # Arguments
/// *`col`: color to use
///
/// # Returns
/// the color code to be printed to the terminal
///
/// # Example
/// ```rust
/// use btui::ft::formatting::print::fg;
/// use btui::colors::Color;
/// println!("{}test{}", fg(Color::Red), fg(Color::Reset));
/// ```
pub fn fg(col: Color) -> String {
match col {
Color::Black => String::from("\x1b[30m"),
Color::Red => String::from("\x1b[31m"),
Color::Green => String::from("\x1b[32m"),
Color::Yellow => String::from("\x1b[33m"),
Color::Blue => String::from("\x1b[34m"),
Color::Magenta => String::from("\x1b[35m"),
Color::Cyan => String::from("\x1b[36m"),
Color::White => String::from("\x1b[37m"),
Color::Reset => String::from("\x1b[0m"),
}
/// change background color
///
/// # Arguments
/// *`col`: color to use
///
/// # Returns
/// the color code to be printed to the terminal
///
/// # Example
/// ```rust
/// use btui::print::bg;
/// use btui::effects::Color;
/// println!("{}test", bg(Color::Red));
/// ```
pub fn bg(col: Color) -> String {
match col {
Color::Black => String::from("\x1b[40m"),
Color::Red => String::from("\x1b[41m"),
Color::Green => String::from("\x1b[42m"),
Color::Yellow => String::from("\x1b[43m"),
Color::Blue => String::from("\x1b[44m"),
Color::Magenta => String::from("\x1b[45m"),
Color::Cyan => String::from("\x1b[46m"),
Color::White => String::from("\x1b[47m"),
}
}
/// change background color
///
/// # Arguments
/// *`col`: color to use
///
/// # Returns
/// the color code to be printed to the terminal
///
/// # Example
/// ```rust
/// use btui::ft::formatting::print::bg;
/// use btui::colors::Color;
/// println!("{}test{}", bg(Color::Red), bg(Color::Reset));
/// ```
pub fn bg(col: Color) -> String {
match col {
Color::Black => String::from("\x1b[40m"),
Color::Red => String::from("\x1b[41m"),
Color::Green => String::from("\x1b[42m"),
Color::Yellow => String::from("\x1b[43m"),
Color::Blue => String::from("\x1b[44m"),
Color::Magenta => String::from("\x1b[45m"),
Color::Cyan => String::from("\x1b[46m"),
Color::White => String::from("\x1b[47m"),
Color::Reset => String::from("\x1b[0m"),
}
/// apply a special effect
///
/// # Arguments
/// *`special`: effect to apply
///
/// # Returns
/// a string containing the escape sequence to use
///
/// # Example
/// ```rust
/// use btui::effects::Special;
/// use btui::print::sp;
/// println!("{}test{}", sp(Special::Underline), sp(Special::Reset));
/// ```
pub fn sp(special: Special) -> String {
match special {
Special::Bold => String::from("\x1b[1m"),
Special::Underline => String::from("\x1b[4m"),
Special::Stroke => String::from("\x1b[9m"),
Special::Italic => String::from("\x1b[3m"),
Special::Reset => String::from("\x1b[0m"),
}
}
}
......@@ -4,19 +4,18 @@
//! with btui you can create beautiful text user interfaces for the terminal.
//! To get started add `btui` as a dependency to your project:
//! ```toml
//! btui = "0.2.1"
//! btui = "0.3.2"
//! ```
pub mod ft;
pub enum Variant<P, Q> {
One(P),
Two(Q),
}
mod ft;
pub use ft::{effects, print};
#[cfg(test)]
mod tests {
use crate::effects::Color::Black;
use crate::print::fg;
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
fn correct_color() {
assert_eq!(String::from("\x1b[30m"), fg(Black));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment