From d60e41a57d773c3cf5ba0ba15c94394b9a90c39b Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 4 Dec 2024 19:47:49 -0800 Subject: [PATCH] feat: add new anstyle-ratatui crate --- Cargo.lock | 320 +++++++++++++++++++++++++- README.md | 4 + crates/anstyle-ratatui/Cargo.toml | 36 +++ crates/anstyle-ratatui/LICENSE-APACHE | 202 ++++++++++++++++ crates/anstyle-ratatui/LICENSE-MIT | 19 ++ crates/anstyle-ratatui/README,md | 27 +++ crates/anstyle-ratatui/src/lib.rs | 88 +++++++ typos.toml | 4 + 8 files changed, 697 insertions(+), 3 deletions(-) create mode 100644 crates/anstyle-ratatui/Cargo.toml create mode 100644 crates/anstyle-ratatui/LICENSE-APACHE create mode 100644 crates/anstyle-ratatui/LICENSE-MIT create mode 100644 crates/anstyle-ratatui/README,md create mode 100644 crates/anstyle-ratatui/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 1995ca57..d3a7940b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "ansi_term" version = "0.12.1" @@ -155,6 +161,14 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "anstyle-ratatui" +version = "0.1.0" +dependencies = [ + "anstyle 1.0.10", + "ratatui", +] + [[package]] name = "anstyle-roff" version = "0.3.5" @@ -175,7 +189,7 @@ dependencies = [ "anstyle-lossy", "html-escape", "snapbox", - "unicode-width", + "unicode-width 0.2.0", ] [[package]] @@ -283,6 +297,21 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bdcae87153686017415ce77e48c53e6818a0a058f0e21b56640d1e944967ef8" +[[package]] +name = "cassowary" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" + +[[package]] +name = "castaway" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5" +dependencies = [ + "rustversion", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -309,7 +338,7 @@ dependencies = [ "anstream 0.3.2", "anstyle 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", "clap_lex", - "strsim", + "strsim 0.10.0", "terminal_size", ] @@ -319,7 +348,7 @@ version = "4.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro2", "quote", "syn", @@ -361,6 +390,20 @@ dependencies = [ "owo-colors", ] +[[package]] +name = "compact_str" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "rustversion", + "ryu", + "static_assertions", +] + [[package]] name = "condtype" version = "1.3.0" @@ -375,8 +418,11 @@ checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" dependencies = [ "bitflags 2.6.0", "crossterm_winapi", + "mio", "parking_lot", "rustix 0.38.38", + "signal-hook", + "signal-hook-mio", "winapi", ] @@ -389,6 +435,47 @@ dependencies = [ "winapi", ] +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.11.1", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difference" version = "2.0.0" @@ -420,6 +507,18 @@ dependencies = [ "syn", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.9" @@ -442,6 +541,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" + [[package]] name = "getrandom" version = "0.2.15" @@ -453,12 +558,29 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + [[package]] name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -480,6 +602,32 @@ dependencies = [ "utf8-width", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + +[[package]] +name = "instability" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b829f37dead9dc39df40c2d3376c179fdfd2ac771f53f55d3c30dc096a3c0c6e" +dependencies = [ + "darling", + "indoc", + "pretty_assertions", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "io-lifetimes" version = "1.0.11" @@ -511,6 +659,15 @@ dependencies = [ "is-terminal", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -563,12 +720,39 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "lru" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" +dependencies = [ + "hashbrown", +] + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.52.0", +] + [[package]] name = "normalize-line-endings" version = "0.3.0" @@ -620,6 +804,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -629,6 +819,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "pretty_assertions" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.89" @@ -712,6 +912,27 @@ dependencies = [ "rand_core", ] +[[package]] +name = "ratatui" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b" +dependencies = [ + "bitflags 2.6.0", + "cassowary", + "compact_str", + "crossterm", + "indoc", + "instability", + "itertools", + "lru", + "paste", + "strum", + "unicode-segmentation", + "unicode-truncate", + "unicode-width 0.2.0", +] + [[package]] name = "redox_syscall" version = "0.5.7" @@ -766,6 +987,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustversion" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" + [[package]] name = "rusty-fork" version = "0.3.0" @@ -831,6 +1058,36 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "similar" version = "2.6.0" @@ -865,6 +1122,12 @@ dependencies = [ "anstream 0.6.17", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "strip-ansi-escapes" version = "0.2.0" @@ -880,6 +1143,34 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "syn" version = "2.0.86" @@ -970,6 +1261,29 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "unicode-truncate" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf" +dependencies = [ + "itertools", + "unicode-segmentation", + "unicode-width 0.1.14", +] + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unicode-width" version = "0.2.0" diff --git a/README.md b/README.md index f86d6f62..81247f33 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ +# anstyle + This repo contains: + - [`anstream`](./crates/anstream) for a simple cross platform library for writing colored text to a terminal - [`anstyle`](./crates/anstyle) for style definitions - User-styling parsers @@ -11,6 +14,7 @@ This repo contains: - [`anstyle-ansi-term`](./crates/anstyle-ansi-term) for adapting `anstyle` to `ansi_term` - [`anstyle-crossterm`](./crates/anstyle-crossterm) for adapting `anstyle` to `crossterm` - [`anstyle-owo-colors`](./crates/anstyle-owo-colors) for adapting `anstyle` to `owo-colors` + - [`anstyle-ratatui`](./crates/anstyle-ratatui) for adapting `anstyle` to `ratatui` - [`anstyle-syntect`](./crates/anstyle-syntect) for adapting `anstyle` to `syntect` - [`anstyle-termcolor`](./crates/anstyle-termcolor) for adapting `anstyle` to `termcolor` - [`anstyle-yansi`](./crates/anstyle-yansi) for adapting `anstyle` to `yansi` diff --git a/crates/anstyle-ratatui/Cargo.toml b/crates/anstyle-ratatui/Cargo.toml new file mode 100644 index 00000000..e4737d5d --- /dev/null +++ b/crates/anstyle-ratatui/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "anstyle-ratatui" +version = "0.1.0" +description = "Adapt between ratatui and anstyle" +homepage = "https://github.com/rust-cli/anstyle" +categories = ["command-line-interface"] +keywords = ["ansi", "terminal", "color", "ratatui"] +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 = ["--cfg", "docsrs", "--generate-link-to-definition"] + +[package.metadata.release] +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 = "", replace = "\n## [Unreleased] - ReleaseDate\n", exactly = 1 }, + { file = "CHANGELOG.md", search = "", replace = "\n[Unreleased]: https://github.com/rust-cli/anstyle/compare/{{tag_name}}...HEAD", exactly = 1 }, +] + +[features] +default = ["underline-color"] +underline-color = ["ratatui/underline-color"] + +[dependencies] +anstyle = { version = "1.0.0", path = "../anstyle" } +ratatui = { version = "0.29.0", default-features = false } + +[lints] +workspace = true diff --git a/crates/anstyle-ratatui/LICENSE-APACHE b/crates/anstyle-ratatui/LICENSE-APACHE new file mode 100644 index 00000000..8f71f43f --- /dev/null +++ b/crates/anstyle-ratatui/LICENSE-APACHE @@ -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. + diff --git a/crates/anstyle-ratatui/LICENSE-MIT b/crates/anstyle-ratatui/LICENSE-MIT new file mode 100644 index 00000000..9dc20e2e --- /dev/null +++ b/crates/anstyle-ratatui/LICENSE-MIT @@ -0,0 +1,19 @@ +Copyright (c) 2022 The rust-cli Developers + +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. diff --git a/crates/anstyle-ratatui/README,md b/crates/anstyle-ratatui/README,md new file mode 100644 index 00000000..a4ec8055 --- /dev/null +++ b/crates/anstyle-ratatui/README,md @@ -0,0 +1,27 @@ +# anstyle-ratatui + +Convert between [`ratatui`](https://lib.rs/ratatui) and generic styling types + +[![Doc Badge]][Documentation] ![License Badge] [![Crate Badge]][Crate] + +## License + +Licensed under either of + +- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or + ) +- MIT license ([LICENSE-MIT](LICENSE-MIT) or ) + +at your option. + +## Contribution + +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. + +[Doc Badge]: https://img.shields.io/badge/docs-master-blue.svg +[Documentation]: https://docs.rs/anstyle-ratatui +[License Badge]: https://img.shields.io/crates/l/anstyle-ratatui.svg +[Crate Badge]: https://img.shields.io/crates/v/anstyle-ratatui.svg +[Crate]: https://crates.io/crates/anstyle-ratatui diff --git a/crates/anstyle-ratatui/src/lib.rs b/crates/anstyle-ratatui/src/lib.rs new file mode 100644 index 00000000..f27e91ac --- /dev/null +++ b/crates/anstyle-ratatui/src/lib.rs @@ -0,0 +1,88 @@ +//! Convert between [`ratatui`](https://lib.rs/ratatui) and [generic styling types][anstyle] + +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![warn(missing_docs)] +#![warn(clippy::print_stderr)] +#![warn(clippy::print_stdout)] + +/// Adapt generic styling to [`ratatui`] +pub fn to_ratatui(astyle: anstyle::Style) -> ratatui::style::Style { + let foreground_color = astyle.get_fg_color().map(to_ansi_color); + let background_color = astyle.get_bg_color().map(to_ansi_color); + let underline_color = astyle.get_underline_color().map(to_ansi_color); + + let mut modifiers = ratatui::style::Modifier::default(); + + let effects = astyle.get_effects(); + if effects.contains(anstyle::Effects::BOLD) { + modifiers.set(ratatui::style::Modifier::BOLD, true); + } + if effects.contains(anstyle::Effects::DIMMED) { + modifiers.set(ratatui::style::Modifier::DIM, true); + } + if effects.contains(anstyle::Effects::ITALIC) { + modifiers.set(ratatui::style::Modifier::ITALIC, true); + } + if effects.contains(anstyle::Effects::UNDERLINE) { + modifiers.set(ratatui::style::Modifier::UNDERLINED, true); + } + if effects.contains(anstyle::Effects::BLINK) { + modifiers.set(ratatui::style::Modifier::SLOW_BLINK, true); + } + if effects.contains(anstyle::Effects::INVERT) { + modifiers.set(ratatui::style::Modifier::REVERSED, true); + } + if effects.contains(anstyle::Effects::HIDDEN) { + modifiers.set(ratatui::style::Modifier::HIDDEN, true); + } + if effects.contains(anstyle::Effects::STRIKETHROUGH) { + modifiers.set(ratatui::style::Modifier::CROSSED_OUT, true); + } + + ratatui::style::Style { + fg: foreground_color, + bg: background_color, + #[cfg(feature = "underline-color")] + underline_color, + add_modifier: modifiers, + sub_modifier: ratatui::style::Modifier::default(), + } +} + +fn to_ansi_color(color: anstyle::Color) -> ratatui::style::Color { + match color { + anstyle::Color::Ansi(ansi) => ansi_to_ansi_color(ansi), + anstyle::Color::Ansi256(xterm) => xterm_to_ansi_color(xterm), + anstyle::Color::Rgb(rgb) => rgb_to_ansi_color(rgb), + } +} + +fn ansi_to_ansi_color(color: anstyle::AnsiColor) -> ratatui::style::Color { + match color { + anstyle::AnsiColor::Black => ratatui::style::Color::Black, + anstyle::AnsiColor::Red => ratatui::style::Color::Red, + anstyle::AnsiColor::Green => ratatui::style::Color::Green, + anstyle::AnsiColor::Yellow => ratatui::style::Color::Yellow, + anstyle::AnsiColor::Blue => ratatui::style::Color::Blue, + anstyle::AnsiColor::Magenta => ratatui::style::Color::Magenta, + anstyle::AnsiColor::Cyan => ratatui::style::Color::Cyan, + anstyle::AnsiColor::White => ratatui::style::Color::Gray, + anstyle::AnsiColor::BrightBlack => ratatui::style::Color::DarkGray, + anstyle::AnsiColor::BrightRed => ratatui::style::Color::LightRed, + anstyle::AnsiColor::BrightGreen => ratatui::style::Color::LightGreen, + anstyle::AnsiColor::BrightYellow => ratatui::style::Color::LightYellow, + anstyle::AnsiColor::BrightBlue => ratatui::style::Color::LightBlue, + anstyle::AnsiColor::BrightMagenta => ratatui::style::Color::LightMagenta, + anstyle::AnsiColor::BrightCyan => ratatui::style::Color::LightCyan, + anstyle::AnsiColor::BrightWhite => ratatui::style::Color::White, + } +} + +fn xterm_to_ansi_color(color: anstyle::Ansi256Color) -> ratatui::style::Color { + ratatui::style::Color::Indexed(color.index()) +} + +fn rgb_to_ansi_color(color: anstyle::RgbColor) -> ratatui::style::Color { + let anstyle::RgbColor(r, g, b) = color; + ratatui::style::Color::Rgb(r, g, b) +} diff --git a/typos.toml b/typos.toml index 8b09460d..54726ab0 100644 --- a/typos.toml +++ b/typos.toml @@ -4,3 +4,7 @@ check-file = false [type.svg] check-file = false + +[default.extend-words] +# until https://github.com/crate-ci/typos/issues/1055 is fixed +ratatui = "ratatui"