lliw-0.2.0/.cargo_vcs_info.json0000644000000001120000000000100117740ustar { "git": { "sha1": "bc94f628fe1fc773e2d1760b8244b06889df01f2" } } lliw-0.2.0/.gitignore000064400000000000000000000000510072674642500126060ustar 00000000000000/target Cargo.lock example/target/ *.swp lliw-0.2.0/Cargo.toml0000644000000016450000000000100100060ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "lliw" version = "0.2.0" authors = ["curlpipe <11898833+curlpipe@users.noreply.github.com>"] description = "Text colours and styles for your terminal with no additional dependencies" readme = "README.md" keywords = ["terminal", "color", "colour", "ansi", "lightweight"] categories = ["command-line-interface"] license = "MIT" repository = "https://github.com/curlpipe/lliw" lliw-0.2.0/Cargo.toml.orig000064400000000000000000000006300072674642500135100ustar 00000000000000[package] name = "lliw" version = "0.2.0" authors = ["curlpipe <11898833+curlpipe@users.noreply.github.com>"] edition = "2018" license = "MIT" description = "Text colours and styles for your terminal with no additional dependencies" repository = "https://github.com/curlpipe/lliw" readme = "README.md" keywords = ["terminal", "color", "colour", "ansi", "lightweight"] categories = ["command-line-interface"] lliw-0.2.0/LICENSE000064400000000000000000000020510072674642500116250ustar 00000000000000MIT License Copyright (c) 2021 curlpipe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. lliw-0.2.0/README.md000064400000000000000000000070330072674642500121040ustar 00000000000000# Lliw *Roughly pronounced khlew*, it is "colour" in Welsh Lliw provides colours for your terminal, with no additional dependencies. # Aims - No dependencies - Works in `#![no_std]` environments - Provides colours and styles in a non-opinionated way - Provides multiple ways to use - Doesn't make your code look like trash - Doesn't assume how you'll use the crate # Installation If you have `cargo-edit` installed, it's as easy as: ```sh cargo add lliw ``` if you don't have `cargo-edit` you can add the following to your `Cargo.toml` file ```toml [dependencies] lliw = "0" ``` # Example usage ```rust use lliw::{Fg, Bg, Style, Reset}; fn main() { // Prints "Blue" in a blue colour println!("{}Blue{}", Fg::Blue, Fg::Reset); // Prints "Bold" in bold println!("{}Bold{}", Style::Bold, Style::NoBold); // Prints "Green" with a green background println!("{}Green{}", Bg::Green, Bg::Reset); // You can even use it in more complicated ways println!( "{}{}Attention!{}{} You have {}{}1{}{} new message", Style::Underline, Fg::Yellow, Style::NoUnderline, Fg::Reset, Bg::White, Fg::Black, Bg::Reset, Fg::Reset, ); // You can make them go over the top of each other too println!( "{}Hello{} Wor{}ld! My{} Name{} {}Is{} Lliw{}", Style::Italic, Fg::LightPurple, Bg::Black, Fg::Reset, Style::NoItalic, Style::Underline, Bg::Reset, Reset ); // Don't like these long formatting macros? You can use it like this too! print!("{}", Fg::LightRed); print!("Hello\nThere!"); print!("{}\n", Reset); // You can also use an RGB value if you want 24-bit colours println!( "{}R{}a{}i{}n{}b{}o{}w{}", Fg::Rgb(255, 0, 0), Fg::Rgb(255, 128, 0), Fg::Rgb(255, 255, 0), Fg::Rgb(0, 255, 0), Fg::Rgb(0, 255, 255), Fg::Rgb(128, 0, 255), Fg::Rgb(255, 0, 128), Fg::Reset, ); } ``` # Usage There are 3 enums provided with lliw, `Fg`, `Bg` and `Style`. There is also 1 struct, `Reset`. - `Fg` - Control the text colour + Consists of the types: Rgb, Black, Red, Green, Yellow, Blue, Purple, Cyan, White, LightBlack, LightRed, LightGreen, LightYellow, LightBlue, LightPurple, LightCyan, LightWhite and Reset. + The colours are reset using the `Reset` variant. + You can use the Rgb variant to provide true 24-bit colour values - `Bg` - Control the text background colour + Consists of the types: Rgb, Black, Red, Green, Yellow, Blue, Purple, Cyan, White, LightBlack, LightRed, LightGreen, LightYellow, LightBlue, LightPurple, LightCyan, LightWhite and Reset. + The colours are reset using the `Reset` variant. + You can use the Rgb variant to provide true 24-bit colour values - `Style` - Control the text styles + Consists of the types: Bold, NoBold, Underline, NoUnderline, Strike, NoStrike, Italic, NoItalic, Inverse, NoInverse, Faint and NoFaint. + Bold: Make the text bold, can be terminated with `NoBold` + Underline: Make the text have an underline, can be terminated with `NoUnderline` + Italic: Make the text go italic, can be terminated with `NoItalic` + Inverse: Inverse the text colours, can be terminated with `NoInverse` + Faint: Make the text fainter, can be terminated with `NoFaint` + Strike: Make the text have a strike through it, can be terminated with `NoStrike` - `Reset` - This is a full reset struct that resets foreground, background and style when used. Be sure to check out the docs over at https://docs.rs/lliw License: MIT lliw-0.2.0/src/lib.rs000064400000000000000000000163600072674642500125330ustar 00000000000000// Zero crate colour library #![no_std] #![warn(clippy::pedantic, clippy::all)] use core::fmt; // Foreground colours pub const FG_BLACK: &str = ""; pub const FG_RED: &str = ""; pub const FG_GREEN: &str = ""; pub const FG_YELLOW: &str = ""; pub const FG_BLUE: &str = ""; pub const FG_PURPLE: &str = ""; pub const FG_CYAN: &str = ""; pub const FG_WHITE: &str = ""; pub const FG_LIGHTBLACK: &str = ""; pub const FG_LIGHTRED: &str = ""; pub const FG_LIGHTGREEN: &str = ""; pub const FG_LIGHTYELLOW: &str = ""; pub const FG_LIGHTBLUE: &str = ""; pub const FG_LIGHTPURPLE: &str = ""; pub const FG_LIGHTCYAN: &str = ""; pub const FG_LIGHTWHITE: &str = ""; // Background colours pub const BG_BLACK: &str = ""; pub const BG_RED: &str = ""; pub const BG_GREEN: &str = ""; pub const BG_YELLOW: &str = ""; pub const BG_BLUE: &str = ""; pub const BG_PURPLE: &str = ""; pub const BG_CYAN: &str = ""; pub const BG_WHITE: &str = ""; pub const BG_LIGHTBLACK: &str = ""; pub const BG_LIGHTRED: &str = ""; pub const BG_LIGHTGREEN: &str = ""; pub const BG_LIGHTYELLOW: &str = ""; pub const BG_LIGHTBLUE: &str = ""; pub const BG_LIGHTPURPLE: &str = ""; pub const BG_LIGHTCYAN: &str = ""; pub const BG_LIGHTWHITE: &str = ""; // Resetting of colours pub const FG_RESET: &str = ""; pub const BG_RESET: &str = ""; // Text styles pub const BOLD: &str = ""; pub const BOLD_RESET: &str = ""; pub const UNDERLINE: &str = ""; pub const UNDERLINE_RESET: &str = ""; pub const STRIKE: &str = ""; pub const STRIKE_RESET: &str = ""; pub const ITALIC: &str = ""; pub const ITALIC_RESET: &str = ""; pub const INVERSE: &str = ""; pub const INVERSE_RESET: &str = ""; pub const FAINT: &str = ""; pub const FAINT_RESET: &str = ""; // Resetting of everything pub const RESET: &str = ""; /// Foreground colours for setting text colour #[derive(Debug, Clone, Copy)] pub enum Fg { Rgb(u8, u8, u8), Hex(&'static str), Black, Red, Green, Yellow, Blue, Purple, Cyan, White, LightBlack, LightRed, LightGreen, LightYellow, LightBlue, LightPurple, LightCyan, LightWhite, Reset, } // Allow use in format macros impl fmt::Display for Fg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Fg::Rgb(r, g, b) => write!(f, "[38;2;{};{};{}m", r, g, b), Fg::Hex(h) => { let rgb = hex_to_rgb(h); write!(f, "[38;2;{};{};{}m", rgb.0, rgb.1, rgb.2) } col => write!( f, "{}", match col { Fg::Black => FG_BLACK, Fg::Red => FG_RED, Fg::Green => FG_GREEN, Fg::Yellow => FG_YELLOW, Fg::Blue => FG_BLUE, Fg::Purple => FG_PURPLE, Fg::Cyan => FG_CYAN, Fg::White => FG_WHITE, Fg::LightBlack => FG_LIGHTBLACK, Fg::LightRed => FG_LIGHTRED, Fg::LightGreen => FG_LIGHTGREEN, Fg::LightYellow => FG_LIGHTYELLOW, Fg::LightBlue => FG_LIGHTBLUE, Fg::LightPurple => FG_LIGHTPURPLE, Fg::LightCyan => FG_LIGHTCYAN, Fg::LightWhite => FG_LIGHTWHITE, Fg::Reset => FG_RESET, Fg::Rgb(_, _, _) | Fg::Hex(_) => unreachable!(), } ), } } } /// Background colours for setting text background colour #[derive(Debug, Clone, Copy)] pub enum Bg { Rgb(u8, u8, u8), Hex(&'static str), Black, Red, Green, Yellow, Blue, Purple, Cyan, White, LightBlack, LightRed, LightGreen, LightYellow, LightBlue, LightPurple, LightCyan, LightWhite, Reset, } // Allow use in format macros impl fmt::Display for Bg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Bg::Rgb(r, g, b) => write!(f, "[48;2;{};{};{}m", r, g, b), Bg::Hex(h) => { let rgb = hex_to_rgb(h); write!(f, "[48;2;{};{};{}m", rgb.0, rgb.1, rgb.2) } col => write!( f, "{}", match col { Bg::Black => BG_BLACK, Bg::Red => BG_RED, Bg::Green => BG_GREEN, Bg::Yellow => BG_YELLOW, Bg::Blue => BG_BLUE, Bg::Purple => BG_PURPLE, Bg::Cyan => BG_CYAN, Bg::White => BG_WHITE, Bg::LightBlack => BG_LIGHTBLACK, Bg::LightRed => BG_LIGHTRED, Bg::LightGreen => BG_LIGHTGREEN, Bg::LightYellow => BG_LIGHTYELLOW, Bg::LightBlue => BG_LIGHTBLUE, Bg::LightPurple => BG_LIGHTPURPLE, Bg::LightCyan => BG_LIGHTCYAN, Bg::LightWhite => BG_LIGHTWHITE, Bg::Reset => BG_RESET, Bg::Rgb(_, _, _) | Bg::Hex(_) => unreachable!(), } ), } } } /// Style enum to style text #[derive(Debug, Clone, Copy)] pub enum Style { Bold, NoBold, Underline, NoUnderline, Strike, NoStrike, Italic, NoItalic, Inverse, NoInverse, Faint, NoFaint, } // Allow use in format macros impl fmt::Display for Style { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "{}", match self { Style::Bold => BOLD, Style::NoBold => BOLD_RESET, Style::Underline => UNDERLINE, Style::NoUnderline => UNDERLINE_RESET, Style::Strike => STRIKE, Style::NoStrike => STRIKE_RESET, Style::Italic => ITALIC, Style::NoItalic => ITALIC_RESET, Style::Inverse => INVERSE, Style::NoInverse => INVERSE_RESET, Style::Faint => FAINT, Style::NoFaint => FAINT_RESET, } ) } } /// A reset type that clears all styling at once #[derive(Debug, Clone, Copy)] pub struct Reset; // Allow use in format macros impl fmt::Display for Reset { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", RESET) } } // Function to conver hex code to rgb fn hex_to_rgb(hex: &str) -> (u8, u8, u8) { let mut hex = hex; // The '#' isn't necessary if hex.starts_with("#") { hex = &hex[1..]; } // If the hex-code is invalid it defaults to black let mut rgb: (u8, u8, u8) = (0, 0, 0); if hex.len() == 6 { rgb.0 = hex_to_dec(&hex[0..2]); rgb.1 = hex_to_dec(&hex[2..4]); rgb.2 = hex_to_dec(&hex[4..6]); } rgb } fn hex_to_dec(hex: &str) -> u8 { u8::from_str_radix(hex, 16).unwrap_or(0) }