Skip to content
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

Enable lints #191

Open
wants to merge 4 commits into
base: master
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
11 changes: 7 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt
- name: Run formatting checks
run: cargo fmt --check

cargo-clippy:
name: "${{ matrix.os }}:cargo-clippy@stable"
runs-on: "${{ matrix.os }}-latest"
Expand All @@ -26,14 +27,15 @@ jobs:
os: ["ubuntu", "windows", "macos"]
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: clippy
- name: Run clippy checks
run: cargo clippy --all-features -- -D warnings

cargo-test:
name: "${{ matrix.os }}:cargo-test@${{ matrix.version }}"
runs-on: "${{ matrix.os }}-latest"
Expand All @@ -43,19 +45,20 @@ jobs:
version: ["stable", "beta", "1.80"]
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust ${{ matrix.version }}
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.version }}
- name: Run tests
run: cargo test

check-semver:
name: Check semver compatibility
if: github.base_ref == 'master'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2
28 changes: 24 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,32 @@ no-color = []

[target.'cfg(windows)'.dependencies.windows-sys]
version = ">=0.48,<=0.59"
features = [
"Win32_Foundation",
"Win32_System_Console",
]
features = ["Win32_Foundation", "Win32_System_Console"]

[dev-dependencies]
ansi_term = "0.12"
insta = "1"
rspec = "1"

[lints.rust]
unsafe_code = "warn"
deprecated = "warn"

[lints.clippy]
complexity = "warn"
correctness = "warn"
nursery = "warn"
pedantic = "warn"
perf = "warn"
style = "warn"
suspicious = "warn"

# Could be enabled in the future
too_many_lines = { level = "allow", priority = 10 }
unwrap_used = { level = "allow", priority = 10 }
expect_used = { level = "allow", priority = 10 }
wildcard_imports = { level = "allow", priority = 10 }

# not necessary
module_name_repetitions = { level = "allow", priority = 10 }
missing_const_for_fn = { level = "allow", priority = 10 }
2 changes: 1 addition & 1 deletion examples/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_must_use)]
extern crate colored;
use colored::*;
use colored::Colorize;

#[cfg(not(windows))]
fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_colors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use colored::*;
use colored::{Colorize, CustomColor};
fn main() {
let my_color = CustomColor::new(0, 120, 120);
println!("{}", "Greetings from Ukraine".custom_color(my_color));
Expand Down
2 changes: 1 addition & 1 deletion examples/dynamic_colors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate colored;
use colored::*;
use colored::{Color, Colorize};

fn main() {
// the easy way
Expand Down
6 changes: 3 additions & 3 deletions examples/most_simple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate colored;

use colored::*;
use colored::Colorize;

fn main() {
// TADAA !
Expand All @@ -17,8 +17,8 @@ fn main() {
s.push_str(&"why not ".red().to_string());
s.push_str(&"push things ".blue().to_string());
s.push_str(&"a little further ?".green().to_string());
println!("{}", s);
println!("{s}");

let s = format!("{} {} {}", "this".red(), "is".blue(), "easier".green());
println!("{}", s);
println!("{s}");
}
10 changes: 5 additions & 5 deletions examples/nested_colors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate colored;

use colored::*;
use colored::Colorize;

/*
* This example use colored strings in a nested way (at line 14). It shows that colored is able to
Expand All @@ -9,8 +9,8 @@ use colored::*;

fn main() {
let world = "world".bold();
let hello_world = format!("Hello, {}!", world);
println!("{}", hello_world);
let hello_world = format!("Hello, {}!lalalala", world).red();
println!("{}", hello_world);
let hello_world = format!("Hello, {world}!");
println!("{hello_world}");
let hello_world = format!("Hello, {world}!lalalala").red();
println!("{hello_world}");
}
130 changes: 63 additions & 67 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::{borrow::Cow, env, str::FromStr};

use std::{borrow::Cow, cmp, env, str::FromStr};
use Color::{
Black, Blue, BrightBlack, BrightBlue, BrightCyan, BrightGreen, BrightMagenta, BrightRed,
BrightWhite, BrightYellow, Cyan, Green, Magenta, Red, TrueColor, White, Yellow,
};
/// The 8 standard colors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(missing_docs)]
Expand All @@ -25,70 +28,65 @@ pub enum Color {

fn truecolor_support() -> bool {
let truecolor = env::var("COLORTERM");
if let Ok(truecolor) = truecolor {
truecolor == "truecolor" || truecolor == "24bit"
} else {
false
}
truecolor.is_ok_and(|truecolor| truecolor == "truecolor" || truecolor == "24bit")
}

#[allow(missing_docs)]
impl Color {
#[must_use]
pub fn to_fg_str(&self) -> Cow<'static, str> {
match *self {
Color::Black => "30".into(),
Color::Red => "31".into(),
Color::Green => "32".into(),
Color::Yellow => "33".into(),
Color::Blue => "34".into(),
Color::Magenta => "35".into(),
Color::Cyan => "36".into(),
Color::White => "37".into(),
Color::BrightBlack => "90".into(),
Color::BrightRed => "91".into(),
Color::BrightGreen => "92".into(),
Color::BrightYellow => "93".into(),
Color::BrightBlue => "94".into(),
Color::BrightMagenta => "95".into(),
Color::BrightCyan => "96".into(),
Color::BrightWhite => "97".into(),
Color::TrueColor { .. } if !truecolor_support() => {
Self::Black => "30".into(),
Self::Red => "31".into(),
Self::Green => "32".into(),
Self::Yellow => "33".into(),
Self::Blue => "34".into(),
Self::Magenta => "35".into(),
Self::Cyan => "36".into(),
Self::White => "37".into(),
Self::BrightBlack => "90".into(),
Self::BrightRed => "91".into(),
Self::BrightGreen => "92".into(),
Self::BrightYellow => "93".into(),
Self::BrightBlue => "94".into(),
Self::BrightMagenta => "95".into(),
Self::BrightCyan => "96".into(),
Self::BrightWhite => "97".into(),
Self::TrueColor { .. } if !truecolor_support() => {
self.closest_color_euclidean().to_fg_str()
}
Color::TrueColor { r, g, b } => format!("38;2;{};{};{}", r, g, b).into(),
Self::TrueColor { r, g, b } => format!("38;2;{r};{g};{b}").into(),
}
}

#[must_use]
pub fn to_bg_str(&self) -> Cow<'static, str> {
match *self {
Color::Black => "40".into(),
Color::Red => "41".into(),
Color::Green => "42".into(),
Color::Yellow => "43".into(),
Color::Blue => "44".into(),
Color::Magenta => "45".into(),
Color::Cyan => "46".into(),
Color::White => "47".into(),
Color::BrightBlack => "100".into(),
Color::BrightRed => "101".into(),
Color::BrightGreen => "102".into(),
Color::BrightYellow => "103".into(),
Color::BrightBlue => "104".into(),
Color::BrightMagenta => "105".into(),
Color::BrightCyan => "106".into(),
Color::BrightWhite => "107".into(),
Color::TrueColor { .. } if !truecolor_support() => {
Self::Black => "40".into(),
Self::Red => "41".into(),
Self::Green => "42".into(),
Self::Yellow => "43".into(),
Self::Blue => "44".into(),
Self::Magenta => "45".into(),
Self::Cyan => "46".into(),
Self::White => "47".into(),
Self::BrightBlack => "100".into(),
Self::BrightRed => "101".into(),
Self::BrightGreen => "102".into(),
Self::BrightYellow => "103".into(),
Self::BrightBlue => "104".into(),
Self::BrightMagenta => "105".into(),
Self::BrightCyan => "106".into(),
Self::BrightWhite => "107".into(),
Self::TrueColor { .. } if !truecolor_support() => {
self.closest_color_euclidean().to_bg_str()
}
Color::TrueColor { r, g, b } => format!("48;2;{};{};{}", r, g, b).into(),
Self::TrueColor { r, g, b } => format!("48;2;{r};{g};{b}").into(),
}
}

/// Gets the closest plain color to the TrueColor
/// Gets the closest plain color to the `TrueColor`
fn closest_color_euclidean(self) -> Self {
use std::cmp;
use Color::*;

match self {
TrueColor {
r: r1,
Expand Down Expand Up @@ -136,7 +134,6 @@ impl Color {
}

fn into_truecolor(self) -> Self {
use Color::*;
match self {
Black => TrueColor { r: 0, g: 0, b: 0 },
Red => TrueColor { r: 205, g: 0, b: 0 },
Expand Down Expand Up @@ -201,13 +198,13 @@ impl Color {

impl From<&str> for Color {
fn from(src: &str) -> Self {
src.parse().unwrap_or(Color::White)
src.parse().unwrap_or(Self::White)
}
}

impl From<String> for Color {
fn from(src: String) -> Self {
src.parse().unwrap_or(Color::White)
src.parse().unwrap_or(Self::White)
}
}

Expand All @@ -218,23 +215,22 @@ impl FromStr for Color {
let src = src.to_lowercase();

match src.as_ref() {
"black" => Ok(Color::Black),
"red" => Ok(Color::Red),
"green" => Ok(Color::Green),
"yellow" => Ok(Color::Yellow),
"blue" => Ok(Color::Blue),
"magenta" => Ok(Color::Magenta),
"purple" => Ok(Color::Magenta),
"cyan" => Ok(Color::Cyan),
"white" => Ok(Color::White),
"bright black" => Ok(Color::BrightBlack),
"bright red" => Ok(Color::BrightRed),
"bright green" => Ok(Color::BrightGreen),
"bright yellow" => Ok(Color::BrightYellow),
"bright blue" => Ok(Color::BrightBlue),
"bright magenta" => Ok(Color::BrightMagenta),
"bright cyan" => Ok(Color::BrightCyan),
"bright white" => Ok(Color::BrightWhite),
"black" => Ok(Self::Black),
"red" => Ok(Self::Red),
"green" => Ok(Self::Green),
"yellow" => Ok(Self::Yellow),
"blue" => Ok(Self::Blue),
"magenta" | "purple" => Ok(Self::Magenta),
"cyan" => Ok(Self::Cyan),
"white" => Ok(Self::White),
"bright black" => Ok(Self::BrightBlack),
"bright red" => Ok(Self::BrightRed),
"bright green" => Ok(Self::BrightGreen),
"bright yellow" => Ok(Self::BrightYellow),
"bright blue" => Ok(Self::BrightBlue),
"bright magenta" => Ok(Self::BrightMagenta),
"bright cyan" => Ok(Self::BrightCyan),
"bright white" => Ok(Self::BrightWhite),
_ => Err(()),
}
}
Expand Down
Loading