Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #43 from epage/choice
Browse files Browse the repository at this point in the history
refactor: Switch to colorchoice
  • Loading branch information
epage committed Apr 13, 2023
2 parents 744c358 + 0104fbe commit f0e09b7
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
msrv = "1.60.0" # MSRV
msrv = "1.64.0" # MSRV
warn-on-all-wildcard-imports = true
allow-expect-in-tests = true
allow-unwrap-in-tests = true
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ jobs:
- name: No-default features
run: cargo test --workspace --no-default-features
msrv:
name: "Check MSRV: 1.60.0"
name: "Check MSRV: 1.64.0"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.60.0 # MSRV
toolchain: 1.64.0 # MSRV
- uses: Swatinem/rust-cache@v2
- name: Default features
run: cargo check --workspace --all-targets
Expand Down Expand Up @@ -107,7 +107,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.60.0 # MSRV
toolchain: 1.64.0 # MSRV
components: clippy
- uses: Swatinem/rust-cache@v2
- name: Install SARIF tools
Expand Down
9 changes: 9 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/clap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repository = "https://github.com/rust-cli/concolor"
categories = ["command-line-interface"]
keywords = ["cli", "color", "no-std", "terminal", "ansi"]
edition = "2021"
rust-version = "1.60.0" # MSRV
rust-version = "1.64.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
Expand Down
2 changes: 1 addition & 1 deletion crates/concolor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repository = "https://github.com/rust-cli/concolor"
categories = ["command-line-interface"]
keywords = ["cli", "color", "no-std", "terminal", "ansi"]
edition = "2021"
rust-version = "1.60.0" # MSRV
rust-version = "1.64.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
Expand Down
2 changes: 1 addition & 1 deletion crates/example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repository = "https://github.com/rust-cli/concolor"
categories = ["command-line-interface"]
keywords = ["cli", "color", "no-std", "terminal", "ansi"]
edition = "2021"
rust-version = "1.60.0" # MSRV
rust-version = "1.64.0" # MSRV
publish = false

[dependencies]
Expand Down
5 changes: 4 additions & 1 deletion crates/override/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ repository = "https://github.com/rust-cli/concolor"
categories = ["command-line-interface"]
keywords = ["cli", "color", "no-std", "terminal", "ansi"]
edition = "2021"
rust-version = "1.60.0" # MSRV
rust-version = "1.64.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
"LICENSE*",
"README.md",
"examples/**/*"
]

[dependencies]
colorchoice = "1.0.0"
95 changes: 3 additions & 92 deletions crates/override/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,14 @@

#![cfg_attr(not(test), no_std)]

use core::sync::atomic::{AtomicUsize, Ordering};

/// Selection for overriding color output with [`set`][crate::set]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorChoice {
Auto,
AlwaysAnsi,
Always,
Never,
}

impl Default for ColorChoice {
fn default() -> Self {
Self::Auto
}
}
pub use colorchoice::ColorChoice;

/// Get the current [`ColorChoice`] state
pub fn get() -> ColorChoice {
USER.get()
ColorChoice::global()
}

/// Override the detected [`ColorChoice`]
pub fn set(choice: ColorChoice) {
USER.set(choice)
}

static USER: AtomicChoice = AtomicChoice::new();

#[derive(Debug)]
pub(crate) struct AtomicChoice(AtomicUsize);

impl AtomicChoice {
pub(crate) const fn new() -> Self {
Self(AtomicUsize::new(Self::from_choice(
crate::ColorChoice::Auto,
)))
}

pub(crate) fn get(&self) -> crate::ColorChoice {
let choice = self.0.load(Ordering::SeqCst);
Self::to_choice(choice).unwrap()
}

pub(crate) fn set(&self, choice: crate::ColorChoice) {
let choice = Self::from_choice(choice);
self.0.store(choice, Ordering::SeqCst);
}

const fn from_choice(choice: crate::ColorChoice) -> usize {
match choice {
crate::ColorChoice::Auto => 0,
crate::ColorChoice::AlwaysAnsi => 1,
crate::ColorChoice::Always => 2,
crate::ColorChoice::Never => 3,
}
}

const fn to_choice(choice: usize) -> Option<crate::ColorChoice> {
match choice {
0 => Some(crate::ColorChoice::Auto),
1 => Some(crate::ColorChoice::AlwaysAnsi),
2 => Some(crate::ColorChoice::Always),
3 => Some(crate::ColorChoice::Never),
_ => None,
}
}
}

impl Default for AtomicChoice {
fn default() -> Self {
Self::new()
}
}

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

#[test]
fn choice_serialization() {
let expected = vec![
ColorChoice::Auto,
ColorChoice::AlwaysAnsi,
ColorChoice::Always,
ColorChoice::Never,
];
let values: Vec<_> = expected
.iter()
.cloned()
.map(AtomicChoice::from_choice)
.collect();
let actual: Vec<_> = values
.iter()
.cloned()
.filter_map(AtomicChoice::to_choice)
.collect();
assert_eq!(expected, actual);
}
choice.write_global()
}
2 changes: 1 addition & 1 deletion crates/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repository = "https://github.com/rust-cli/concolor"
categories = ["command-line-interface"]
keywords = ["cli", "color", "no-std", "terminal", "ansi"]
edition = "2021"
rust-version = "1.60.0" # MSRV
rust-version = "1.64.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
Expand Down

0 comments on commit f0e09b7

Please sign in to comment.