color-print-0.3.6/.cargo_vcs_info.json0000644000000001510000000000100133070ustar { "git": { "sha1": "dbe4057f92480ed8f3e0434c821fa1d88469b53b" }, "path_in_vcs": "color-print" }color-print-0.3.6/Cargo.toml0000644000000022400000000000100113060ustar # 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 = "2018" name = "color-print" version = "0.3.6" authors = ["Johann David "] description = "Colorize and stylize strings for terminal at compile-time, by using an HTML-like syntax" readme = "README.md" keywords = [ "color", "terminal", "ansi", "terminfo", ] license = "MIT OR Apache-2.0" repository = "https://gitlab.com/dajoha/color-print" [dependencies.color-print-proc-macro] version = "0.3.6" [dependencies.lazy_static] version = "1.4" optional = true [dependencies.terminfo_crate] version = "0.7.3" optional = true package = "terminfo" [features] terminfo = [ "color-print-proc-macro/terminfo", "lazy_static", "terminfo_crate", ] color-print-0.3.6/Cargo.toml.orig000064400000000000000000000012411046102023000147670ustar 00000000000000[package] name = "color-print" version = "0.3.6" edition = "2018" authors = ["Johann David "] license = "MIT OR Apache-2.0" description = "Colorize and stylize strings for terminal at compile-time, by using an HTML-like syntax" keywords = ["color", "terminal", "ansi", "terminfo"] repository = "https://gitlab.com/dajoha/color-print" readme = "README.md" [features] terminfo = [ "color-print-proc-macro/terminfo", "lazy_static", "terminfo_crate", ] [dependencies] color-print-proc-macro = "0.3.6" lazy_static = { version = "1.4", optional = true } terminfo_crate = { version = "0.7.3", optional = true, package = "terminfo" } color-print-0.3.6/LICENSE-APACHE000064400000000000000000000227731046102023000140410ustar 00000000000000 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 color-print-0.3.6/LICENSE-MIT000064400000000000000000000017771046102023000135520ustar 00000000000000Permission 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. color-print-0.3.6/README.md000064400000000000000000000276271046102023000133770ustar 00000000000000# color-print Colorize and stylize strings for terminal at compile-time, by using an HTML-like syntax. This library provides the following macros: - `cformat!( [, ARGS...])` - `cprint!( [, ARGS...])` - `cprintln!( [, ARGS...])` - `cstr!()` - `untagged!()` `cformat!()`, `cprint!()`, and `cprintln!()` have the same syntax as `format!()`, `print!()` and `println!()` respectively, but they accept an additional syntax inside the format string: HTML-like tags which add ANSI colors/styles at compile-time. `cstr!()` only transforms the given string literal into another string literal, without formatting anything else than the colors tag. `untagged!()` removes all the tags found in the given string literal. ### What does it do ? By default, the provided macros will replace the tags found in the format string by ANSI hexadecimal escape codes. E.g.: ```rust cprintln!("HELLO WORLD") cprintln!("HELLO WORLD"); // Alternative, shorter syntax ``` will be replaced by: ```rust println!("HELLO \u{1b}[31mWORLD\u{1b}[39m") ``` *Note*: it is possible to change this behaviour by activating the feature `terminfo`. Then it will question the `terminfo` database at runtime in order to know which sequence to write for each kind of styling/colorizing (see below for more detail). ## Pros/cons of this crate ### Pros * Styling is processed at compile-time, so the runtime payload is inexistant (unless the feature `terminfo` is activated); * Nested tags are well handled, e.g. `"........."`; * Some optimizations are performed to avoid redundant ANSI sequences, because these optimizations can be done at compile-time without impacting the runtime; * Almost every tag has a short name, so colorizing can be done quickly: `"my blue word"`; * Each provided macro can be used exactly in the same way as the standard `format!`-like macros; e.g., positional arguments and named arguments can be used as usual; * Supports 16, 256 and 16M colors; * Fine-grained error handling (errors will be given at compile-time). ### Cons * Not compatible with non-ANSI terminals. ## Introduction ### Basic example ```rust use color_print::cprintln; cprintln!("Hello world!"); ``` ### Closing a tag more simply: the `` tag Basically, tags must be closed by giving *exactly* the same colors/styles as their matching open tag (with a slash `/` at the beginning), e.g: `...`. But it can be tedious! So, it is also possible to close the last open tag simply with ``: ```rust cprintln!("Hello world!"); ``` ### Combining colors and styles Multiple styles and colors can be combined into a single tag by separating them with the `,` comma character: ```rust cprintln!("This a green and bold text."); // The same, but closing with the tag: cprintln!("This a green and bold text."); ``` ### Nesting tags Any tag can be nested with any other. *Note*: The closing tags must match correctly (following the basic rules of nesting for HTML tags), but it can always be simplified by using the tag ``. Example of nested tags: ```rust cprintln!("This is green, then green and bold, then green again"); cprintln!("This is green, then green and bold, then green again"); // Colors can be nested as well: cprintln!("This is green, then blue, then green again"); cprintln!("This is green, then blue, then green again"); ``` ### Unclosed tags are automatically closed at the end of the format string Tags which have not been closed manually will be closed automatically, which means that the ANSI sequences needed to go back to the original state will be added: ```rust // The two following lines are strictly equivalent: cprintln!("Hello"); cprintln!("Hello"); ``` ### How to display the chars `<` and `>` verbatim As for `{` and `}` in standard format strings, the chars `<` and `>` have to be doubled in order to display them verbatim: ```rust cprintln!("This is an angle bracket character: <<, and here is another one: >>"); ``` ## Optimization: no redundant ANSI codes The expanded format string will only contain the *needed* ANSI codes. This is done by making a diff of the different style attributes, each time a tag is encountered, instead of mechanically adding the ANSI codes. E.g., several nested `` tags will only produce one bold ANSI sequence: ```rust cprintln!(" A B C ") ``` will be replaced by: ```rust println!("\u{1b}[1m A \u{1b}[34m B \u{1b}[39m C \u{1b}[22m") // ^-------^ ^--------^ ^--------^ ^--------^ // bold blue color bold // reset reset ``` ## The feature `terminfo` Instead of inserting ANSI sequences directly into the format string, it is possible to activate the feature `terminfo`: this will add the format sequences at runtime, by consulting the `terminfo` database. This has one pro and several cons: ##### Pros * This adds a level of compatibility for some terminals. ##### Cons * This adds a little runtime payload; * This adds two dependencies: `lazy_static` and `terminfo`; * The styles `` and `` are not handled; * With `terminfo`, many styles are not resettable individually, which implies longer format sequences for the same result; * For now, the provided macros can only be used in one thread. `lazy_static`: https://crates.io/crates/lazy_static `terminfo`: https://crates.io/crates/terminfo ## Naming rules of the tags: Each tag has at least a **long name**, like `` or ``. The tags directly relative to *colors* (like ``, ``, ``..., as opposed to *style* tags like ``, ``...) have some common naming rules: * Each tag has four variants: - ``: the normal, foreground color; - `` or ``: the bright, foreground color; - ``, ``: the normal, background color; - ``, ``, `` or ``: the bright, background color; * Each tag has a *shortcut*, with a base letter for each color; example with the `x` letter: - ``: the normal, foreground color; - ``: the bright, foreground color; - ``, ``: the normal, background color; - ``, ``: the bright, background color; * Each color's shortcut letter is simply the **first letter of its name** (excepted for `` which is the shortcut for ``), e.g. `` is the shortcut for ``; * Each color's tag which is uppercase is a **background color**; * Each tag which has a trailing exclamation point `!` is a **bright color**; ## List of accepted tags: The two first columns show which styles are supported, respectively with the default crate features (ANSI column), and with the feature `terminfo` being activated. | ANSI | Terminfo | Shortcuts | Long names | Aliases | | ---- | -------- | --------- | ----------------------- | ----------------------------------------------- | | X | X | `` | `` | `` `` | | X | X | | `` | | | X | X | `` | `` | | | X | | | `` | | | X | X | | `` | `` | | X | | | `` | `` | | X | X | `` | `` | `` | | X | X | | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | X | `` | `` | `` `` `` | | X | | | `` | `<#RRGGBB>` | | X | | | `` | `` `` | | X | | `<0>`...`<255>` | `` | `` `` | | X | | `` | `` | `` `` `` `` | License: MIT OR Apache-2.0 color-print-0.3.6/src/lib.rs000064400000000000000000000437631046102023000140220ustar 00000000000000//! Colorize and stylize strings for terminal at compile-time, by using an HTML-like syntax. //! //! This library provides the following macros: //! //! - `cformat!( [, ARGS...])` //! - `cprint!( [, ARGS...])` //! - `cprintln!( [, ARGS...])` //! - `cstr!()` //! - `untagged!()` //! //! [`cformat!()`], [`cprint!()`], and [`cprintln!()`] have the same syntax as `format!()`, //! `print!()` and `println!()` respectively, but they accept an additional syntax inside the //! format string: HTML-like tags which add ANSI colors/styles at compile-time. //! //! [`cstr!()`] only transforms the given string literal into another string literal, without //! formatting anything else than the colors tag. //! //! [`untagged!()`] removes all the tags found in the given string literal. //! //! ## What does it do ? //! //! By default, the provided macros will replace the tags found in the format string by ANSI //! hexadecimal escape codes. E.g.: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! cprintln!("HELLO WORLD"); //! cprintln!("HELLO WORLD"); // Alternative, shorter syntax //! # } //! ``` //! //! will be replaced by: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! println!("HELLO \u{1b}[31mWORLD\u{1b}[39m") //! # } //! ``` //! //! *Note*: it is possible to change this behaviour by activating the feature `terminfo`. Then it //! will question the `terminfo` database at runtime in order to know which sequence to write for //! each kind of styling/colorizing (see below for more detail). //! //! # Pros/cons of this crate //! //! ## Pros //! //! * Styling is processed at compile-time, so the runtime payload is inexistant (unless the //! feature `terminfo` is activated); //! * Nested tags are well handled, e.g. `"........."`; //! * Some optimizations are performed to avoid redundant ANSI sequences, because these //! optimizations can be done at compile-time without impacting the runtime; //! * Almost every tag has a short name, so colorizing can be done quickly: `"my blue word"`; //! * Each provided macro can be used exactly in the same way as the standard `format!`-like //! macros; e.g., positional arguments and named arguments can be used as usual; //! * Supports 16, 256 and 16M colors; //! * Fine-grained error handling (errors will be given at compile-time). //! //! ## Cons //! //! * Not compatible with non-ANSI terminals. //! //! # Introduction //! //! ## Basic example //! //! ``` //! use color_print::cprintln; //! cprintln!("Hello world!"); //! ``` //! //! ## Closing a tag more simply: the `` tag //! //! Basically, tags must be closed by giving *exactly* the same colors/styles as their matching //! open tag (with a slash `/` at the beginning), e.g: `...`. But it can be //! tedious! //! //! So, it is also possible to close the last open tag simply with ``: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! cprintln!("Hello world!"); //! # } //! ``` //! //! ## Combining colors and styles //! //! Multiple styles and colors can be combined into a single tag by separating them with the `,` //! comma character: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! cprintln!("This a green and bold text."); //! // The same, but closing with the tag: //! cprintln!("This a green and bold text."); //! # } //! ``` //! //! ## Nesting tags //! //! Any tag can be nested with any other. //! //! *Note*: The closing tags must match correctly (following the basic rules of nesting for HTML //! tags), but it can always be simplified by using the tag ``. //! //! Example of nested tags: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! cprintln!("This is green, then green and bold, then green again"); //! cprintln!("This is green, then green and bold, then green again"); //! //! // Colors can be nested as well: //! cprintln!("This is green, then blue, then green again"); //! cprintln!("This is green, then blue, then green again"); //! # } //! ``` //! //! ## Unclosed tags are automatically closed at the end of the format string //! //! Tags which have not been closed manually will be closed automatically, which means that the //! ANSI sequences needed to go back to the original state will be added: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! // The two following lines are strictly equivalent: //! cprintln!("Hello"); //! cprintln!("Hello"); //! # } //! ``` //! //! ## How to display the chars `<` and `>` verbatim //! //! As for `{` and `}` in standard format strings, the chars `<` and `>` have to be doubled in //! order to display them verbatim: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! cprintln!("This is an angle bracket character: <<, and here is another one: >>"); //! # } //! ``` //! //! # Optimization: no redundant ANSI codes //! //! The expanded format string will only contain the *needed* ANSI codes. This is done by making a //! diff of the different style attributes, each time a tag is encountered, instead of mechanically //! adding the ANSI codes. //! //! E.g., several nested `` tags will only produce one bold ANSI sequence: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! cprintln!(" A B C ") //! # } //! ``` //! //! will be replaced by: //! //! ``` //! # use color_print::cprintln; //! # fn main() { //! println!("\u{1b}[1m A \u{1b}[34m B \u{1b}[39m C \u{1b}[22m") //! // ^-------^ ^--------^ ^--------^ ^--------^ //! // bold blue color bold //! // reset reset //! # } //! ``` //! //! # The feature `terminfo` //! //! Instead of inserting ANSI sequences directly into the format string, it is possible to activate //! the feature `terminfo`: this will add the format sequences at runtime, by consulting the //! `terminfo` database. //! //! This has one pro and several cons: //! //! #### Pros //! //! * This adds a level of compatibility for some terminals. //! //! #### Cons //! //! * This adds a little runtime payload; //! * This adds two dependencies: [`lazy_static`] and [`terminfo`]; //! * The styles `` and `` are not handled; //! * With `terminfo`, many styles are not resettable individually, which implies longer format //! sequences for the same result; //! * For now, the provided macros can only be used in one thread. //! //! [`lazy_static`]: https://crates.io/crates/lazy_static //! [`terminfo`]: https://crates.io/crates/terminfo //! //! # Naming rules of the tags: //! //! Each tag has at least a **long name**, like `` or ``. //! //! The tags directly relative to *colors* (like ``, ``, ``..., as //! opposed to *style* tags like ``, ``...) have some common naming rules: //! //! * Each tag has four variants: //! - ``: the normal, foreground color; //! - `` or ``: the bright, foreground color; //! - ``, ``: the normal, background color; //! - ``, ``, `` or ``: the bright, //! background color; //! * Each tag has a *shortcut*, with a base letter for each color; example with the `x` letter: //! - ``: the normal, foreground color; //! - ``: the bright, foreground color; //! - ``, ``: the normal, background color; //! - ``, ``: the bright, background color; //! * Each color's shortcut letter is simply the **first letter of its name** (excepted for `` //! which is the shortcut for ``), e.g. `` is the shortcut for ``; //! * Each color's tag which is uppercase is a **background color**; //! * Each tag which has a trailing exclamation point `!` is a **bright color**; //! //! # List of accepted tags: //! //! The two first columns show which styles are supported, respectively with the default crate //! features (ANSI column), and with the feature `terminfo` being activated. //! //! | ANSI | Terminfo | Shortcuts | Long names | Aliases | //! | ---- | -------- | --------- | ----------------------- | ----------------------------------------------- | //! | X | X | `` | `` | `` `` | //! | X | X | | `` | | //! | X | X | `` | `` | | //! | X | | | `` | | //! | X | X | | `` | `` | //! | X | | | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | X | `` | `` | `` `` `` | //! | X | | | `` | `<#RRGGBB>` | //! | X | | | `` | `` `` | //! | X | | `<0>`...`<255>` | `` | `` `` | //! | X | | `` | `` | `` `` `` `` | pub use color_print_proc_macro::{cformat, cprint, cprintln, cstr, untagged}; #[cfg(feature = "terminfo")] mod terminfo; #[cfg(feature = "terminfo")] pub use terminfo::*; #[cfg(test)] mod tests { use super::*; #[cfg(feature = "terminfo")] pub mod color_print { pub use super::*; } #[test] fn format_no_arg() { assert_eq!(cformat!(), ""); cprint!(); cprintln!(); } #[test] fn format_no_color() { assert_eq!(cformat!(""), ""); assert_eq!(cformat!("Hi"), "Hi"); assert_eq!(cformat!("Hi {}", 12), "Hi 12"); assert_eq!(cformat!("Hi {n} {}", 12, n = 24), "Hi 24 12"); } #[test] #[cfg(not(feature = "terminfo"))] #[rustfmt::skip] fn format_basic() { assert_eq!(cformat!("Hi"), "\u{1b}[31mHi\u{1b}[39m"); assert_eq!(cformat!("Hi"), "\u{1b}[31mHi\u{1b}[39m"); assert_eq!(cformat!("Hi"), "\u{1b}[31mHi\u{1b}[39m"); assert_eq!(cformat!("Hi"), "\u{1b}[41mHi\u{1b}[49m"); assert_eq!(cformat!("Hi"), "\u{1b}[41mHi\u{1b}[49m"); assert_eq!(cformat!("Hi"), "\u{1b}[41mHi\u{1b}[49m"); assert_eq!( cformat!("Hi word !"), "Hi \u{1b}[1mword\u{1b}[22m !" ); assert_eq!(cformat!("Hi word !"), "Hi \u{1b}[1mword\u{1b}[22m !"); assert_eq!(cformat!("Hi word !"), "Hi \u{1b}[1mword\u{1b}[22m !"); assert_eq!( cformat!(" bold dim underline strike reverse conceal italics blink "), " \u{1b}[1mbold\u{1b}[22m \u{1b}[2mdim\u{1b}[22m \u{1b}[4munderline\u{1b}[24m \u{1b}[9mstrike\u{1b}[29m \u{1b}[7mreverse\u{1b}[27m \u{1b}[8mconceal\u{1b}[28m \u{1b}[3mitalics\u{1b}[23m \u{1b}[5mblink\u{1b}[25m " ); } #[test] #[ignore] #[cfg(not(feature = "terminfo"))] fn bold_and_dim_should_be_optimized() { assert_eq!( cformat!("BOLDDIM"), "\u{1b}[1mBOLD\u{1b}[2mDIM\u{1b}[22m" ); } #[test] #[cfg(not(feature = "terminfo"))] fn format_multiple() { assert_eq!( cformat!("Hi word red !"), "Hi \u{1b}[1mword\u{1b}[22m \u{1b}[31mred\u{1b}[39m !" ); } #[test] #[cfg(not(feature = "terminfo"))] fn format_optimization() { assert_eq!( cformat!("REDBLUERED"), "\u{1b}[31mRED\u{1b}[34mBLUE\u{1b}[31mRED\u{1b}[39m" ); assert_eq!( cformat!("BLUERED"), "\u{1b}[34mBLUE\u{1b}[31mRED\u{1b}[39m" ); assert_eq!(cformat!("Text"), "Text"); } #[test] #[cfg(not(feature = "terminfo"))] #[rustfmt::skip] fn format_auto_close_tag() { assert_eq!( cformat!("REDBLUE"), "\u{1b}[31mRED\u{1b}[34mBLUE\u{1b}[39m" ); assert!( cformat!("REDBOLD") == "\u{1b}[31mRED\u{1b}[1mBOLD\u{1b}[22m\u{1b}[39m" || cformat!("REDBOLD") == "\u{1b}[31mRED\u{1b}[1mBOLD\u{1b}[39m\u{1b}[22m" ); } #[test] #[cfg(feature = "terminfo")] fn terminfo_format_basic() { assert_eq!(cformat!("Hi"), format!("{}Hi{}", *RED, *CLEAR)); assert_eq!( cformat!("Hi word !"), format!("Hi {}word{} !", *BOLD, *CLEAR) ); } #[test] #[cfg(feature = "terminfo")] fn terminfo_format_multiple() { assert_eq!( cformat!("Hi word red !"), format!("Hi {}word{} {}red{} !", *BOLD, *CLEAR, *RED, *CLEAR) ); } #[test] #[cfg(feature = "terminfo")] fn terminfo_format_auto_close_tag() { assert_eq!( cformat!("REDBLUE"), format!("{}RED{}BLUE{}", *RED, *BLUE, *CLEAR) ); assert_eq!( cformat!("REDBOLD"), format!("{}RED{}BOLD{}", *RED, *BOLD, *CLEAR) ); } #[test] fn untagged() { assert_eq!(untagged!(""), ""); assert_eq!(untagged!("hi"), "hi"); assert_eq!(untagged!("hi"), "hi"); assert_eq!(untagged!("hi"), "hi"); assert_eq!(untagged!("hi all"), "hi all"); assert_eq!(untagged!("hi all"), "hi all"); } } color-print-0.3.6/src/terminfo/color.rs000064400000000000000000000057751046102023000162160ustar 00000000000000//! Lazy constants representing the ANSI codes for setting terminal colors. //! //! Terminfo is used internally to guess the right codes. use lazy_static::lazy_static; use terminfo_crate::{capability as cap, expand, Capability}; use crate::terminfo::TERMINFO; #[rustfmt::skip] lazy_static! { pub static ref BLACK: String = foreground(0); pub static ref RED: String = foreground(1); pub static ref GREEN: String = foreground(2); pub static ref YELLOW: String = foreground(3); pub static ref BLUE: String = foreground(4); pub static ref MAGENTA: String = foreground(5); pub static ref CYAN: String = foreground(6); pub static ref WHITE: String = foreground(7); pub static ref BRIGHT_BLACK: String = foreground(8); pub static ref BRIGHT_RED: String = foreground(9); pub static ref BRIGHT_GREEN: String = foreground(10); pub static ref BRIGHT_YELLOW: String = foreground(11); pub static ref BRIGHT_BLUE: String = foreground(12); pub static ref BRIGHT_MAGENTA: String = foreground(13); pub static ref BRIGHT_CYAN: String = foreground(14); pub static ref BRIGHT_WHITE: String = foreground(15); pub static ref BG_BLACK: String = background(0); pub static ref BG_RED: String = background(1); pub static ref BG_GREEN: String = background(2); pub static ref BG_YELLOW: String = background(3); pub static ref BG_BLUE: String = background(4); pub static ref BG_MAGENTA: String = background(5); pub static ref BG_CYAN: String = background(6); pub static ref BG_WHITE: String = background(7); pub static ref BG_BRIGHT_BLACK: String = background(8); pub static ref BG_BRIGHT_RED: String = background(9); pub static ref BG_BRIGHT_GREEN: String = background(10); pub static ref BG_BRIGHT_YELLOW: String = background(11); pub static ref BG_BRIGHT_BLUE: String = background(12); pub static ref BG_BRIGHT_MAGENTA: String = background(13); pub static ref BG_BRIGHT_CYAN: String = background(14); pub static ref BG_BRIGHT_WHITE: String = background(15); } /// Gets the ANSI code which sets the foreground color to the given color (0 to 15 included). fn foreground(v: u8) -> String { #[cfg(debug_assertions)] assert!(v < 16); expand1_string::(v) } /// Gets the ANSI code which sets the background color to the given color (0 to 15 included). fn background(v: u8) -> String { #[cfg(debug_assertions)] assert!(v < 16); expand1_string::(v) } /// Shortcut function for the `foreground()` and `background()` functions. fn expand1_string<'a, T>(v: u8) -> String where T: Capability<'a> + AsRef<[u8]>, { expand1::<'a, T>(v).unwrap_or_else(|| String::new()) } /// Shortcut function for the `foreground()` and `background()` functions. fn expand1<'a, T>(v: u8) -> Option where T: Capability<'a> + AsRef<[u8]>, { let info = (*TERMINFO).as_ref()?; let e = expand!(info.get::()?.as_ref(); v).ok()?; let s = std::str::from_utf8(&e).ok()?; Some(s.to_owned()) } color-print-0.3.6/src/terminfo/mod.rs000064400000000000000000000003601046102023000156400ustar 00000000000000mod color; mod style; pub use color::*; pub use style::*; use lazy_static::lazy_static; use terminfo_crate::Database; lazy_static! { /// The terminfo database. static ref TERMINFO: Option = Database::from_env().ok(); } color-print-0.3.6/src/terminfo/style.rs000064400000000000000000000026221046102023000162240ustar 00000000000000//! Lazy constants representing the ANSI codes for setting terminal styles, like bold, underline, //! etc... //! //! Terminfo is used internally to guess the right codes. use lazy_static::lazy_static; use terminfo_crate::{capability as cap, expand, Capability}; use crate::terminfo::TERMINFO; lazy_static! { pub static ref CLEAR: String = style::(); pub static ref BOLD: String = style::(); pub static ref DIM: String = style::(); pub static ref BLINK: String = style::(); pub static ref ITALICS: String = style::(); pub static ref REVERSE: String = style::(); pub static ref UNDERLINE: String = style::(); pub static ref NO_ITALICS: String = style::(); pub static ref NO_UNDERLINE: String = style::(); } /// Gets the ANSI code which sets the given style `T`. fn style<'a, T>() -> String where T: Capability<'a> + AsRef<[u8]>, { expand0::<'a, T>().unwrap_or_else(|| String::new()) } /// Shortcut function for the `style()` function. fn expand0<'a, T>() -> Option where T: Capability<'a> + AsRef<[u8]>, { let info = (*TERMINFO).as_ref()?; let e = expand!(info.get::()?.as_ref()).ok()?; let s = std::str::from_utf8(&e).ok()?; Some(s.to_owned()) }