build(ariadne): vendor clap
This commit was merged in pull request #30.
This commit is contained in:
1 parent
e6af135702
commit
8855b6d379
681 files changed
+400416
No files matched your search
@@ -0,0 +1 @@
|
||||
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"00598318503c452840bff2f331dc4aeba638d07ca04c19df391480077651d8ce","Cargo.lock":"99932d5df3eb13cfd9b5e1a2dd5e8745ee50e54639b06fa3a40ef67a3478b939","Cargo.toml":"1464191f3d9cdcddee83a73a16c7cee7e48dba818248b3fd4273757fbed4407e","Cargo.toml.orig":"25ef1f8a15173a072714c3cd93700a3d3b2d377f83578955914fb0bfb5028094","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"6efb0476a1cc085077ed49357026d8c173bf33017278ef440f222fb9cbcb66e6","README.md":"015245c48ad8fb28bc4b2ed85a992a6e1182aa7fd633ecaac8f69b229d2aacab","examples/dump-style.rs":"a6c89b70be79594485312ec4670886ea9f4bebbf2f39fedfbd16d92745824f82","src/color.rs":"fc82bbfc225372584c1985ecb11e744ec0e59d8c208ad404c98846759eb01f4b","src/effect.rs":"ed72ba456c4804ff6669f41ee0754bf8e2d974c2217e3eeac0d4d23202b81289","src/lib.rs":"0b3ac694950781211aee56f48129317880c1c3056b0b7639f0ea6cddb0201c6c","src/macros.rs":"0c90b45626fe8331d5b3326abb831f4ba6e04bcc975b1b9c01e465715050caa2","src/reset.rs":"456baf708f7d595369d2d852427d1891208a9fad5e21291be3217c4e69114faf","src/style.rs":"6f5eee7892f10df47bc07dd8f5715ee5d0c438cba53b63b72dce0843423b42bd"},"package":"940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "0fe6f0ff6d52e9f91d4071199bd0b24bd46f3d35"
|
||||
},
|
||||
"path_in_vcs": "crates/anstyle"
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
###############################################################################
|
||||
# @generated
|
||||
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
|
||||
# regenerate this file, run the following:
|
||||
#
|
||||
# bazel run @@//third_party:vendor_crates
|
||||
###############################################################################
|
||||
|
||||
load("@rules_rust//cargo:defs.bzl", "cargo_toml_env_vars")
|
||||
load("@rules_rust//rust:defs.bzl", "rust_library")
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
cargo_toml_env_vars(
|
||||
name = "cargo_toml_env_vars",
|
||||
src = "Cargo.toml",
|
||||
)
|
||||
|
||||
rust_library(
|
||||
name = "anstyle",
|
||||
srcs = glob(
|
||||
include = ["**/*.rs"],
|
||||
allow_empty = False,
|
||||
),
|
||||
compile_data = glob(
|
||||
include = ["**"],
|
||||
allow_empty = True,
|
||||
exclude = [
|
||||
"**/* *",
|
||||
".tmp_git_root/**/*",
|
||||
"BUILD",
|
||||
"BUILD.bazel",
|
||||
"WORKSPACE",
|
||||
"WORKSPACE.bazel",
|
||||
],
|
||||
),
|
||||
crate_features = [
|
||||
"default",
|
||||
"std",
|
||||
],
|
||||
crate_root = "src/lib.rs",
|
||||
edition = "2021",
|
||||
rustc_env_files = [
|
||||
":cargo_toml_env_vars",
|
||||
],
|
||||
rustc_flags = [
|
||||
"--cap-lints=allow",
|
||||
],
|
||||
tags = [
|
||||
"cargo-bazel",
|
||||
"crate-name=anstyle",
|
||||
"manual",
|
||||
"noclippy",
|
||||
"norustfmt",
|
||||
],
|
||||
target_compatible_with = select({
|
||||
"@rules_rust//rust/platform:aarch64-apple-darwin": [],
|
||||
"@rules_rust//rust/platform:aarch64-apple-ios": [],
|
||||
"@rules_rust//rust/platform:aarch64-apple-ios-macabi": [],
|
||||
"@rules_rust//rust/platform:aarch64-apple-ios-sim": [],
|
||||
"@rules_rust//rust/platform:aarch64-linux-android": [],
|
||||
"@rules_rust//rust/platform:aarch64-pc-windows-msvc": [],
|
||||
"@rules_rust//rust/platform:aarch64-unknown-fuchsia": [],
|
||||
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:aarch64-unknown-nixos-gnu": [],
|
||||
"@rules_rust//rust/platform:aarch64-unknown-none": [],
|
||||
"@rules_rust//rust/platform:aarch64-unknown-nto-qnx710": [],
|
||||
"@rules_rust//rust/platform:aarch64-unknown-uefi": [],
|
||||
"@rules_rust//rust/platform:arm-unknown-linux-gnueabi": [],
|
||||
"@rules_rust//rust/platform:arm-unknown-linux-musleabi": [],
|
||||
"@rules_rust//rust/platform:armv7-linux-androideabi": [],
|
||||
"@rules_rust//rust/platform:armv7-unknown-linux-gnueabi": [],
|
||||
"@rules_rust//rust/platform:i686-apple-darwin": [],
|
||||
"@rules_rust//rust/platform:i686-linux-android": [],
|
||||
"@rules_rust//rust/platform:i686-pc-windows-msvc": [],
|
||||
"@rules_rust//rust/platform:i686-unknown-freebsd": [],
|
||||
"@rules_rust//rust/platform:i686-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:loongarch64-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:mips-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:powerpc-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:riscv32imac-unknown-none-elf": [],
|
||||
"@rules_rust//rust/platform:riscv32imc-unknown-none-elf": [],
|
||||
"@rules_rust//rust/platform:riscv64gc-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:riscv64gc-unknown-none-elf": [],
|
||||
"@rules_rust//rust/platform:s390x-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:sparc64-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:sparc64-unknown-netbsd": [],
|
||||
"@rules_rust//rust/platform:sparc64-unknown-openbsd": [],
|
||||
"@rules_rust//rust/platform:thumbv6m-none-eabi": [],
|
||||
"@rules_rust//rust/platform:thumbv7em-none-eabi": [],
|
||||
"@rules_rust//rust/platform:thumbv7em-none-eabihf": [],
|
||||
"@rules_rust//rust/platform:thumbv7m-none-eabi": [],
|
||||
"@rules_rust//rust/platform:thumbv8m.main-none-eabi": [],
|
||||
"@rules_rust//rust/platform:thumbv8m.main-none-eabihf": [],
|
||||
"@rules_rust//rust/platform:wasm32-unknown-emscripten": [],
|
||||
"@rules_rust//rust/platform:wasm32-unknown-unknown": [],
|
||||
"@rules_rust//rust/platform:wasm32-wasip1": [],
|
||||
"@rules_rust//rust/platform:wasm32-wasip1-threads": [],
|
||||
"@rules_rust//rust/platform:wasm32-wasip2": [],
|
||||
"@rules_rust//rust/platform:x86_64-apple-darwin": [],
|
||||
"@rules_rust//rust/platform:x86_64-apple-ios": [],
|
||||
"@rules_rust//rust/platform:x86_64-apple-ios-macabi": [],
|
||||
"@rules_rust//rust/platform:x86_64-linux-android": [],
|
||||
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": [],
|
||||
"@rules_rust//rust/platform:x86_64-unknown-freebsd": [],
|
||||
"@rules_rust//rust/platform:x86_64-unknown-fuchsia": [],
|
||||
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [],
|
||||
"@rules_rust//rust/platform:x86_64-unknown-nixos-gnu": [],
|
||||
"@rules_rust//rust/platform:x86_64-unknown-none": [],
|
||||
"@rules_rust//rust/platform:x86_64-unknown-uefi": [],
|
||||
"//conditions:default": ["@platforms//:incompatible"],
|
||||
}),
|
||||
version = "1.0.14",
|
||||
)
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
|
||||
dependencies = [
|
||||
"anstyle 1.0.13",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
dependencies = [
|
||||
"lexopt",
|
||||
"snapbox",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e231f6134f61b71076a3eab506c379d4f36122f2af15a9ff04415ea4c3339e2"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3e0633414522a32ffaac8ac6cc8f748e090c5717661fddeea04219e2344f5f2a"
|
||||
dependencies = [
|
||||
"anstyle 1.0.13",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.48.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2736dd548daf35f50d261bbad35a83890bb9b461797f15de528485fbf206ab15"
|
||||
dependencies = [
|
||||
"is-terminal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lexopt"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fa0e2a1fcbe2f6be6c42e342259976206b383122fc152e872795338b5a3f3a7"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.180"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
|
||||
|
||||
[[package]]
|
||||
name = "normalize-line-endings"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.56.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8aab899efe6ce4fe821f6dda30c02d7041c79f147598ab4b60197f181e12d001"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "similar"
|
||||
version = "2.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"
|
||||
|
||||
[[package]]
|
||||
name = "snapbox"
|
||||
version = "0.6.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96fa1ce81be900d083b30ec2d481e6658c2acfaa2cfc7be45ccc2cc1b820edb3"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle 1.0.13",
|
||||
"normalize-line-endings",
|
||||
"similar",
|
||||
"snapbox-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "snapbox-macros"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b750c344002d7cc69afb9da00ebd9b5c0f8ac2eb7d115d9d45d5b5f47718d74"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.53.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
# 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 are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
rust-version = "1.66.0"
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
build = false
|
||||
include = [
|
||||
"build.rs",
|
||||
"src/**/*",
|
||||
"Cargo.toml",
|
||||
"Cargo.lock",
|
||||
"LICENSE*",
|
||||
"README.md",
|
||||
"examples/**/*",
|
||||
]
|
||||
autolib = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
autobenches = false
|
||||
description = "ANSI text styling"
|
||||
readme = "README.md"
|
||||
keywords = [
|
||||
"ansi",
|
||||
"terminal",
|
||||
"color",
|
||||
"no_std",
|
||||
]
|
||||
categories = ["command-line-interface"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/rust-cli/anstyle.git"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
|
||||
[package.metadata.release]
|
||||
tag-prefix = ""
|
||||
|
||||
[[package.metadata.release.pre-release-replacements]]
|
||||
file = "CHANGELOG.md"
|
||||
search = "Unreleased"
|
||||
replace = "{{version}}"
|
||||
min = 1
|
||||
|
||||
[[package.metadata.release.pre-release-replacements]]
|
||||
file = "CHANGELOG.md"
|
||||
search = '\.\.\.HEAD'
|
||||
replace = "...{{tag_name}}"
|
||||
exactly = 1
|
||||
|
||||
[[package.metadata.release.pre-release-replacements]]
|
||||
file = "CHANGELOG.md"
|
||||
search = "ReleaseDate"
|
||||
replace = "{{date}}"
|
||||
min = 1
|
||||
|
||||
[[package.metadata.release.pre-release-replacements]]
|
||||
file = "CHANGELOG.md"
|
||||
search = "<!-- next-header -->"
|
||||
replace = """
|
||||
<!-- next-header -->
|
||||
## [Unreleased] - ReleaseDate
|
||||
"""
|
||||
exactly = 1
|
||||
|
||||
[[package.metadata.release.pre-release-replacements]]
|
||||
file = "CHANGELOG.md"
|
||||
search = "<!-- next-url -->"
|
||||
replace = """
|
||||
<!-- next-url -->
|
||||
[Unreleased]: https://github.com/rust-cli/anstyle/compare/{{tag_name}}...HEAD"""
|
||||
exactly = 1
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
|
||||
[lib]
|
||||
name = "anstyle"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[example]]
|
||||
name = "dump-style"
|
||||
path = "examples/dump-style.rs"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies.lexopt]
|
||||
version = "0.3.1"
|
||||
|
||||
[dev-dependencies.snapbox]
|
||||
version = "0.6.23"
|
||||
|
||||
[lints.clippy]
|
||||
bool_assert_comparison = "allow"
|
||||
branches_sharing_code = "allow"
|
||||
checked_conversions = "warn"
|
||||
collapsible_else_if = "allow"
|
||||
create_dir = "warn"
|
||||
dbg_macro = "warn"
|
||||
debug_assert_with_mut_call = "warn"
|
||||
doc_markdown = "warn"
|
||||
empty_enums = "warn"
|
||||
enum_glob_use = "warn"
|
||||
expl_impl_clone_on_copy = "warn"
|
||||
explicit_deref_methods = "warn"
|
||||
explicit_into_iter_loop = "warn"
|
||||
fallible_impl_from = "warn"
|
||||
filter_map_next = "warn"
|
||||
flat_map_option = "warn"
|
||||
float_cmp_const = "warn"
|
||||
fn_params_excessive_bools = "warn"
|
||||
from_iter_instead_of_collect = "warn"
|
||||
if_same_then_else = "allow"
|
||||
implicit_clone = "warn"
|
||||
imprecise_flops = "warn"
|
||||
inconsistent_struct_constructor = "warn"
|
||||
inefficient_to_string = "warn"
|
||||
infinite_loop = "warn"
|
||||
invalid_upcast_comparisons = "warn"
|
||||
large_digit_groups = "warn"
|
||||
large_stack_arrays = "warn"
|
||||
large_types_passed_by_value = "warn"
|
||||
let_and_return = "allow"
|
||||
linkedlist = "warn"
|
||||
lossy_float_literal = "warn"
|
||||
macro_use_imports = "warn"
|
||||
mem_forget = "warn"
|
||||
mutex_integer = "warn"
|
||||
needless_continue = "allow"
|
||||
needless_for_each = "warn"
|
||||
negative_feature_names = "warn"
|
||||
path_buf_push_overwrite = "warn"
|
||||
ptr_as_ptr = "warn"
|
||||
rc_mutex = "warn"
|
||||
redundant_feature_names = "warn"
|
||||
ref_option_ref = "warn"
|
||||
rest_pat_in_fully_bound_structs = "warn"
|
||||
result_large_err = "allow"
|
||||
same_functions_in_if_condition = "warn"
|
||||
self_named_module_files = "warn"
|
||||
semicolon_if_nothing_returned = "warn"
|
||||
str_to_string = "warn"
|
||||
string_add = "warn"
|
||||
string_add_assign = "warn"
|
||||
string_lit_as_bytes = "warn"
|
||||
todo = "warn"
|
||||
trait_duplication_in_bounds = "warn"
|
||||
uninlined_format_args = "warn"
|
||||
verbose_file_reads = "warn"
|
||||
wildcard_imports = "warn"
|
||||
zero_sized_map_values = "warn"
|
||||
|
||||
[lints.rust]
|
||||
unnameable_types = "warn"
|
||||
unreachable_pub = "warn"
|
||||
unsafe_op_in_unsafe_fn = "warn"
|
||||
unused_lifetimes = "warn"
|
||||
unused_macro_rules = "warn"
|
||||
unused_qualifications = "warn"
|
||||
|
||||
[lints.rust.rust_2018_idioms]
|
||||
level = "warn"
|
||||
priority = -1
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
[package]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
description = "ANSI text styling"
|
||||
categories = ["command-line-interface"]
|
||||
keywords = ["ansi", "terminal", "color", "no_std"]
|
||||
repository.workspace = true
|
||||
license.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
include.workspace = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
|
||||
[package.metadata.release]
|
||||
tag-prefix = ""
|
||||
pre-release-replacements = [
|
||||
{file="CHANGELOG.md", search="Unreleased", replace="{{version}}", min=1},
|
||||
{file="CHANGELOG.md", search="\\.\\.\\.HEAD", replace="...{{tag_name}}", exactly=1},
|
||||
{file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", min=1},
|
||||
{file="CHANGELOG.md", search="<!-- next-header -->", replace="<!-- next-header -->\n## [Unreleased] - ReleaseDate\n", exactly=1},
|
||||
{file="CHANGELOG.md", search="<!-- next-url -->", replace="<!-- next-url -->\n[Unreleased]: https://github.com/rust-cli/anstyle/compare/{{tag_name}}...HEAD", exactly=1},
|
||||
]
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
lexopt = "0.3.1"
|
||||
snapbox = "0.6.23"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) Individual contributors
|
||||
|
||||
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.
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
# anstyle
|
||||
|
||||
> ANSI text styling
|
||||
|
||||
*A portmanteau of "ansi style"*
|
||||
|
||||
[][Documentation]
|
||||

|
||||
[](https://crates.io/crates/anstyle)
|
||||
|
||||
`anstyle` provides core types describing [ANSI styling escape
|
||||
codes](https://en.wikipedia.org/wiki/ANSI_escape_code) for interoperability
|
||||
between crates. For example, this would allow a crate to provide an API for
|
||||
customizing the colors used without putting the underlying text styling crate
|
||||
in the API.
|
||||
|
||||
For integration with your text styling crate, see:
|
||||
- [anstyle-termcolor](crates/termcolor)
|
||||
- [anstyle-owo-colors](crates/owo)
|
||||
- [anstyle-yansi](crates/yansi)
|
||||
|
||||
General utilities:
|
||||
- [anstyle-git](crates/git): Parse Git style descriptions
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
||||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/license/mit>)
|
||||
|
||||
at your option.
|
||||
|
||||
## [Contribute](../../CONTRIBUTING.md)
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally
|
||||
submitted for inclusion in the work by you, as defined in the Apache-2.0
|
||||
license, shall be dual-licensed as above, without any additional terms or
|
||||
conditions.
|
||||
|
||||
[Crates.io]: https://crates.io/crates/anstyle
|
||||
[Documentation]: https://docs.rs/anstyle
|
||||
@@ -0,0 +1,134 @@
|
||||
//! Write ANSI escape code colored text
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
fn main() -> Result<(), lexopt::Error> {
|
||||
let args = Args::parse()?;
|
||||
let stdout = std::io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
|
||||
for fixed in 0..16 {
|
||||
let color = anstyle::Ansi256Color(fixed)
|
||||
.into_ansi()
|
||||
.expect("4-bit range used");
|
||||
let style = style(color, args.layer, args.effects);
|
||||
let _ = print_number(&mut stdout, fixed, style);
|
||||
if fixed == 7 || fixed == 15 {
|
||||
let _ = writeln!(&mut stdout);
|
||||
}
|
||||
}
|
||||
|
||||
for fixed in 16..232 {
|
||||
let col = (fixed - 16) % 36;
|
||||
if col == 0 {
|
||||
let _ = writeln!(stdout);
|
||||
}
|
||||
let color = anstyle::Ansi256Color(fixed);
|
||||
let style = style(color, args.layer, args.effects);
|
||||
let _ = print_number(&mut stdout, fixed, style);
|
||||
}
|
||||
|
||||
let _ = writeln!(stdout);
|
||||
let _ = writeln!(stdout);
|
||||
for fixed in 232..=255 {
|
||||
let color = anstyle::Ansi256Color(fixed);
|
||||
let style = style(color, args.layer, args.effects);
|
||||
let _ = print_number(&mut stdout, fixed, style);
|
||||
}
|
||||
|
||||
let _ = writeln!(stdout);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn style(
|
||||
color: impl Into<anstyle::Color>,
|
||||
layer: Layer,
|
||||
effects: anstyle::Effects,
|
||||
) -> anstyle::Style {
|
||||
let color = color.into();
|
||||
(match layer {
|
||||
Layer::Fg => anstyle::Style::new().fg_color(Some(color)),
|
||||
Layer::Bg => anstyle::Style::new().bg_color(Some(color)),
|
||||
Layer::Underline => anstyle::Style::new().underline_color(Some(color)),
|
||||
}) | effects
|
||||
}
|
||||
|
||||
fn print_number(
|
||||
stdout: &mut std::io::StdoutLock<'_>,
|
||||
fixed: u8,
|
||||
style: anstyle::Style,
|
||||
) -> std::io::Result<()> {
|
||||
write!(stdout, "{style}{fixed:>3X}{style:#}",)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Args {
|
||||
effects: anstyle::Effects,
|
||||
layer: Layer,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default)]
|
||||
enum Layer {
|
||||
#[default]
|
||||
Fg,
|
||||
Bg,
|
||||
Underline,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
fn parse() -> Result<Self, lexopt::Error> {
|
||||
use lexopt::prelude::*;
|
||||
|
||||
let mut res = Args::default();
|
||||
|
||||
let mut args = lexopt::Parser::from_env();
|
||||
while let Some(arg) = args.next()? {
|
||||
match arg {
|
||||
Long("layer") => {
|
||||
res.layer = args.value()?.parse_with(|s| match s {
|
||||
"fg" => Ok(Layer::Fg),
|
||||
"bg" => Ok(Layer::Bg),
|
||||
"underline" => Ok(Layer::Underline),
|
||||
_ => Err("expected values fg, bg, underline"),
|
||||
})?;
|
||||
}
|
||||
Long("effect") => {
|
||||
const EFFECTS: [(&str, anstyle::Effects); 12] = [
|
||||
("bold", anstyle::Effects::BOLD),
|
||||
("dimmed", anstyle::Effects::DIMMED),
|
||||
("italic", anstyle::Effects::ITALIC),
|
||||
("underline", anstyle::Effects::UNDERLINE),
|
||||
("double_underline", anstyle::Effects::DOUBLE_UNDERLINE),
|
||||
("curly_underline", anstyle::Effects::CURLY_UNDERLINE),
|
||||
("dotted_underline", anstyle::Effects::DOTTED_UNDERLINE),
|
||||
("dashed_underline", anstyle::Effects::DASHED_UNDERLINE),
|
||||
("blink", anstyle::Effects::BLINK),
|
||||
("invert", anstyle::Effects::INVERT),
|
||||
("hidden", anstyle::Effects::HIDDEN),
|
||||
("strikethrough", anstyle::Effects::STRIKETHROUGH),
|
||||
];
|
||||
let effect = args.value()?.parse_with(|s| {
|
||||
EFFECTS
|
||||
.into_iter()
|
||||
.find(|(name, _)| *name == s)
|
||||
.map(|(_, effect)| effect)
|
||||
.ok_or_else(|| {
|
||||
format!(
|
||||
"expected one of {}",
|
||||
EFFECTS
|
||||
.into_iter()
|
||||
.map(|(n, _)| n)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
})
|
||||
})?;
|
||||
res.effects = res.effects.insert(effect);
|
||||
}
|
||||
_ => return Err(arg.unexpected()),
|
||||
}
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
+685
@@ -0,0 +1,685 @@
|
||||
/// Any ANSI color code scheme
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Color {
|
||||
/// Available 4-bit ANSI color palette codes
|
||||
///
|
||||
/// The user's terminal defines the meaning of each palette code.
|
||||
Ansi(AnsiColor),
|
||||
/// 256 (8-bit) color support
|
||||
///
|
||||
/// - `0..16` are [`AnsiColor`] palette codes
|
||||
/// - `0..232` map to [`RgbColor`] color values
|
||||
/// - `232..` map to [`RgbColor`] gray-scale values
|
||||
Ansi256(Ansi256Color),
|
||||
/// 24-bit ANSI RGB color codes
|
||||
Rgb(RgbColor),
|
||||
}
|
||||
|
||||
impl Color {
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub fn on(self, background: impl Into<Color>) -> crate::Style {
|
||||
crate::Style::new()
|
||||
.fg_color(Some(self))
|
||||
.bg_color(Some(background.into()))
|
||||
}
|
||||
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub const fn on_default(self) -> crate::Style {
|
||||
crate::Style::new().fg_color(Some(self))
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a foreground color
|
||||
#[inline]
|
||||
pub fn render_fg(self) -> impl core::fmt::Display + Copy {
|
||||
match self {
|
||||
Self::Ansi(color) => color.as_fg_buffer(),
|
||||
Self::Ansi256(color) => color.as_fg_buffer(),
|
||||
Self::Rgb(color) => color.as_fg_buffer(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
pub(crate) fn write_fg_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
let buffer = match self {
|
||||
Self::Ansi(color) => color.as_fg_buffer(),
|
||||
Self::Ansi256(color) => color.as_fg_buffer(),
|
||||
Self::Rgb(color) => color.as_fg_buffer(),
|
||||
};
|
||||
buffer.write_to(write)
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a background color
|
||||
#[inline]
|
||||
pub fn render_bg(self) -> impl core::fmt::Display + Copy {
|
||||
match self {
|
||||
Self::Ansi(color) => color.as_bg_buffer(),
|
||||
Self::Ansi256(color) => color.as_bg_buffer(),
|
||||
Self::Rgb(color) => color.as_bg_buffer(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
pub(crate) fn write_bg_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
let buffer = match self {
|
||||
Self::Ansi(color) => color.as_bg_buffer(),
|
||||
Self::Ansi256(color) => color.as_bg_buffer(),
|
||||
Self::Rgb(color) => color.as_bg_buffer(),
|
||||
};
|
||||
buffer.write_to(write)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn render_underline(self) -> impl core::fmt::Display + Copy {
|
||||
match self {
|
||||
Self::Ansi(color) => color.as_underline_buffer(),
|
||||
Self::Ansi256(color) => color.as_underline_buffer(),
|
||||
Self::Rgb(color) => color.as_underline_buffer(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
pub(crate) fn write_underline_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
let buffer = match self {
|
||||
Self::Ansi(color) => color.as_underline_buffer(),
|
||||
Self::Ansi256(color) => color.as_underline_buffer(),
|
||||
Self::Rgb(color) => color.as_underline_buffer(),
|
||||
};
|
||||
buffer.write_to(write)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AnsiColor> for Color {
|
||||
#[inline]
|
||||
fn from(inner: AnsiColor) -> Self {
|
||||
Self::Ansi(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Ansi256Color> for Color {
|
||||
#[inline]
|
||||
fn from(inner: Ansi256Color) -> Self {
|
||||
Self::Ansi256(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RgbColor> for Color {
|
||||
#[inline]
|
||||
fn from(inner: RgbColor) -> Self {
|
||||
Self::Rgb(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for Color {
|
||||
#[inline]
|
||||
fn from(inner: u8) -> Self {
|
||||
Self::Ansi256(inner.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(u8, u8, u8)> for Color {
|
||||
#[inline]
|
||||
fn from(inner: (u8, u8, u8)) -> Self {
|
||||
Self::Rgb(inner.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Available 4-bit ANSI color palette codes
|
||||
///
|
||||
/// The user's terminal defines the meaning of each palette code.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[repr(u8)]
|
||||
pub enum AnsiColor {
|
||||
/// Black: #0 (foreground code `30`, background code `40`).
|
||||
Black,
|
||||
|
||||
/// Red: #1 (foreground code `31`, background code `41`).
|
||||
Red,
|
||||
|
||||
/// Green: #2 (foreground code `32`, background code `42`).
|
||||
Green,
|
||||
|
||||
/// Yellow: #3 (foreground code `33`, background code `43`).
|
||||
Yellow,
|
||||
|
||||
/// Blue: #4 (foreground code `34`, background code `44`).
|
||||
Blue,
|
||||
|
||||
/// Magenta: #5 (foreground code `35`, background code `45`).
|
||||
Magenta,
|
||||
|
||||
/// Cyan: #6 (foreground code `36`, background code `46`).
|
||||
Cyan,
|
||||
|
||||
/// White: #7 (foreground code `37`, background code `47`).
|
||||
White,
|
||||
|
||||
/// Bright black: #0 (foreground code `90`, background code `100`).
|
||||
BrightBlack,
|
||||
|
||||
/// Bright red: #1 (foreground code `91`, background code `101`).
|
||||
BrightRed,
|
||||
|
||||
/// Bright green: #2 (foreground code `92`, background code `102`).
|
||||
BrightGreen,
|
||||
|
||||
/// Bright yellow: #3 (foreground code `93`, background code `103`).
|
||||
BrightYellow,
|
||||
|
||||
/// Bright blue: #4 (foreground code `94`, background code `104`).
|
||||
BrightBlue,
|
||||
|
||||
/// Bright magenta: #5 (foreground code `95`, background code `105`).
|
||||
BrightMagenta,
|
||||
|
||||
/// Bright cyan: #6 (foreground code `96`, background code `106`).
|
||||
BrightCyan,
|
||||
|
||||
/// Bright white: #7 (foreground code `97`, background code `107`).
|
||||
BrightWhite,
|
||||
}
|
||||
|
||||
impl AnsiColor {
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub fn on(self, background: impl Into<Color>) -> crate::Style {
|
||||
crate::Style::new()
|
||||
.fg_color(Some(self.into()))
|
||||
.bg_color(Some(background.into()))
|
||||
}
|
||||
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub const fn on_default(self) -> crate::Style {
|
||||
crate::Style::new().fg_color(Some(Color::Ansi(self)))
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a foreground color
|
||||
#[inline]
|
||||
pub fn render_fg(self) -> impl core::fmt::Display + Copy {
|
||||
NullFormatter(self.as_fg_str())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_fg_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Black => escape!("3", "0"),
|
||||
Self::Red => escape!("3", "1"),
|
||||
Self::Green => escape!("3", "2"),
|
||||
Self::Yellow => escape!("3", "3"),
|
||||
Self::Blue => escape!("3", "4"),
|
||||
Self::Magenta => escape!("3", "5"),
|
||||
Self::Cyan => escape!("3", "6"),
|
||||
Self::White => escape!("3", "7"),
|
||||
Self::BrightBlack => escape!("9", "0"),
|
||||
Self::BrightRed => escape!("9", "1"),
|
||||
Self::BrightGreen => escape!("9", "2"),
|
||||
Self::BrightYellow => escape!("9", "3"),
|
||||
Self::BrightBlue => escape!("9", "4"),
|
||||
Self::BrightMagenta => escape!("9", "5"),
|
||||
Self::BrightCyan => escape!("9", "6"),
|
||||
Self::BrightWhite => escape!("9", "7"),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_fg_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default().write_str(self.as_fg_str())
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a background color
|
||||
#[inline]
|
||||
pub fn render_bg(self) -> impl core::fmt::Display + Copy {
|
||||
NullFormatter(self.as_bg_str())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_bg_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Black => escape!("4", "0"),
|
||||
Self::Red => escape!("4", "1"),
|
||||
Self::Green => escape!("4", "2"),
|
||||
Self::Yellow => escape!("4", "3"),
|
||||
Self::Blue => escape!("4", "4"),
|
||||
Self::Magenta => escape!("4", "5"),
|
||||
Self::Cyan => escape!("4", "6"),
|
||||
Self::White => escape!("4", "7"),
|
||||
Self::BrightBlack => escape!("10", "0"),
|
||||
Self::BrightRed => escape!("10", "1"),
|
||||
Self::BrightGreen => escape!("10", "2"),
|
||||
Self::BrightYellow => escape!("10", "3"),
|
||||
Self::BrightBlue => escape!("10", "4"),
|
||||
Self::BrightMagenta => escape!("10", "5"),
|
||||
Self::BrightCyan => escape!("10", "6"),
|
||||
Self::BrightWhite => escape!("10", "7"),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_bg_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default().write_str(self.as_bg_str())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_underline_buffer(&self) -> DisplayBuffer {
|
||||
// No per-color codes; must delegate to `Ansi256Color`
|
||||
Ansi256Color::from(*self).as_underline_buffer()
|
||||
}
|
||||
|
||||
/// Change the color to/from bright
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn bright(self, yes: bool) -> Self {
|
||||
if yes {
|
||||
match self {
|
||||
Self::Black => Self::BrightBlack,
|
||||
Self::Red => Self::BrightRed,
|
||||
Self::Green => Self::BrightGreen,
|
||||
Self::Yellow => Self::BrightYellow,
|
||||
Self::Blue => Self::BrightBlue,
|
||||
Self::Magenta => Self::BrightMagenta,
|
||||
Self::Cyan => Self::BrightCyan,
|
||||
Self::White => Self::BrightWhite,
|
||||
Self::BrightBlack => self,
|
||||
Self::BrightRed => self,
|
||||
Self::BrightGreen => self,
|
||||
Self::BrightYellow => self,
|
||||
Self::BrightBlue => self,
|
||||
Self::BrightMagenta => self,
|
||||
Self::BrightCyan => self,
|
||||
Self::BrightWhite => self,
|
||||
}
|
||||
} else {
|
||||
match self {
|
||||
Self::Black => self,
|
||||
Self::Red => self,
|
||||
Self::Green => self,
|
||||
Self::Yellow => self,
|
||||
Self::Blue => self,
|
||||
Self::Magenta => self,
|
||||
Self::Cyan => self,
|
||||
Self::White => self,
|
||||
Self::BrightBlack => Self::Black,
|
||||
Self::BrightRed => Self::Red,
|
||||
Self::BrightGreen => Self::Green,
|
||||
Self::BrightYellow => Self::Yellow,
|
||||
Self::BrightBlue => Self::Blue,
|
||||
Self::BrightMagenta => Self::Magenta,
|
||||
Self::BrightCyan => Self::Cyan,
|
||||
Self::BrightWhite => Self::White,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Report whether the color is bright
|
||||
#[inline]
|
||||
pub fn is_bright(self) -> bool {
|
||||
match self {
|
||||
Self::Black => false,
|
||||
Self::Red => false,
|
||||
Self::Green => false,
|
||||
Self::Yellow => false,
|
||||
Self::Blue => false,
|
||||
Self::Magenta => false,
|
||||
Self::Cyan => false,
|
||||
Self::White => false,
|
||||
Self::BrightBlack => true,
|
||||
Self::BrightRed => true,
|
||||
Self::BrightGreen => true,
|
||||
Self::BrightYellow => true,
|
||||
Self::BrightBlue => true,
|
||||
Self::BrightMagenta => true,
|
||||
Self::BrightCyan => true,
|
||||
Self::BrightWhite => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 256 (8-bit) color support
|
||||
///
|
||||
/// - `0..16` are [`AnsiColor`] palette codes
|
||||
/// - `0..232` map to [`RgbColor`] color values
|
||||
/// - `232..` map to [`RgbColor`] gray-scale values
|
||||
#[allow(clippy::exhaustive_structs)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct Ansi256Color(pub u8);
|
||||
|
||||
impl Ansi256Color {
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub fn on(self, background: impl Into<Color>) -> crate::Style {
|
||||
crate::Style::new()
|
||||
.fg_color(Some(self.into()))
|
||||
.bg_color(Some(background.into()))
|
||||
}
|
||||
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub const fn on_default(self) -> crate::Style {
|
||||
crate::Style::new().fg_color(Some(Color::Ansi256(self)))
|
||||
}
|
||||
|
||||
/// Get the raw value
|
||||
#[inline]
|
||||
pub const fn index(self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Convert to [`AnsiColor`] when there is a 1:1 mapping
|
||||
#[inline]
|
||||
pub const fn into_ansi(self) -> Option<AnsiColor> {
|
||||
match self.index() {
|
||||
0 => Some(AnsiColor::Black),
|
||||
1 => Some(AnsiColor::Red),
|
||||
2 => Some(AnsiColor::Green),
|
||||
3 => Some(AnsiColor::Yellow),
|
||||
4 => Some(AnsiColor::Blue),
|
||||
5 => Some(AnsiColor::Magenta),
|
||||
6 => Some(AnsiColor::Cyan),
|
||||
7 => Some(AnsiColor::White),
|
||||
8 => Some(AnsiColor::BrightBlack),
|
||||
9 => Some(AnsiColor::BrightRed),
|
||||
10 => Some(AnsiColor::BrightGreen),
|
||||
11 => Some(AnsiColor::BrightYellow),
|
||||
12 => Some(AnsiColor::BrightBlue),
|
||||
13 => Some(AnsiColor::BrightMagenta),
|
||||
14 => Some(AnsiColor::BrightCyan),
|
||||
15 => Some(AnsiColor::BrightWhite),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Losslessly convert from [`AnsiColor`]
|
||||
#[inline]
|
||||
pub const fn from_ansi(color: AnsiColor) -> Self {
|
||||
match color {
|
||||
AnsiColor::Black => Self(0),
|
||||
AnsiColor::Red => Self(1),
|
||||
AnsiColor::Green => Self(2),
|
||||
AnsiColor::Yellow => Self(3),
|
||||
AnsiColor::Blue => Self(4),
|
||||
AnsiColor::Magenta => Self(5),
|
||||
AnsiColor::Cyan => Self(6),
|
||||
AnsiColor::White => Self(7),
|
||||
AnsiColor::BrightBlack => Self(8),
|
||||
AnsiColor::BrightRed => Self(9),
|
||||
AnsiColor::BrightGreen => Self(10),
|
||||
AnsiColor::BrightYellow => Self(11),
|
||||
AnsiColor::BrightBlue => Self(12),
|
||||
AnsiColor::BrightMagenta => Self(13),
|
||||
AnsiColor::BrightCyan => Self(14),
|
||||
AnsiColor::BrightWhite => Self(15),
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a foreground color
|
||||
#[inline]
|
||||
pub fn render_fg(self) -> impl core::fmt::Display + Copy {
|
||||
self.as_fg_buffer()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_fg_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default()
|
||||
.write_str("\x1B[38;5;")
|
||||
.write_code(self.index())
|
||||
.write_str("m")
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a background color
|
||||
#[inline]
|
||||
pub fn render_bg(self) -> impl core::fmt::Display + Copy {
|
||||
self.as_bg_buffer()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_bg_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default()
|
||||
.write_str("\x1B[48;5;")
|
||||
.write_code(self.index())
|
||||
.write_str("m")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_underline_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default()
|
||||
.write_str("\x1B[58;5;")
|
||||
.write_code(self.index())
|
||||
.write_str("m")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for Ansi256Color {
|
||||
#[inline]
|
||||
fn from(inner: u8) -> Self {
|
||||
Self(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AnsiColor> for Ansi256Color {
|
||||
#[inline]
|
||||
fn from(inner: AnsiColor) -> Self {
|
||||
Self::from_ansi(inner)
|
||||
}
|
||||
}
|
||||
|
||||
/// 24-bit ANSI RGB color codes
|
||||
#[allow(clippy::exhaustive_structs)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct RgbColor(pub u8, pub u8, pub u8);
|
||||
|
||||
impl RgbColor {
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub fn on(self, background: impl Into<Color>) -> crate::Style {
|
||||
crate::Style::new()
|
||||
.fg_color(Some(self.into()))
|
||||
.bg_color(Some(background.into()))
|
||||
}
|
||||
|
||||
/// Create a [`Style`][crate::Style] with this as the foreground
|
||||
#[inline]
|
||||
pub const fn on_default(self) -> crate::Style {
|
||||
crate::Style::new().fg_color(Some(Color::Rgb(self)))
|
||||
}
|
||||
|
||||
/// Red
|
||||
#[inline]
|
||||
pub const fn r(self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Green
|
||||
#[inline]
|
||||
pub const fn g(self) -> u8 {
|
||||
self.1
|
||||
}
|
||||
|
||||
/// Blue
|
||||
#[inline]
|
||||
pub const fn b(self) -> u8 {
|
||||
self.2
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a foreground color
|
||||
#[inline]
|
||||
pub fn render_fg(self) -> impl core::fmt::Display + Copy {
|
||||
self.as_fg_buffer()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_fg_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default()
|
||||
.write_str("\x1B[38;2;")
|
||||
.write_code(self.r())
|
||||
.write_str(";")
|
||||
.write_code(self.g())
|
||||
.write_str(";")
|
||||
.write_code(self.b())
|
||||
.write_str("m")
|
||||
}
|
||||
|
||||
/// Render the ANSI code for a background color
|
||||
#[inline]
|
||||
pub fn render_bg(self) -> impl core::fmt::Display + Copy {
|
||||
self.as_bg_buffer()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_bg_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default()
|
||||
.write_str("\x1B[48;2;")
|
||||
.write_code(self.r())
|
||||
.write_str(";")
|
||||
.write_code(self.g())
|
||||
.write_str(";")
|
||||
.write_code(self.b())
|
||||
.write_str("m")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_underline_buffer(&self) -> DisplayBuffer {
|
||||
DisplayBuffer::default()
|
||||
.write_str("\x1B[58;2;")
|
||||
.write_code(self.r())
|
||||
.write_str(";")
|
||||
.write_code(self.g())
|
||||
.write_str(";")
|
||||
.write_code(self.b())
|
||||
.write_str("m")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(u8, u8, u8)> for RgbColor {
|
||||
#[inline]
|
||||
fn from(inner: (u8, u8, u8)) -> Self {
|
||||
let (r, g, b) = inner;
|
||||
Self(r, g, b)
|
||||
}
|
||||
}
|
||||
|
||||
const DISPLAY_BUFFER_CAPACITY: usize = 19;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug)]
|
||||
struct DisplayBuffer {
|
||||
buffer: [u8; DISPLAY_BUFFER_CAPACITY],
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl DisplayBuffer {
|
||||
#[must_use]
|
||||
#[inline(never)]
|
||||
fn write_str(mut self, part: &'static str) -> Self {
|
||||
for (i, b) in part.as_bytes().iter().enumerate() {
|
||||
self.buffer[self.len + i] = *b;
|
||||
}
|
||||
self.len += part.len();
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
#[inline(never)]
|
||||
fn write_code(mut self, code: u8) -> Self {
|
||||
let c1: u8 = (code / 100) % 10;
|
||||
let c2: u8 = (code / 10) % 10;
|
||||
let c3: u8 = code % 10;
|
||||
|
||||
let mut printed = false;
|
||||
if c1 != 0 {
|
||||
printed = true;
|
||||
self.buffer[self.len] = b'0' + c1;
|
||||
self.len += 1;
|
||||
}
|
||||
if c2 != 0 || printed {
|
||||
self.buffer[self.len] = b'0' + c2;
|
||||
self.len += 1;
|
||||
}
|
||||
// If we received a zero value we must still print a value.
|
||||
self.buffer[self.len] = b'0' + c3;
|
||||
self.len += 1;
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn as_str(&self) -> &str {
|
||||
// SAFETY: Only `&str` can be written to the buffer
|
||||
#[allow(unsafe_code)]
|
||||
unsafe {
|
||||
core::str::from_utf8_unchecked(&self.buffer[0..self.len])
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
fn write_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
write.write_all(self.as_str().as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Display for DisplayBuffer {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug)]
|
||||
struct NullFormatter(&'static str);
|
||||
|
||||
impl core::fmt::Display for NullFormatter {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.write_str(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "std")]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn max_display_buffer() {
|
||||
let c = RgbColor(255, 255, 255);
|
||||
let actual = c.render_fg().to_string();
|
||||
assert_eq!(actual, "\u{1b}[38;2;255;255;255m");
|
||||
assert_eq!(actual.len(), DISPLAY_BUFFER_CAPACITY);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_size_of() {
|
||||
use core::mem::size_of;
|
||||
dbg!(size_of::<Color>());
|
||||
dbg!(size_of::<AnsiColor>());
|
||||
dbg!(size_of::<Ansi256Color>());
|
||||
dbg!(size_of::<RgbColor>());
|
||||
dbg!(size_of::<DisplayBuffer>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_align() {
|
||||
#[track_caller]
|
||||
fn assert_no_align(d: impl core::fmt::Display) {
|
||||
let expected = format!("{d}");
|
||||
let actual = format!("{d:<10}");
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
assert_no_align(AnsiColor::White.render_fg());
|
||||
assert_no_align(AnsiColor::White.render_bg());
|
||||
assert_no_align(Ansi256Color(0).render_fg());
|
||||
assert_no_align(Ansi256Color(0).render_bg());
|
||||
assert_no_align(RgbColor(0, 0, 0).render_fg());
|
||||
assert_no_align(RgbColor(0, 0, 0).render_bg());
|
||||
assert_no_align(Color::Ansi(AnsiColor::White).render_fg());
|
||||
assert_no_align(Color::Ansi(AnsiColor::White).render_bg());
|
||||
}
|
||||
}
|
||||
+404
@@ -0,0 +1,404 @@
|
||||
/// A set of text effects
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
|
||||
/// ```
|
||||
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Effects(u16);
|
||||
|
||||
impl Effects {
|
||||
/// No [`Effects`] applied
|
||||
const PLAIN: Self = Effects(0);
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub const BOLD: Self = Effects(1 << 0);
|
||||
#[allow(missing_docs)]
|
||||
pub const DIMMED: Self = Effects(1 << 1);
|
||||
/// Not widely supported. Sometimes treated as inverse or blink
|
||||
pub const ITALIC: Self = Effects(1 << 2);
|
||||
/// Style extensions exist for Kitty, VTE, mintty and iTerm2.
|
||||
pub const UNDERLINE: Self = Effects(1 << 3);
|
||||
#[allow(missing_docs)]
|
||||
pub const DOUBLE_UNDERLINE: Self = Effects(1 << 4);
|
||||
#[allow(missing_docs)]
|
||||
pub const CURLY_UNDERLINE: Self = Effects(1 << 5);
|
||||
#[allow(missing_docs)]
|
||||
pub const DOTTED_UNDERLINE: Self = Effects(1 << 6);
|
||||
#[allow(missing_docs)]
|
||||
pub const DASHED_UNDERLINE: Self = Effects(1 << 7);
|
||||
#[allow(missing_docs)]
|
||||
pub const BLINK: Self = Effects(1 << 8);
|
||||
/// Swap foreground and background colors; inconsistent emulation
|
||||
pub const INVERT: Self = Effects(1 << 9);
|
||||
#[allow(missing_docs)]
|
||||
pub const HIDDEN: Self = Effects(1 << 10);
|
||||
/// Characters legible but marked as if for deletion. Not supported in Terminal.app
|
||||
pub const STRIKETHROUGH: Self = Effects(1 << 11);
|
||||
|
||||
/// No effects enabled
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::new();
|
||||
/// ```
|
||||
#[inline]
|
||||
pub const fn new() -> Self {
|
||||
Self::PLAIN
|
||||
}
|
||||
|
||||
/// Check if no effects are enabled
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::new();
|
||||
/// assert!(effects.is_plain());
|
||||
///
|
||||
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
|
||||
/// assert!(!effects.is_plain());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub const fn is_plain(self) -> bool {
|
||||
self.0 == Self::PLAIN.0
|
||||
}
|
||||
|
||||
/// Returns `true` if all of the effects in `other` are contained within `self`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
|
||||
/// assert!(effects.contains(anstyle::Effects::BOLD));
|
||||
///
|
||||
/// let effects = anstyle::Effects::new();
|
||||
/// assert!(!effects.contains(anstyle::Effects::BOLD));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub const fn contains(self, other: Effects) -> bool {
|
||||
(other.0 & self.0) == other.0
|
||||
}
|
||||
|
||||
/// Inserts the specified effects in-place.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::new().insert(anstyle::Effects::new());
|
||||
/// assert!(effects.is_plain());
|
||||
///
|
||||
/// let effects = anstyle::Effects::new().insert(anstyle::Effects::BOLD);
|
||||
/// assert!(effects.contains(anstyle::Effects::BOLD));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub const fn insert(mut self, other: Effects) -> Self {
|
||||
self.0 |= other.0;
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes the specified effects in-place.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = (anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE).remove(anstyle::Effects::BOLD);
|
||||
/// assert!(!effects.contains(anstyle::Effects::BOLD));
|
||||
/// assert!(effects.contains(anstyle::Effects::UNDERLINE));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub const fn remove(mut self, other: Effects) -> Self {
|
||||
self.0 &= !other.0;
|
||||
self
|
||||
}
|
||||
|
||||
/// Reset all effects in-place
|
||||
/// ```rust
|
||||
/// let effects = (anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE).clear();
|
||||
/// assert!(!effects.contains(anstyle::Effects::BOLD));
|
||||
/// assert!(!effects.contains(anstyle::Effects::UNDERLINE));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub const fn clear(self) -> Self {
|
||||
Self::new()
|
||||
}
|
||||
|
||||
/// Enable or disable the specified effects depending on the passed value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::new().set(anstyle::Effects::BOLD, true);
|
||||
/// assert!(effects.contains(anstyle::Effects::BOLD));
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn set(self, other: Self, enable: bool) -> Self {
|
||||
if enable {
|
||||
self.insert(other)
|
||||
} else {
|
||||
self.remove(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate over enabled effects
|
||||
#[inline(always)]
|
||||
pub fn iter(self) -> EffectIter {
|
||||
EffectIter {
|
||||
index: 0,
|
||||
effects: self,
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate over enabled effect indices
|
||||
#[inline(always)]
|
||||
pub(crate) fn index_iter(self) -> EffectIndexIter {
|
||||
EffectIndexIter {
|
||||
index: 0,
|
||||
effects: self,
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the ANSI code
|
||||
#[inline]
|
||||
pub fn render(self) -> impl core::fmt::Display + Copy {
|
||||
EffectsDisplay(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
pub(crate) fn write_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
for index in self.index_iter() {
|
||||
write.write_all(METADATA[index].escape.as_bytes())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::new();
|
||||
/// assert_eq!(format!("{:?}", effects), "Effects()");
|
||||
///
|
||||
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
|
||||
/// assert_eq!(format!("{:?}", effects), "Effects(BOLD | UNDERLINE)");
|
||||
/// ```
|
||||
impl core::fmt::Debug for Effects {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "Effects(")?;
|
||||
for (i, index) in self.index_iter().enumerate() {
|
||||
if i != 0 {
|
||||
write!(f, " | ")?;
|
||||
}
|
||||
write!(f, "{}", METADATA[index].name)?;
|
||||
}
|
||||
write!(f, ")")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
|
||||
/// assert_eq!(format!("{:?}", effects), "Effects(BOLD | UNDERLINE)");
|
||||
/// ```
|
||||
impl core::ops::BitOr for Effects {
|
||||
type Output = Self;
|
||||
|
||||
#[inline(always)]
|
||||
fn bitor(self, rhs: Self) -> Self {
|
||||
self.insert(rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut effects = anstyle::Effects::BOLD;
|
||||
/// effects |= anstyle::Effects::UNDERLINE;
|
||||
/// assert_eq!(format!("{:?}", effects), "Effects(BOLD | UNDERLINE)");
|
||||
/// ```
|
||||
impl core::ops::BitOrAssign for Effects {
|
||||
#[inline]
|
||||
fn bitor_assign(&mut self, other: Self) {
|
||||
*self = self.insert(other);
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = (anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE) - anstyle::Effects::BOLD;
|
||||
/// assert_eq!(format!("{:?}", effects), "Effects(UNDERLINE)");
|
||||
/// ```
|
||||
impl core::ops::Sub for Effects {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, other: Self) -> Self {
|
||||
self.remove(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
|
||||
/// effects -= anstyle::Effects::BOLD;
|
||||
/// assert_eq!(format!("{:?}", effects), "Effects(UNDERLINE)");
|
||||
/// ```
|
||||
impl core::ops::SubAssign for Effects {
|
||||
#[inline]
|
||||
fn sub_assign(&mut self, other: Self) {
|
||||
*self = self.remove(other);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Metadata {
|
||||
pub(crate) name: &'static str,
|
||||
pub(crate) escape: &'static str,
|
||||
}
|
||||
|
||||
pub(crate) const METADATA: [Metadata; 12] = [
|
||||
Metadata {
|
||||
name: "BOLD",
|
||||
escape: escape!("1"),
|
||||
},
|
||||
Metadata {
|
||||
name: "DIMMED",
|
||||
escape: escape!("2"),
|
||||
},
|
||||
Metadata {
|
||||
name: "ITALIC",
|
||||
escape: escape!("3"),
|
||||
},
|
||||
Metadata {
|
||||
name: "UNDERLINE",
|
||||
escape: escape!("4"),
|
||||
},
|
||||
Metadata {
|
||||
name: "DOUBLE_UNDERLINE",
|
||||
escape: escape!("21"),
|
||||
},
|
||||
Metadata {
|
||||
name: "CURLY_UNDERLINE",
|
||||
escape: escape!("4:3"),
|
||||
},
|
||||
Metadata {
|
||||
name: "DOTTED_UNDERLINE",
|
||||
escape: escape!("4:4"),
|
||||
},
|
||||
Metadata {
|
||||
name: "DASHED_UNDERLINE",
|
||||
escape: escape!("4:5"),
|
||||
},
|
||||
Metadata {
|
||||
name: "BLINK",
|
||||
escape: escape!("5"),
|
||||
},
|
||||
Metadata {
|
||||
name: "INVERT",
|
||||
escape: escape!("7"),
|
||||
},
|
||||
Metadata {
|
||||
name: "HIDDEN",
|
||||
escape: escape!("8"),
|
||||
},
|
||||
Metadata {
|
||||
name: "STRIKETHROUGH",
|
||||
escape: escape!("9"),
|
||||
},
|
||||
];
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug)]
|
||||
struct EffectsDisplay(Effects);
|
||||
|
||||
impl core::fmt::Display for EffectsDisplay {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
for index in self.0.index_iter() {
|
||||
f.write_str(METADATA[index].escape)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Enumerate each enabled value in [`Effects`]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct EffectIter {
|
||||
index: usize,
|
||||
effects: Effects,
|
||||
}
|
||||
|
||||
impl Iterator for EffectIter {
|
||||
type Item = Effects;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while self.index < METADATA.len() {
|
||||
let index = self.index;
|
||||
self.index += 1;
|
||||
|
||||
let effect = Effects(1 << index);
|
||||
if self.effects.contains(effect) {
|
||||
return Some(effect);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct EffectIndexIter {
|
||||
index: usize,
|
||||
effects: Effects,
|
||||
}
|
||||
|
||||
impl Iterator for EffectIndexIter {
|
||||
type Item = usize;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while self.index < METADATA.len() {
|
||||
let index = self.index;
|
||||
self.index += 1;
|
||||
|
||||
let effect = Effects(1 << index);
|
||||
if self.effects.contains(effect) {
|
||||
return Some(index);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "std")]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn print_size_of() {
|
||||
use core::mem::size_of;
|
||||
dbg!(size_of::<Effects>());
|
||||
dbg!(size_of::<EffectsDisplay>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_align() {
|
||||
#[track_caller]
|
||||
fn assert_no_align(d: impl core::fmt::Display) {
|
||||
let expected = format!("{d}");
|
||||
let actual = format!("{d:<10}");
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
assert_no_align(Effects::BOLD.render());
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
//! ANSI Text Styling
|
||||
//!
|
||||
//! *A portmanteau of "ansi style"*
|
||||
//!
|
||||
//! `anstyle` provides core types describing [ANSI styling escape
|
||||
//! codes](https://en.wikipedia.org/wiki/ANSI_escape_code) for interoperability
|
||||
//! between crates.
|
||||
//!
|
||||
//! Example use cases:
|
||||
//! - An argument parser allowing callers to define the colors used in the help-output without
|
||||
//! putting the text formatting crate in the public API
|
||||
//! - A style description parser that can work with any text formatting crate
|
||||
//!
|
||||
//! Priorities:
|
||||
//! 1. API stability
|
||||
//! 2. Low compile-time and binary-size overhead
|
||||
//! 3. `const` friendly API for callers to statically define their stylesheet
|
||||
//!
|
||||
//! For integration with text styling crate, see:
|
||||
//! - [anstyle-ansi-term](https://docs.rs/anstyle-ansi-term)
|
||||
//! - [anstyle-crossterm](https://docs.rs/anstyle-crossterm)
|
||||
//! - [anstyle-owo-colors](https://docs.rs/anstyle-owo-colors)
|
||||
//! - [anstyle-termcolor](https://docs.rs/anstyle-termcolor)
|
||||
//! - [anstyle-yansi](https://docs.rs/anstyle-yansi)
|
||||
//!
|
||||
//! User-styling parsers:
|
||||
//! - [anstyle-git](https://docs.rs/anstyle-git): Parse Git style descriptions
|
||||
//! - [anstyle-ls](https://docs.rs/anstyle-ls): Parse `LS_COLORS` style descriptions
|
||||
//!
|
||||
//! Convert to other formats
|
||||
//! - [anstream](https://docs.rs/anstream): A simple cross platform library for writing colored text to a terminal
|
||||
//! - [anstyle-roff](https://docs.rs/anstyle-roff): For converting to ROFF
|
||||
//! - [anstyle-syntect](https://docs.rs/anstyle-syntect): For working with syntax highlighting
|
||||
//!
|
||||
//! Utilities
|
||||
//! - [anstyle-lossy](https://docs.rs/anstyle-lossy): Convert between `anstyle::Color` types
|
||||
//! - [anstyle-parse](https://docs.rs/anstyle-parse): Parsing ANSI Style Escapes
|
||||
//! - [anstyle-wincon](https://docs.rs/anstyle-wincon): Styling legacy Microsoft terminals
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! The core type is [`Style`]:
|
||||
//! ```rust
|
||||
//! let style = anstyle::Style::new().bold();
|
||||
//! ```
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![warn(missing_docs)]
|
||||
#![warn(clippy::std_instead_of_core)]
|
||||
#![warn(clippy::std_instead_of_alloc)]
|
||||
#![warn(clippy::print_stderr)]
|
||||
#![warn(clippy::print_stdout)]
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
mod color;
|
||||
mod effect;
|
||||
mod reset;
|
||||
mod style;
|
||||
|
||||
pub use color::*;
|
||||
pub use effect::*;
|
||||
pub use reset::*;
|
||||
pub use style::*;
|
||||
|
||||
#[doc = include_str!("../README.md")]
|
||||
#[cfg(doctest)]
|
||||
pub struct ReadmeDoctests;
|
||||
@@ -0,0 +1,5 @@
|
||||
macro_rules! escape {
|
||||
($($inner:expr),*) => {
|
||||
concat!("\x1B[", $($inner),*, "m")
|
||||
};
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/// Reset terminal formatting
|
||||
#[allow(clippy::exhaustive_structs)]
|
||||
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Reset;
|
||||
|
||||
impl Reset {
|
||||
/// Render the ANSI code
|
||||
///
|
||||
/// `Reset` also implements `Display` directly, so calling this method is optional.
|
||||
#[inline]
|
||||
pub fn render(self) -> impl core::fmt::Display + Copy {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Display for Reset {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.write_str(RESET)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const RESET: &str = "\x1B[0m";
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "std")]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn print_size_of() {
|
||||
use core::mem::size_of;
|
||||
dbg!(size_of::<Reset>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_align() {
|
||||
#[track_caller]
|
||||
fn assert_no_align(d: impl core::fmt::Display) {
|
||||
let expected = format!("{d}");
|
||||
let actual = format!("{d:<10}");
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
assert_no_align(Reset);
|
||||
assert_no_align(Reset.render());
|
||||
}
|
||||
}
|
||||
+437
@@ -0,0 +1,437 @@
|
||||
use crate::reset::RESET;
|
||||
|
||||
/// ANSI Text styling
|
||||
///
|
||||
/// You can print a `Style` to render the corresponding ANSI code.
|
||||
/// Using the alternate flag `#` will render the ANSI reset code, if needed.
|
||||
/// Together, this makes it convenient to render styles using inline format arguments.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().bold();
|
||||
///
|
||||
/// let value = 42;
|
||||
/// println!("{style}{value}{style:#}");
|
||||
/// ```
|
||||
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Style {
|
||||
fg: Option<crate::Color>,
|
||||
bg: Option<crate::Color>,
|
||||
underline: Option<crate::Color>,
|
||||
effects: crate::Effects,
|
||||
}
|
||||
|
||||
/// # Core
|
||||
impl Style {
|
||||
/// No effects enabled
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new();
|
||||
/// ```
|
||||
#[inline]
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
fg: None,
|
||||
bg: None,
|
||||
underline: None,
|
||||
effects: crate::Effects::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set foreground color
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into()));
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn fg_color(mut self, fg: Option<crate::Color>) -> Self {
|
||||
self.fg = fg;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set background color
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().bg_color(Some(anstyle::AnsiColor::Red.into()));
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn bg_color(mut self, bg: Option<crate::Color>) -> Self {
|
||||
self.bg = bg;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set underline color
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().underline_color(Some(anstyle::AnsiColor::Red.into()));
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn underline_color(mut self, underline: Option<crate::Color>) -> Self {
|
||||
self.underline = underline;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set text effects
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().effects(anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE);
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn effects(mut self, effects: crate::Effects) -> Self {
|
||||
self.effects = effects;
|
||||
self
|
||||
}
|
||||
|
||||
/// Render the ANSI code
|
||||
///
|
||||
/// `Style` also implements `Display` directly, so calling this method is optional.
|
||||
#[inline]
|
||||
pub fn render(self) -> impl core::fmt::Display + Copy {
|
||||
StyleDisplay(self)
|
||||
}
|
||||
|
||||
fn fmt_to(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
use core::fmt::Display as _;
|
||||
|
||||
self.effects.render().fmt(f)?;
|
||||
|
||||
if let Some(fg) = self.fg {
|
||||
fg.render_fg().fmt(f)?;
|
||||
}
|
||||
|
||||
if let Some(bg) = self.bg {
|
||||
bg.render_bg().fmt(f)?;
|
||||
}
|
||||
|
||||
if let Some(underline) = self.underline {
|
||||
underline.render_underline().fmt(f)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write the ANSI code
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn write_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
self.effects.write_to(write)?;
|
||||
|
||||
if let Some(fg) = self.fg {
|
||||
fg.write_fg_to(write)?;
|
||||
}
|
||||
|
||||
if let Some(bg) = self.bg {
|
||||
bg.write_bg_to(write)?;
|
||||
}
|
||||
|
||||
if let Some(underline) = self.underline {
|
||||
underline.write_underline_to(write)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Renders the relevant [`Reset`][crate::Reset] code
|
||||
///
|
||||
/// Unlike [`Reset::render`][crate::Reset::render], this will elide the code if there is nothing to reset.
|
||||
#[inline]
|
||||
pub fn render_reset(self) -> impl core::fmt::Display + Copy {
|
||||
if self != Self::new() {
|
||||
RESET
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the relevant [`Reset`][crate::Reset] code
|
||||
///
|
||||
/// Unlike [`Reset::render`][crate::Reset::render], this will elide the code if there is nothing to reset.
|
||||
#[inline]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn write_reset_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
|
||||
if self != Self::new() {
|
||||
write.write_all(RESET.as_bytes())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// # Convenience
|
||||
impl Style {
|
||||
/// Apply `bold` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().bold();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn bold(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::BOLD);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `dimmed` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().dimmed();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn dimmed(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::DIMMED);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `italic` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().italic();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn italic(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::ITALIC);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `underline` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().underline();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn underline(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::UNDERLINE);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `blink` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().blink();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn blink(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::BLINK);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `invert` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().invert();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn invert(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::INVERT);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `hidden` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().hidden();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn hidden(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::HIDDEN);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply `strikethrough` effect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().strikethrough();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn strikethrough(mut self) -> Self {
|
||||
self.effects = self.effects.insert(crate::Effects::STRIKETHROUGH);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// # Reflection
|
||||
impl Style {
|
||||
/// Get the foreground color
|
||||
#[inline]
|
||||
pub const fn get_fg_color(self) -> Option<crate::Color> {
|
||||
self.fg
|
||||
}
|
||||
|
||||
/// Get the background color
|
||||
#[inline]
|
||||
#[allow(missing_docs)]
|
||||
pub const fn get_bg_color(self) -> Option<crate::Color> {
|
||||
self.bg
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(missing_docs)]
|
||||
pub const fn get_underline_color(self) -> Option<crate::Color> {
|
||||
self.underline
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(missing_docs)]
|
||||
pub const fn get_effects(self) -> crate::Effects {
|
||||
self.effects
|
||||
}
|
||||
|
||||
/// Check if no styling is enabled
|
||||
#[inline]
|
||||
pub const fn is_plain(self) -> bool {
|
||||
self.fg.is_none()
|
||||
&& self.bg.is_none()
|
||||
&& self.underline.is_none()
|
||||
&& self.effects.is_plain()
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style: anstyle::Style = anstyle::Effects::BOLD.into();
|
||||
/// ```
|
||||
impl From<crate::Effects> for Style {
|
||||
#[inline]
|
||||
fn from(effects: crate::Effects) -> Self {
|
||||
Self::new().effects(effects)
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new() | anstyle::Effects::BOLD.into();
|
||||
/// ```
|
||||
impl core::ops::BitOr<crate::Effects> for Style {
|
||||
type Output = Self;
|
||||
|
||||
#[inline(always)]
|
||||
fn bitor(mut self, rhs: crate::Effects) -> Self {
|
||||
self.effects |= rhs;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut style = anstyle::Style::new();
|
||||
/// style |= anstyle::Effects::BOLD.into();
|
||||
/// ```
|
||||
impl core::ops::BitOrAssign<crate::Effects> for Style {
|
||||
#[inline]
|
||||
fn bitor_assign(&mut self, other: crate::Effects) {
|
||||
self.effects |= other;
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let style = anstyle::Style::new().bold().underline() - anstyle::Effects::BOLD.into();
|
||||
/// ```
|
||||
impl core::ops::Sub<crate::Effects> for Style {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn sub(mut self, other: crate::Effects) -> Self {
|
||||
self.effects -= other;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut style = anstyle::Style::new().bold().underline();
|
||||
/// style -= anstyle::Effects::BOLD.into();
|
||||
/// ```
|
||||
impl core::ops::SubAssign<crate::Effects> for Style {
|
||||
#[inline]
|
||||
fn sub_assign(&mut self, other: crate::Effects) {
|
||||
self.effects -= other;
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// let effects = anstyle::Effects::BOLD;
|
||||
/// assert_eq!(anstyle::Style::new().effects(effects), effects);
|
||||
/// assert_ne!(anstyle::Effects::UNDERLINE | effects, effects);
|
||||
/// assert_ne!(anstyle::RgbColor(0, 0, 0).on_default() | effects, effects);
|
||||
/// ```
|
||||
impl PartialEq<crate::Effects> for Style {
|
||||
#[inline]
|
||||
fn eq(&self, other: &crate::Effects) -> bool {
|
||||
let other = Self::from(*other);
|
||||
*self == other
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Display for Style {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
if f.alternate() {
|
||||
self.render_reset().fmt(f)
|
||||
} else {
|
||||
self.fmt_to(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug)]
|
||||
struct StyleDisplay(Style);
|
||||
|
||||
impl core::fmt::Display for StyleDisplay {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
self.0.fmt_to(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
fn print_size_of() {
|
||||
use core::mem::size_of;
|
||||
dbg!(size_of::<Style>());
|
||||
dbg!(size_of::<StyleDisplay>());
|
||||
}
|
||||
Reference in new issue
Block a user