Skip to content

This can become even simpler to use #16

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[workspace]
members = [".", "example"]

[package]
name = "term_tools"
version = "0.1.0"
Expand All @@ -7,7 +10,7 @@ authors = ["Siavash [email protected]"]
homepage = "https://crates.io/crates/term_tools"
repository = "https://github.com/Syaw0/term_tools"
license-file = "./LICENSE"
keywords = ["cli","terminal","rust","tool","coloize"]
categories = ["command-line-interface","command-line-utilities"]
keywords = ["cli", "terminal", "rust", "tool", "coloize"]
categories = ["command-line-interface", "command-line-utilities"]

[dependencies]
7 changes: 7 additions & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "example"
version = "0.1.0"
edition = "2021"

[dependencies]
term_tools = { path = ".." }
23 changes: 23 additions & 0 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use term_tools::prelude::*;

fn main() {
println!(
"{}{}{}",
"In the name of ",
"GOD".styled().bold().cyan(),
"."
);

let mut styled = "Hello World!".styled();
styled
.color(colors::RED)
.format(formats::BOLD)
.underline()
.fg()
.white()
.bg();

println!("{}", styled);

println!("{}", "Walking dead!".styled().italic().red())
}
17 changes: 10 additions & 7 deletions src/ansi_code.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// A module for creating ansi escape code.
///
/// This module provides an struct `ANSIEscapeCode`
/// that represents ansi escape code, with `parameter` field.
/// It also implements the `ANSIEscapeCode`, with `new` and `code` method
/// which allows for generating anis escape code and getting a code.
//! A module for creating ansi escape code.
//!
//! This module provides an struct `ANSIEscapeCode`
//! that represents ansi escape code, with `parameter` field.
//! It also implements the `ANSIEscapeCode`, with `new` and `code` method
//! which allows for generating anis escape code and getting a code.

// =======================================================================

Expand All @@ -13,10 +13,13 @@
pub struct ANSIEscapeCode {
parameter: String,
}

impl ANSIEscapeCode {
/// Returns a ANSIEscapeCode instance with parameter .
pub fn new(parameter: &str) -> Self {
ANSIEscapeCode { parameter: parameter.to_string() }
ANSIEscapeCode {
parameter: parameter.to_string(),
}
}

/// Returns a String that represent the ansi code.
Expand Down
98 changes: 98 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//!
//!

// =======================================================================

use crate::{styles::Styles, StyledText};

// =======================================================================

/// Converts everything that can convert into [`String`], to an [`StyledText`].
pub trait IntoStyled {
/// Creates a styled text string with the given text and styles.
///
/// This function returns a `StyledText` object that can be customized with various styles,
/// colors, and effects. The resulting styled text can be printed or used in other contexts.
///
/// # Examples
///
/// ```
/// use term_tools::styled;
///
/// let txt = "Happy Day!"
/// .styled()
/// .rgb(204, 182, 122)
/// .italic()
/// .rapid_blink()
/// .bg()
/// .black()
/// .fg()
/// .paint();
///
/// println!("{txt}");
/// ```
///
/// # Styles
///
/// The following styles can be applied to the text:
///
/// * `rgb(r, g, b)`: Sets the text color to the given RGB values.
/// * `italic()`: Sets the text to italic.
/// * `rapid_blink()`: Sets the text to rapidly blink.
/// * `bg()`: Sets the background color.
/// * `fg()`: Sets the foreground color.
/// * basic colors like : `red`,`black` and etc.
///
/// # Colors
///
/// The following colors can be used with the `rgb` method:
///
/// * `rgb(r, g, b)`: Sets the color to the given RGB values.
///
/// # Effects
///
/// The following effects can be applied to the text:
///
/// * `rapid_blink()`: Sets the text to rapidly blink.
///
/// # Returns
///
/// A `StyledText` object that can be printed or used in other contexts.
fn styled(self) -> StyledText;
}

impl<T: Into<String>> IntoStyled for T {
fn styled(self) -> StyledText {
StyledText::new(self.into())
}
}

pub trait IntoStyle {
fn into_style(self) -> Styles;
}

impl<T: Into<Styles>> IntoStyle for T {
fn into_style(self) -> Styles {
self.into()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_into_styles() {
let txt = "Hi Father"
.styled()
.rgb(204, 182, 122)
.italic()
.rapid_blink()
.bg()
.black()
.fg()
.paint();

println!("{txt}");
}
}
Loading