├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── Makefile ├── README.md ├── examples ├── basic.rs ├── kitchen_sink.rs └── test.png └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | /target 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "kimono" 3 | version = "0.1.3" 4 | authors = ["Richard Anaya"] 5 | edition = "2018" 6 | description = "A terminal style toolkit inspired by CSS for elegant TUIs" 7 | license = "MIT OR Apache-2.0" 8 | repository = "https://github.com/richardanaya/kimono" 9 | readme="README.md" 10 | 11 | [dependencies] 12 | ansi-escapes = "0.1.1" 13 | colored = "2.0.0" 14 | unicode-width = "0.1.9" 15 | 16 | [dev-dependencies] 17 | ukiyoe = "0" 18 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright (c) 2019 Richard Anaya 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright 2020 Richard Anaya 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | 9 | End license text. 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | cargo build 3 | lint: 4 | cargo fmt 5 | test: 6 | cargo test 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kimono 2 | 3 | docs.rs docs 4 | 5 | A terminal style toolkit inspired by CSS and [lipgloss](https://github.com/charmbracelet/lipgloss) for [truecolor 24-bit terminals](https://github.com/termstandard/colors#terminal-emulators). Made for elegant TUIs. 6 | 7 |

8 | Screen Shot 2022-07-30 at 10 57 29 AM 9 |

10 | 11 | Image terminal rendering done by our sister project [ukiyeo](https://github.com/richardanaya/ukiyoe/). 12 | 13 | ```terminal 14 | cargo add kimono 15 | ``` 16 | # Examples 17 | 18 | *Text can be styled.* 19 | 20 | ```rust 21 | use kimono::*; 22 | 23 | const STYLE: Style = Style::new().bold().color(0x91b984); 24 | 25 | fn main() { 26 | STYLE.render("こんにちは"); 27 | } 28 | ``` 29 | 30 | *Unicode text can be padded, bordered, and positioned.* 31 | 32 | Screen Shot 2022-07-23 at 5 01 28 PM 33 | 34 | ```rust 35 | use kimono::*; 36 | 37 | const STYLE: Style = Style::new() 38 | .padding_top(1) 39 | .padding_left(1) 40 | .padding_right(2) 41 | .padding_bottom(3) 42 | .border(1) 43 | .border_style(BORDER_STYLE_OUTLINE) 44 | .border_color(0xddae74) 45 | .border_background(0xbc5633) 46 | .color(0xebdeb8) 47 | .background(0x407955); 48 | 49 | fn main() { 50 | clear_screen(); 51 | STYLE.render_at_position(10, 3, "着物"); 52 | print!("\n\r"); 53 | } 54 | ``` 55 | 56 | *Text can be constrained to width and/or height.* 57 | 58 | Screen Shot 2022-07-23 at 9 17 45 PM 59 | 60 | ```rust 61 | use kimono::*; 62 | 63 | const STYLE: Style = Style::new() 64 | .padding(1) 65 | .color(0xfffd7c) 66 | .width(8) 67 | .background(0x956471); 68 | 69 | fn main() { 70 | clear_screen(); 71 | STYLE.render_at_position(10, 3, "abcdefghijklmno"); 72 | print!("\n\r"); 73 | } 74 | ``` 75 | 76 | *Text can be measured.* 77 | 78 | ```terminal 79 | (8, 5) 80 | ``` 81 | 82 | ```rust 83 | use kimono::*; 84 | 85 | const STYLE: Style = Style::new() 86 | .padding(1) 87 | .color(0xfffd7c) 88 | .width(8) 89 | .background(0x956471); 90 | 91 | fn main() { 92 | println!("{:?}", STYLE.measure("abcdefghijklmno")); 93 | } 94 | ``` 95 | 96 | *Borders have advanced styling.* 97 | 98 | Screen Shot 2022-07-24 at 1 31 08 PM 99 | 100 | ```rust 101 | use kimono::*; 102 | 103 | const STYLE: Style = Style::new() 104 | .padding(1) 105 | .color(0xe46281) 106 | .background(0xc50f47) 107 | .border(1) 108 | .italic() 109 | .border_style(BorderStyle { 110 | top_left: None, 111 | top: Some('•'), 112 | top_right: None, 113 | left: Some('•'), 114 | right: Some('•'), 115 | bottom_left: None, 116 | bottom: Some('•'), 117 | bottom_right: None, 118 | bold: true, 119 | italic: false, 120 | underline: false, 121 | strikethrough: false, 122 | }) 123 | .border_color(0xe5c7c9) 124 | .border_background(0x9e1d49); 125 | 126 | fn main() { 127 | clear_screen(); 128 | STYLE.render_at_position(10, 3, "The Tale of Genji by 紫 式部"); 129 | print!("\n\r"); 130 | } 131 | ``` 132 | 133 | *Text can be aligned.* 134 | 135 | Screen Shot 2022-07-30 at 2 10 38 PM 136 | 137 | ```rust 138 | use kimono::*; 139 | 140 | const STYLE: Style = Style::new() 141 | .padding(1) 142 | .color(0xb1a49a) 143 | .background(0xea664d) 144 | .border(1) 145 | .width(30) 146 | .text_align(TextAlign::Center); 147 | 148 | fn main() { 149 | clear_screen(); 150 | STYLE.render_at_position(10, 3, "sun egg mango"); 151 | print!("\n\r"); 152 | } 153 | ``` 154 | 155 | # Art 156 | 157 | Kimono patterns inspired from Unix terminals. 158 | 159 | ![1658677463667](https://user-images.githubusercontent.com/294042/180660263-281711c1-d29d-48f2-bee4-32b8f0658fd7.jpeg) 160 | 161 | ![1658677463603](https://user-images.githubusercontent.com/294042/180660265-1a82172d-1b89-4ab6-b653-b496f2396b24.jpeg) 162 | 163 | ![1658677461856](https://user-images.githubusercontent.com/294042/180660271-d5085b4d-bd4e-463c-acb5-5ed425b92403.jpeg) 164 | 165 | # License 166 | 167 | This project is licensed under either of 168 | 169 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or 170 | http://www.apache.org/licenses/LICENSE-2.0) 171 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or 172 | http://opensource.org/licenses/MIT) 173 | 174 | at your option. 175 | 176 | ### Contribution 177 | 178 | Unless you explicitly state otherwise, any contribution intentionally submitted 179 | for inclusion in `kimono` by you, as defined in the Apache-2.0 license, shall be 180 | dual licensed as above, without any additional terms or conditions. 181 | -------------------------------------------------------------------------------- /examples/basic.rs: -------------------------------------------------------------------------------- 1 | use kimono::*; 2 | 3 | const STYLE: Style = Style::new() 4 | .padding(1) 5 | .color(0xe46281) 6 | .background(0xc50f47) 7 | .border(1) 8 | .italic() 9 | .border_style(BorderStyle { 10 | top_left: None, 11 | top: Some('•'), 12 | top_right: None, 13 | left: Some('•'), 14 | right: Some('•'), 15 | bottom_left: None, 16 | bottom: Some('•'), 17 | bottom_right: None, 18 | bold: true, 19 | italic: false, 20 | underline: false, 21 | strikethrough: false, 22 | }) 23 | .border_color(0xe5c7c9) 24 | .border_background(0x9e1d49); 25 | 26 | fn main() { 27 | clear_screen(); 28 | STYLE.render_at_position(10, 3, "The Tale of Genji by 紫 式部"); 29 | print!("\n\r"); 30 | } 31 | -------------------------------------------------------------------------------- /examples/kitchen_sink.rs: -------------------------------------------------------------------------------- 1 | use kimono::*; 2 | use ukiyoe::Image; 3 | 4 | const TAB_SELECTED: Style = Style::new() 5 | .color(0x0d9211) 6 | .border(1) 7 | .italic() 8 | .border_style(BorderStyle { 9 | top_left: Some('╭'), 10 | top: Some('─'), 11 | top_right: Some('╮'), 12 | left: Some('│'), 13 | right: Some('│'), 14 | bottom_left: Some('╯'), 15 | bottom: Some(' '), 16 | bottom_right: Some('╰'), 17 | bold: true, 18 | italic: false, 19 | underline: false, 20 | strikethrough: false, 21 | }) 22 | .border_color(0xe5c7c9); 23 | 24 | const TAB_UNSELECTED: Style = TAB_SELECTED.border_style(BorderStyle { 25 | top_left: Some('╭'), 26 | top: Some('─'), 27 | top_right: Some('╮'), 28 | left: Some('│'), 29 | right: Some('│'), 30 | bottom_left: Some('┴'), 31 | bottom: Some('─'), 32 | bottom_right: Some('┴'), 33 | bold: true, 34 | italic: false, 35 | underline: false, 36 | strikethrough: false, 37 | }); 38 | 39 | const TAB_HOLDER: Style = TAB_SELECTED 40 | .border_style(BorderStyle { 41 | top_left: Some('╭'), 42 | top: None, 43 | top_right: Some('╮'), 44 | left: Some('│'), 45 | right: Some('│'), 46 | bottom_left: Some('╰'), 47 | bottom: Some('─'), 48 | bottom_right: Some('╯'), 49 | bold: true, 50 | italic: false, 51 | underline: false, 52 | strikethrough: false, 53 | }) 54 | .width(40) 55 | .color(0xffffff); 56 | 57 | const CONTAINER: Style = TAB_SELECTED 58 | .border_style(BorderStyle { 59 | top_left: Some('╭'), 60 | top: Some('─'), 61 | top_right: Some('╮'), 62 | left: Some('│'), 63 | right: Some('│'), 64 | bottom_left: Some('╰'), 65 | bottom: Some('─'), 66 | bottom_right: Some('╯'), 67 | bold: true, 68 | italic: false, 69 | underline: false, 70 | strikethrough: false, 71 | }) 72 | .width(40) 73 | .color(0xffffff); 74 | 75 | const FANCY: Style = Style::new().background(0xdcb97e).color(0x0d9211); 76 | const FANCY1: Style = Style::new().background(0xfff1ce).color(0x0d9211); 77 | const FANCY2: Style = Style::new().background(0xfe3900).color(0x0d9211); 78 | const FANCY3: Style = Style::new().background(0x657374).color(0x0d9211); 79 | 80 | const FANCY4: Style = Style::new().color(0x0d9211); 81 | 82 | const WHITE: Style = Style::new().color(0xdcdcdc); 83 | const SEPARATOR: Style = Style::new().color(0x5f6563); 84 | 85 | fn main() { 86 | clear_screen(); 87 | TAB_HOLDER.render_at_position(0, 2, "Yōkai (妖怪, \"strange apparition\") are a class of supernatural entities and spirits in Japanese folklore. The word yōkai is composed of the kanji for \"attractive; calamity\" and \"apparition; mystery; suspicious.\"[1][2] Yōkai are also referred to as ayakashi (あやかし), mononoke (物の怪) or mamono (魔物). Yokai are not literally demons in the Western sense of the word, but are instead spirits and entities, whose behaviour can range from malevolent or mischievous to friendly, fortuitous, or helpful to humans."); 88 | TAB_SELECTED.render_at_position(1, 0, "Kimono"); 89 | TAB_UNSELECTED.render_at_position(9, 0, "Shoji"); 90 | TAB_UNSELECTED.render_at_position(16, 0, "Mochi"); 91 | TAB_UNSELECTED.render_at_position(23, 0, "Ramen"); 92 | TAB_UNSELECTED.render_at_position(30, 0, "Samurai"); 93 | 94 | let mut image = Image::new("examples/test.png"); 95 | image.render_at_position(40, 0, 60, 18); 96 | 97 | FANCY.render_at_position(65, 8, "KIMONO"); 98 | FANCY1.render_at_position(67, 9, "KIMONO"); 99 | FANCY2.render_at_position(69, 10, "KIMONO"); 100 | FANCY3.render_at_position(71, 11, "KIMONO"); 101 | 102 | WHITE.render_at_position(45, 21, "A terminal style library for elegant TUIs"); 103 | FANCY4.render_at_position(45, 22, "https://github.com/richardanaya/kimono"); 104 | 105 | CONTAINER.width(100).render_at_position( 106 | 0, 107 | 18, 108 | "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░", 109 | ); 110 | print!("\n\r"); 111 | use std::io::{stdin, stdout, Write}; 112 | let mut s = String::new(); 113 | print!("Please enter the name you seek: "); 114 | let _ = stdout().flush(); 115 | stdin() 116 | .read_line(&mut s) 117 | .expect("Did not enter a correct string"); 118 | if let Some('\n') = s.chars().next_back() { 119 | s.pop(); 120 | } 121 | if let Some('\r') = s.chars().next_back() { 122 | s.pop(); 123 | } 124 | println!("You typed: {}", s); 125 | } 126 | -------------------------------------------------------------------------------- /examples/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/richardanaya/kimono/2e8b42fd5d20906938b66bb4358f86fe28e79866/examples/test.png -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | pub use ansi_escapes::*; 2 | use colored::ColoredString; 3 | use colored::Colorize; 4 | use unicode_width::UnicodeWidthStr; 5 | 6 | type Color = (u8, u8, u8); 7 | struct Quad { 8 | top: usize, 9 | left: usize, 10 | right: usize, 11 | bottom: usize, 12 | } 13 | 14 | #[derive(Default)] 15 | pub struct BorderStyle { 16 | pub top_left: Option, 17 | pub top: Option, 18 | pub top_right: Option, 19 | pub left: Option, 20 | pub right: Option, 21 | pub bottom_left: Option, 22 | pub bottom: Option, 23 | pub bottom_right: Option, 24 | pub bold: bool, 25 | pub italic: bool, 26 | pub underline: bool, 27 | pub strikethrough: bool, 28 | } 29 | 30 | pub const BORDER_STYLE_DEFAULT: BorderStyle = BorderStyle { 31 | top_left: Some(' '), 32 | top: Some(' '), 33 | top_right: Some(' '), 34 | left: Some(' '), 35 | right: Some(' '), 36 | bottom_left: Some(' '), 37 | bottom: Some(' '), 38 | bottom_right: Some(' '), 39 | bold: false, 40 | italic: false, 41 | underline: false, 42 | strikethrough: false, 43 | }; 44 | 45 | pub const BORDER_STYLE_OUTLINE: BorderStyle = BorderStyle { 46 | top_left: Some('┌'), 47 | top: Some('─'), 48 | top_right: Some('┐'), 49 | left: Some('│'), 50 | right: Some('│'), 51 | bottom_left: Some('└'), 52 | bottom: Some('─'), 53 | bottom_right: Some('┘'), 54 | bold: false, 55 | italic: false, 56 | underline: false, 57 | strikethrough: false, 58 | }; 59 | 60 | pub const BORDER_STYLE_THICK_OUTLINE: BorderStyle = BorderStyle { 61 | top_left: Some('┌'), 62 | top: Some('─'), 63 | top_right: Some('┐'), 64 | left: Some('│'), 65 | right: Some('│'), 66 | bottom_left: Some('└'), 67 | bottom: Some('─'), 68 | bottom_right: Some('┘'), 69 | bold: true, 70 | italic: false, 71 | underline: false, 72 | strikethrough: false, 73 | }; 74 | 75 | #[derive(PartialEq)] 76 | pub enum TextAlign { 77 | Left, 78 | Right, 79 | Center, 80 | } 81 | 82 | pub struct Style { 83 | color: Option, 84 | background: Option, 85 | padding: Quad, 86 | border: Quad, 87 | border_color: Option, 88 | border_background: Option, 89 | width: Option, 90 | height: Option, 91 | border_style: Option, 92 | bold: bool, 93 | italic: bool, 94 | underline: bool, 95 | strikethrough: bool, 96 | text_align: TextAlign, 97 | } 98 | 99 | fn style_str( 100 | text: &str, 101 | color: Option, 102 | background: Option, 103 | bold: bool, 104 | italic: bool, 105 | underline: bool, 106 | strikethrough: bool, 107 | ) -> ColoredString { 108 | let mut result: ColoredString = text.into(); 109 | if let Some(color) = color { 110 | result = result.truecolor(color.0, color.1, color.2); 111 | } 112 | if let Some(background) = background { 113 | result = result.on_truecolor(background.0, background.1, background.2); 114 | } 115 | if bold { 116 | result = result.bold(); 117 | } 118 | if italic { 119 | result = result.italic(); 120 | } 121 | if underline { 122 | result = result.underline(); 123 | } 124 | if strikethrough { 125 | result = result.strikethrough(); 126 | } 127 | result 128 | } 129 | 130 | fn create_string_with_char(char: char, length: usize) -> String { 131 | let mut s = String::new(); 132 | for _ in 0..length { 133 | s.push(char); 134 | } 135 | s 136 | } 137 | 138 | fn get_unicode_length(s: &str) -> usize { 139 | UnicodeWidthStr::width(s) 140 | } 141 | 142 | impl Style { 143 | pub const fn new() -> Style { 144 | Style { 145 | background: None, 146 | color: None, 147 | padding: Quad { 148 | left: 0, 149 | top: 0, 150 | right: 0, 151 | bottom: 0, 152 | }, 153 | border: Quad { 154 | left: 0, 155 | top: 0, 156 | right: 0, 157 | bottom: 0, 158 | }, 159 | border_color: None, 160 | border_background: None, 161 | width: None, 162 | height: None, 163 | border_style: None, 164 | bold: false, 165 | italic: false, 166 | underline: false, 167 | strikethrough: false, 168 | text_align: TextAlign::Left, 169 | } 170 | } 171 | 172 | pub const fn border_style(mut self, border_style: BorderStyle) -> Style { 173 | self.border_style = Some(border_style); 174 | self 175 | } 176 | 177 | pub const fn text_align(mut self, text_align: TextAlign) -> Style { 178 | self.text_align = text_align; 179 | self 180 | } 181 | 182 | pub const fn padding(mut self, padding: usize) -> Self { 183 | self.padding = Quad { 184 | left: padding, 185 | top: padding, 186 | right: padding, 187 | bottom: padding, 188 | }; 189 | self 190 | } 191 | 192 | pub const fn padding_left(mut self, padding: usize) -> Self { 193 | self.padding.left = padding; 194 | self 195 | } 196 | 197 | pub const fn padding_top(mut self, padding: usize) -> Self { 198 | self.padding.top = padding; 199 | self 200 | } 201 | 202 | pub const fn padding_right(mut self, padding: usize) -> Self { 203 | self.padding.right = padding; 204 | self 205 | } 206 | 207 | pub const fn padding_bottom(mut self, padding: usize) -> Self { 208 | self.padding.bottom = padding; 209 | self 210 | } 211 | 212 | pub const fn color(mut self, color: u32) -> Self { 213 | self.color = Some(((color >> 16) as u8, (color >> 8) as u8, color as u8)); 214 | self 215 | } 216 | 217 | pub const fn background(mut self, color: u32) -> Self { 218 | self.background = Some(((color >> 16) as u8, (color >> 8) as u8, color as u8)); 219 | self 220 | } 221 | 222 | pub const fn border_color(mut self, color: u32) -> Self { 223 | self.border_color = Some(((color >> 16) as u8, (color >> 8) as u8, color as u8)); 224 | self 225 | } 226 | 227 | pub const fn border_background(mut self, color: u32) -> Self { 228 | self.border_background = Some(((color >> 16) as u8, (color >> 8) as u8, color as u8)); 229 | self 230 | } 231 | 232 | pub const fn border(mut self, border: usize) -> Self { 233 | self.border = Quad { 234 | left: border, 235 | top: border, 236 | right: border, 237 | bottom: border, 238 | }; 239 | self 240 | } 241 | 242 | pub const fn border_left(mut self, left: usize) -> Self { 243 | self.border.left = left; 244 | self 245 | } 246 | 247 | pub const fn border_top(mut self, top: usize) -> Self { 248 | self.border.top = top; 249 | self 250 | } 251 | 252 | pub const fn border_right(mut self, right: usize) -> Self { 253 | self.border.right = right; 254 | self 255 | } 256 | 257 | pub const fn border_bottom(mut self, bottom: usize) -> Self { 258 | self.border.bottom = bottom; 259 | self 260 | } 261 | 262 | pub const fn width(mut self, width: usize) -> Self { 263 | self.width = Some(width); 264 | self 265 | } 266 | 267 | pub const fn height(mut self, height: usize) -> Self { 268 | self.height = Some(height); 269 | self 270 | } 271 | 272 | fn prepare_text(&self, text: &str) -> (Vec, usize) { 273 | let mut text: Vec = text 274 | .replace("\t", " ") 275 | .lines() 276 | .map(|line| line.trim().to_string()) 277 | .collect(); 278 | if let Some(w) = self.width { 279 | let mut max_content_width = 280 | w - self.padding.left - self.padding.right - self.border.left - self.border.right; 281 | max_content_width = max_content_width.max(0); 282 | if max_content_width == 0 { 283 | text = vec![]; 284 | } else { 285 | let mut new_lines = vec![]; 286 | { 287 | for line in text.iter_mut() { 288 | let line_width = get_unicode_length(line); 289 | if line_width > max_content_width { 290 | let mut new_line = String::new(); 291 | let mut line_width = 0; 292 | for c in line.chars() { 293 | let c_width = get_unicode_length(&c.to_string()); 294 | if line_width + c_width > max_content_width { 295 | new_lines.push(new_line); 296 | new_line = String::new(); 297 | line_width = 0; 298 | } 299 | new_line.push(c); 300 | line_width += c_width; 301 | } 302 | new_lines.push(new_line); 303 | break; 304 | } else { 305 | new_lines.push(line.clone()); 306 | } 307 | } 308 | } 309 | text = new_lines; 310 | } 311 | } 312 | if let Some(h) = self.height { 313 | let mut max_content_height = 314 | h - self.border.top - self.border.bottom - self.padding.top - self.padding.bottom; 315 | max_content_height = max_content_height.max(0); 316 | text = text.iter().take(max_content_height).cloned().collect(); 317 | } 318 | let mut max_len = text 319 | .iter() 320 | .fold(0, |max, line| max.max(get_unicode_length(line))); 321 | if let Some(w) = self.width { 322 | max_len = 323 | w - self.border.left - self.border.right - self.padding.left - self.padding.right; 324 | } 325 | (text, max_len) 326 | } 327 | 328 | pub fn render(&self, text: &str) { 329 | let (text, max_len) = self.prepare_text(text); 330 | let width = max_len + self.padding.left + self.padding.right; 331 | let full_width = width + self.border.left + self.border.right; 332 | let top = style_str( 333 | &create_string_with_char(' ', width), 334 | None, 335 | self.background, 336 | false, 337 | false, 338 | false, 339 | false, 340 | ); 341 | let bottom = style_str( 342 | &create_string_with_char(' ', width), 343 | None, 344 | self.background, 345 | false, 346 | false, 347 | false, 348 | false, 349 | ); 350 | let left = style_str( 351 | &create_string_with_char(' ', self.padding.left), 352 | None, 353 | self.background, 354 | false, 355 | false, 356 | false, 357 | false, 358 | ); 359 | let right = style_str( 360 | &create_string_with_char(' ', self.padding.right), 361 | None, 362 | self.background, 363 | false, 364 | false, 365 | false, 366 | false, 367 | ); 368 | 369 | let current_border_style = self.border_style.as_ref().unwrap_or(&BORDER_STYLE_DEFAULT); 370 | 371 | let border_top = if let Some(b) = current_border_style.top { 372 | style_str( 373 | &create_string_with_char(b, width), 374 | self.border_color, 375 | self.border_background, 376 | current_border_style.bold, 377 | current_border_style.italic, 378 | current_border_style.underline, 379 | current_border_style.strikethrough, 380 | ) 381 | } else { 382 | let s: &str = &format!("{}", CursorMove::X(width as i16)); 383 | s.into() 384 | }; 385 | let border_bottom = if let Some(b) = current_border_style.bottom { 386 | style_str( 387 | &create_string_with_char(b, width), 388 | self.border_color, 389 | self.border_background, 390 | current_border_style.bold, 391 | current_border_style.italic, 392 | current_border_style.underline, 393 | current_border_style.strikethrough, 394 | ) 395 | } else { 396 | let s: &str = &format!("{}", CursorMove::X(width as i16)); 397 | s.into() 398 | }; 399 | let border_left = if let Some(b) = current_border_style.left { 400 | style_str( 401 | &create_string_with_char(b, self.border.left), 402 | self.border_color, 403 | self.border_background, 404 | current_border_style.bold, 405 | current_border_style.italic, 406 | current_border_style.underline, 407 | current_border_style.strikethrough, 408 | ) 409 | } else { 410 | let s: &str = &format!("{}", CursorMove::X(self.border.left as i16)); 411 | s.into() 412 | }; 413 | let border_right = if let Some(b) = current_border_style.right { 414 | style_str( 415 | &create_string_with_char(b, self.border.right), 416 | self.border_color, 417 | self.border_background, 418 | current_border_style.bold, 419 | current_border_style.italic, 420 | current_border_style.underline, 421 | current_border_style.strikethrough, 422 | ) 423 | } else { 424 | let s: &str = &format!("{}", CursorMove::X(self.border.right as i16)); 425 | s.into() 426 | }; 427 | let border_top_left = if let Some(b) = current_border_style.top_left { 428 | style_str( 429 | &create_string_with_char(b, self.border.left), 430 | self.border_color, 431 | self.border_background, 432 | current_border_style.bold, 433 | current_border_style.italic, 434 | current_border_style.underline, 435 | current_border_style.strikethrough, 436 | ) 437 | } else { 438 | let s: &str = &format!("{}", CursorMove::X(self.border.left as i16)); 439 | s.into() 440 | }; 441 | 442 | let border_top_right = if let Some(b) = current_border_style.top_right { 443 | style_str( 444 | &create_string_with_char(b, self.border.right), 445 | self.border_color, 446 | self.border_background, 447 | current_border_style.bold, 448 | current_border_style.italic, 449 | current_border_style.underline, 450 | current_border_style.strikethrough, 451 | ) 452 | } else { 453 | let s: &str = &format!("{}", CursorMove::X(self.border.right as i16)); 454 | s.into() 455 | }; 456 | 457 | let border_bottom_left = if let Some(b) = current_border_style.bottom_left { 458 | style_str( 459 | &create_string_with_char(b, self.border.left), 460 | self.border_color, 461 | self.border_background, 462 | current_border_style.bold, 463 | current_border_style.italic, 464 | current_border_style.underline, 465 | current_border_style.strikethrough, 466 | ) 467 | } else { 468 | let s: &str = &format!("{}", CursorMove::X(self.border.left as i16)); 469 | s.into() 470 | }; 471 | 472 | let border_bottom_right = if let Some(b) = current_border_style.bottom_right { 473 | style_str( 474 | &create_string_with_char(b, self.border.right), 475 | self.border_color, 476 | self.border_background, 477 | current_border_style.bold, 478 | current_border_style.italic, 479 | current_border_style.underline, 480 | current_border_style.strikethrough, 481 | ) 482 | } else { 483 | let s: &str = &format!("{}", CursorMove::X(self.border.right as i16)); 484 | s.into() 485 | }; 486 | 487 | // top 488 | for _ in 0..self.border.top { 489 | print!("{}", border_top_left); 490 | print!("{}", border_top); 491 | print!( 492 | "{}{}", 493 | border_top_right, 494 | CursorMove::XY(-(full_width as i16), 1) 495 | ); 496 | } 497 | for _ in 0..self.padding.top { 498 | print!("{}", border_left); 499 | print!("{}", top); 500 | print!( 501 | "{}{}", 502 | border_right, 503 | CursorMove::XY(-(full_width as i16), 1) 504 | ); 505 | } 506 | 507 | // content 508 | for line in text.iter() { 509 | print!("{}", border_left); 510 | print!("{}", left); 511 | let padding_len = max_len - get_unicode_length(line); 512 | let mut right_padding = create_string_with_char(' ', padding_len); 513 | let mut left_padding: String = String::new(); 514 | if self.text_align == TextAlign::Center { 515 | let left_padding_len = (padding_len / 2) as usize; 516 | let right_padding_len = padding_len - left_padding_len; 517 | left_padding = create_string_with_char(' ', left_padding_len); 518 | right_padding = create_string_with_char(' ', right_padding_len); 519 | } else if self.text_align == TextAlign::Right { 520 | left_padding = create_string_with_char(' ', padding_len); 521 | right_padding = String::new(); 522 | } 523 | print!( 524 | "{}{}{}", 525 | style_str( 526 | &left_padding, 527 | self.color, 528 | self.background, 529 | false, 530 | false, 531 | false, 532 | false 533 | ), 534 | style_str( 535 | &line, 536 | self.color, 537 | self.background, 538 | self.bold, 539 | self.italic, 540 | self.underline, 541 | self.strikethrough 542 | ), 543 | style_str( 544 | &right_padding, 545 | self.color, 546 | self.background, 547 | false, 548 | false, 549 | false, 550 | false 551 | ) 552 | ); 553 | print!("{}", right,); 554 | print!( 555 | "{}{}", 556 | border_right, 557 | CursorMove::XY(-(full_width as i16), 1) 558 | ); 559 | } 560 | 561 | if let Some(h) = self.height { 562 | let max_content_height = 563 | (h - self.border.top - self.padding.top - self.padding.bottom).max(0); 564 | let excess_lines = (max_content_height - text.len()).max(0); 565 | 566 | let padding = create_string_with_char(' ', max_len); 567 | for _ in 0..excess_lines { 568 | print!("{}", border_left); 569 | print!("{}", left); 570 | print!( 571 | "{}", 572 | style_str( 573 | &padding, 574 | self.color, 575 | self.background, 576 | false, 577 | false, 578 | false, 579 | false 580 | ) 581 | ); 582 | print!("{}", right,); 583 | print!( 584 | "{}{}", 585 | border_right, 586 | CursorMove::XY(-(full_width as i16), 1) 587 | ); 588 | } 589 | } 590 | 591 | // bottom 592 | for _ in 0..self.padding.bottom { 593 | print!("{}", border_left); 594 | print!("{}", bottom); 595 | print!( 596 | "{}{}", 597 | border_right, 598 | CursorMove::XY(-(full_width as i16), 1) 599 | ); 600 | } 601 | for _ in 0..self.border.bottom { 602 | print!("{}", border_bottom_left); 603 | print!("{}", border_bottom); 604 | print!( 605 | "{}{}", 606 | border_bottom_right, 607 | CursorMove::XY(-(full_width as i16), 1) 608 | ); 609 | } 610 | } 611 | 612 | pub fn render_at_position(&self, x: u16, y: u16, text: &str) { 613 | print!("{}", CursorTo::AbsoluteXY(x, y)); 614 | self.render(text) 615 | } 616 | 617 | pub fn measure(&self, text: &str) -> (usize, usize) { 618 | let (text, max_width) = self.prepare_text(text); 619 | let cols = max_width 620 | + self.padding.left 621 | + self.padding.right 622 | + self.border.left 623 | + self.border.right; 624 | let rows = text.len() 625 | + self.padding.top 626 | + self.padding.bottom 627 | + self.border.top 628 | + self.border.bottom; 629 | (cols, rows) 630 | } 631 | 632 | pub const fn bold(mut self) -> Self { 633 | self.bold = true; 634 | self 635 | } 636 | 637 | pub const fn italic(mut self) -> Self { 638 | self.italic = true; 639 | self 640 | } 641 | 642 | pub const fn underline(mut self) -> Self { 643 | self.underline = true; 644 | self 645 | } 646 | 647 | pub const fn strikethrough(mut self) -> Self { 648 | self.strikethrough = true; 649 | self 650 | } 651 | } 652 | 653 | pub fn clear_screen() { 654 | print!("{}", CursorMove::XY(0, 0)); 655 | print!("{}", EraseScreen); 656 | } 657 | --------------------------------------------------------------------------------