├── .github ├── package └── workflows │ ├── release.yaml │ └── test.yaml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── Justfile ├── LICENSE.md ├── README.md ├── src └── main.rs └── usage.gif /.github/package: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euxo pipefail 4 | 5 | 6 | VERSION=${REF#"refs/tags/"} 7 | DIST=`pwd`/dist 8 | # This var can be modified if need be. 9 | BIN=${GITHUB_REPOSITORY##*/} 10 | 11 | echo "Packaging $BIN $VERSION for $TARGET..." 12 | 13 | test -f Cargo.lock || cargo generate-lockfile 14 | 15 | echo "Building $BIN..." 16 | RUSTFLAGS="--deny warnings --codegen target-feature=+crt-static $TARGET_RUSTFLAGS" \ 17 | cargo build --bin $BIN --target $TARGET --release 18 | EXECUTABLE=target/$TARGET/release/$BIN 19 | 20 | if [[ $OS == windows-2016 ]]; then 21 | EXECUTABLE=$EXECUTABLE.exe 22 | fi 23 | 24 | echo "Copying release files..." 25 | mkdir dist 26 | cp \ 27 | $EXECUTABLE \ 28 | Cargo.lock \ 29 | Cargo.toml \ 30 | LICENSE.md \ 31 | README.md \ 32 | $DIST 33 | 34 | cd $DIST 35 | echo "Creating release archive..." 36 | case $OS in 37 | ubuntu-latest | macos-latest) 38 | ARCHIVE=$DIST/$BIN-$VERSION-$TARGET.tar.gz 39 | tar czf $ARCHIVE * 40 | echo "::set-output name=archive::$ARCHIVE" 41 | ;; 42 | windows-2016) 43 | ARCHIVE=$DIST/$BIN-$VERSION-$TARGET.zip 44 | 7z a $ARCHIVE * 45 | echo "::set-output name=archive::`pwd -W`/$BIN-$VERSION-$TARGET.zip" 46 | ;; 47 | esac 48 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | defaults: 9 | run: 10 | shell: bash 11 | 12 | jobs: 13 | all: 14 | name: All 15 | 16 | strategy: 17 | matrix: 18 | target: 19 | - aarch64-unknown-linux-musl 20 | - armv7-unknown-linux-musleabihf 21 | - x86_64-apple-darwin 22 | - x86_64-pc-windows-msvc 23 | - x86_64-unknown-linux-musl 24 | include: 25 | - target: aarch64-unknown-linux-musl 26 | os: ubuntu-latest 27 | native: false 28 | target_rustflags: '--codegen linker=aarch64-linux-gnu-gcc' 29 | - target: armv7-unknown-linux-musleabihf 30 | os: ubuntu-latest 31 | native: false 32 | target_rustflags: '--codegen linker=arm-linux-gnueabihf-gcc' 33 | - target: x86_64-apple-darwin 34 | os: macos-latest 35 | native: true 36 | target_rustflags: '' 37 | - target: x86_64-pc-windows-msvc 38 | os: windows-2016 39 | native: true 40 | target_rustflags: '' 41 | - target: x86_64-unknown-linux-musl 42 | os: ubuntu-latest 43 | native: true 44 | target_rustflags: '' 45 | 46 | runs-on: ${{matrix.os}} 47 | 48 | steps: 49 | - uses: actions/checkout@v2 50 | 51 | - name: Install Rust Toolchain Components 52 | uses: actions-rs/toolchain@v1 53 | with: 54 | override: true 55 | target: ${{ matrix.target }} 56 | toolchain: stable 57 | 58 | - uses: Swatinem/rust-cache@v1 59 | 60 | - name: Install AArch64 Toolchain 61 | if: ${{ matrix.target == 'aarch64-unknown-linux-musl' }} 62 | run: | 63 | sudo apt-get update 64 | sudo apt-get install gcc-aarch64-linux-gnu 65 | - name: Install ARM7 Toolchain 66 | if: ${{ matrix.target == 'armv7-unknown-linux-musleabihf' }} 67 | run: | 68 | sudo apt-get update 69 | sudo apt-get install gcc-arm-linux-gnueabihf 70 | - name: Test 71 | if: matrix.native 72 | run: cargo test --all --target ${{ matrix.target }} 73 | 74 | - name: Package 75 | id: package 76 | env: 77 | TARGET: ${{ matrix.target }} 78 | REF: ${{ github.ref }} 79 | OS: ${{ matrix.os }} 80 | TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }} 81 | run: ./.github/package 82 | shell: bash 83 | 84 | - name: Publish Archive 85 | uses: softprops/action-gh-release@v0.1.5 86 | if: ${{ startsWith(github.ref, 'refs/tags/') }} 87 | with: 88 | draft: false 89 | files: ${{ steps.package.outputs.archive }} 90 | prerelease: ${{ steps.ref-type.outputs.value != 'release' }} 91 | env: 92 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 93 | 94 | - name: Publish Changelog 95 | uses: softprops/action-gh-release@v0.1.5 96 | if: >- 97 | ${{ 98 | startsWith(github.ref, 'refs/tags/') 99 | && matrix.target == 'x86_64-unknown-linux-musl' 100 | }} 101 | with: 102 | draft: false 103 | files: CHANGELOG.md 104 | prerelease: ${{ steps.ref-type.outputs.value != 'release' }} 105 | env: 106 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 107 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | 12 | defaults: 13 | run: 14 | shell: bash 15 | 16 | 17 | jobs: 18 | run-test: 19 | permissions: 20 | contents: read 21 | runs-on: ubuntu-latest 22 | 23 | steps: 24 | - uses: actions/checkout@v2 25 | - uses: actions-rs/toolchain@v1 26 | with: 27 | toolchain: stable 28 | - uses: Swatinem/rust-cache@v1 29 | - run: cargo install just 30 | - run: just test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | dist/ 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "atty" 7 | version = "0.2.14" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 10 | dependencies = [ 11 | "hermit-abi", 12 | "libc", 13 | "winapi", 14 | ] 15 | 16 | [[package]] 17 | name = "autocfg" 18 | version = "1.0.1" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 21 | 22 | [[package]] 23 | name = "bitflags" 24 | version = "1.3.2" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 27 | 28 | [[package]] 29 | name = "clap" 30 | version = "3.0.0-beta.4" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "fcd70aa5597dbc42f7217a543f9ef2768b2ef823ba29036072d30e1d88e98406" 33 | dependencies = [ 34 | "atty", 35 | "bitflags", 36 | "clap_derive", 37 | "indexmap", 38 | "lazy_static", 39 | "os_str_bytes", 40 | "strsim", 41 | "termcolor", 42 | "textwrap", 43 | "vec_map", 44 | ] 45 | 46 | [[package]] 47 | name = "clap_derive" 48 | version = "3.0.0-rc.1" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "0152ba3ee01fa5a9133d4e15a1d9659c75d2270365768dd5a880cc7e68871874" 51 | dependencies = [ 52 | "heck", 53 | "proc-macro-error", 54 | "proc-macro2", 55 | "quote", 56 | "syn", 57 | ] 58 | 59 | [[package]] 60 | name = "colored" 61 | version = "2.0.0" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" 64 | dependencies = [ 65 | "atty", 66 | "lazy_static", 67 | "winapi", 68 | ] 69 | 70 | [[package]] 71 | name = "dye-cli" 72 | version = "0.4.2" 73 | dependencies = [ 74 | "clap", 75 | "colored", 76 | ] 77 | 78 | [[package]] 79 | name = "hashbrown" 80 | version = "0.11.2" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" 83 | 84 | [[package]] 85 | name = "heck" 86 | version = "0.3.3" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" 89 | dependencies = [ 90 | "unicode-segmentation", 91 | ] 92 | 93 | [[package]] 94 | name = "hermit-abi" 95 | version = "0.1.19" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 98 | dependencies = [ 99 | "libc", 100 | ] 101 | 102 | [[package]] 103 | name = "indexmap" 104 | version = "1.7.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" 107 | dependencies = [ 108 | "autocfg", 109 | "hashbrown", 110 | ] 111 | 112 | [[package]] 113 | name = "lazy_static" 114 | version = "1.4.0" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 117 | 118 | [[package]] 119 | name = "libc" 120 | version = "0.2.109" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01" 123 | 124 | [[package]] 125 | name = "os_str_bytes" 126 | version = "3.1.0" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d" 129 | 130 | [[package]] 131 | name = "proc-macro-error" 132 | version = "1.0.4" 133 | source = "registry+https://github.com/rust-lang/crates.io-index" 134 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 135 | dependencies = [ 136 | "proc-macro-error-attr", 137 | "proc-macro2", 138 | "quote", 139 | "syn", 140 | "version_check", 141 | ] 142 | 143 | [[package]] 144 | name = "proc-macro-error-attr" 145 | version = "1.0.4" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 148 | dependencies = [ 149 | "proc-macro2", 150 | "quote", 151 | "version_check", 152 | ] 153 | 154 | [[package]] 155 | name = "proc-macro2" 156 | version = "1.0.33" 157 | source = "registry+https://github.com/rust-lang/crates.io-index" 158 | checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a" 159 | dependencies = [ 160 | "unicode-xid", 161 | ] 162 | 163 | [[package]] 164 | name = "quote" 165 | version = "1.0.10" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" 168 | dependencies = [ 169 | "proc-macro2", 170 | ] 171 | 172 | [[package]] 173 | name = "strsim" 174 | version = "0.10.0" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 177 | 178 | [[package]] 179 | name = "syn" 180 | version = "1.0.82" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" 183 | dependencies = [ 184 | "proc-macro2", 185 | "quote", 186 | "unicode-xid", 187 | ] 188 | 189 | [[package]] 190 | name = "termcolor" 191 | version = "1.1.2" 192 | source = "registry+https://github.com/rust-lang/crates.io-index" 193 | checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" 194 | dependencies = [ 195 | "winapi-util", 196 | ] 197 | 198 | [[package]] 199 | name = "textwrap" 200 | version = "0.14.2" 201 | source = "registry+https://github.com/rust-lang/crates.io-index" 202 | checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" 203 | dependencies = [ 204 | "unicode-width", 205 | ] 206 | 207 | [[package]] 208 | name = "unicode-segmentation" 209 | version = "1.8.0" 210 | source = "registry+https://github.com/rust-lang/crates.io-index" 211 | checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" 212 | 213 | [[package]] 214 | name = "unicode-width" 215 | version = "0.1.9" 216 | source = "registry+https://github.com/rust-lang/crates.io-index" 217 | checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" 218 | 219 | [[package]] 220 | name = "unicode-xid" 221 | version = "0.2.2" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 224 | 225 | [[package]] 226 | name = "vec_map" 227 | version = "0.8.2" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" 230 | 231 | [[package]] 232 | name = "version_check" 233 | version = "0.9.3" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" 236 | 237 | [[package]] 238 | name = "winapi" 239 | version = "0.3.9" 240 | source = "registry+https://github.com/rust-lang/crates.io-index" 241 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 242 | dependencies = [ 243 | "winapi-i686-pc-windows-gnu", 244 | "winapi-x86_64-pc-windows-gnu", 245 | ] 246 | 247 | [[package]] 248 | name = "winapi-i686-pc-windows-gnu" 249 | version = "0.4.0" 250 | source = "registry+https://github.com/rust-lang/crates.io-index" 251 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 252 | 253 | [[package]] 254 | name = "winapi-util" 255 | version = "0.1.5" 256 | source = "registry+https://github.com/rust-lang/crates.io-index" 257 | checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" 258 | dependencies = [ 259 | "winapi", 260 | ] 261 | 262 | [[package]] 263 | name = "winapi-x86_64-pc-windows-gnu" 264 | version = "0.4.0" 265 | source = "registry+https://github.com/rust-lang/crates.io-index" 266 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 267 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "dye-cli" 3 | version = "0.4.2" 4 | edition = "2021" 5 | authors = ["Kurt Wolf "] 6 | description = "A tool to color shell text." 7 | license = "MIT" 8 | repository = "https://github.com/kurtbuilds/dye" 9 | 10 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 11 | 12 | [[bin]] 13 | name = "dye" 14 | path = "src/main.rs" 15 | 16 | [dependencies] 17 | clap = { version = "=3.0.0-beta.4", features = ["color", "env", "suggestions"]} 18 | colored = "2.0.0" 19 | -------------------------------------------------------------------------------- /Justfile: -------------------------------------------------------------------------------- 1 | set dotenv-load := false 2 | 3 | help: 4 | @just --list --unsorted 5 | 6 | build: 7 | cargo build 8 | alias b := build 9 | 10 | run *args: 11 | cargo run {{ args }} 12 | alias r := run 13 | 14 | release: 15 | cargo build --release 16 | 17 | install: 18 | cargo install --path . 19 | 20 | bootstrap: 21 | cargo install cargo-edit 22 | 23 | test *args: 24 | cargo test {{ args }} 25 | 26 | check: 27 | cargo check 28 | alias c := check 29 | 30 | fix: 31 | cargo clippy --fix 32 | 33 | # Bump version. level=major,minor,patch 34 | version level: 35 | git diff-index --exit-code HEAD > /dev/null || ! echo You have untracked changes. Commit your changes before bumping the version. 36 | cargo set-version --bump {{ level }} 37 | cargo update # This bumps Cargo.lock 38 | VERSION=$(rg "version = \"([0-9.]+)\"" -or '$1' Cargo.toml | head -n1) && \ 39 | git commit -am "Bump version {{ level }} to $VERSION" && \ 40 | git tag v$VERSION && \ 41 | git push origin v$VERSION 42 | git push 43 | 44 | publish: 45 | cargo publish 46 | 47 | patch: test 48 | just version patch 49 | just publish 50 | 51 | # Used to test the package script. You probably don't need this. 52 | package: 53 | rm -rf dist 54 | REF=master TARGET=$(rustc -vV | sed -n 's|host: ||p') TARGET_RUSTFLAGS= OS=macos-latest GITHUB_REPOSITORY=kurtbuilds/dye \ 55 | .github/package -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright © `` `` 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the “Software”), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

4 | 5 | GitHub Contributors 6 | 7 | 8 | Stars 9 | 10 | 11 | Build Status 12 | 13 | 14 | Downloads 15 | 16 | 17 | Crates.io 18 | 19 | 20 | # Dye 21 | 22 | `dye` is a tool to easily color text in shell. 23 | 24 | # Usage 25 | 26 | See the gif below to see these commands in action. 27 | 28 | ```bash 29 | echo $(dye --red WARN) This tool will knock your socks off. 30 | 31 | echo $(dye -r WARN) It takes too many characters to type --red, so every display modifier has a shortcode. 32 | 33 | echo $(dye -cl INFO) If your shell supports it, this label will be *blinking*. How cool is that? 34 | 35 | echo $(dye --cyan INFO) Lowercase letter options modify the text, uppercase modifies the $(dye -R --black background). 36 | 37 | echo $(dye -g SUCCESS) You are a $(dye -u great) engineer for using tools that make your life simpler and easier. 38 | ``` 39 | 40 | ![gif of dye example usage](usage.gif) 41 | 42 | # Installation 43 | 44 | cargo install dye-cli 45 | 46 | # Documentation 47 | 48 | The `--help` option gives you all you need to know. Lowercase short options affect foreground color and style, and 49 | uppercase short options affect the background. 50 | 51 | Add color to text. Pass text as arguments (like the echo command), or pass no arguments to read 52 | stdin. 53 | 54 | USAGE: 55 | dye [OPTIONS] [string]... 56 | 57 | OPTIONS: 58 | -b, --blue Set foreground blue 59 | -B, --bgblue Set background blue 60 | -c, --cyan Set foreground cyan 61 | -C, --bgcyan Set background cyan 62 | -d, --bold Add bold style 63 | -g, --green Set foreground green 64 | -G, --bggreen Set background green 65 | -h, --help Print help information 66 | -i, --italic Add italic style 67 | -k, --black Set foreground black 68 | -K, --bgblack Set background black 69 | -l, --blink Add blink style 70 | -m, --dimmed Add dimmed style 71 | -p, --purple Set foreground purple 72 | -P, --bgpurple Set background purple 73 | -r, --red Set foreground red 74 | -R, --bgred Set background red 75 | -u, --underline Add underline style 76 | -v, --reversed Add reversed style 77 | -V, --version Print version information 78 | -w, --white Set foreground white 79 | -W, --bgwhite Set background white 80 | -y, --yellow Set foreground yellow 81 | -Y, --bgyellow Set background yellow 82 | 83 | # Contributing 84 | 85 | Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. 86 | 87 | If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". 88 | Don't forget to give the project a star! Thanks again! 89 | 90 | 1. Fork the Project 91 | 2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) 92 | 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) 93 | 4. Push to the Branch (`git push origin feature/AmazingFeature`) 94 | 5. Open a Pull Request 95 | 96 | # Acknowledgments 97 | 98 | This project is built on the great work in these projects: 99 | 100 | * [`colored`](https://github.com/mackwic/colored) does the heavy lifting of setting color escape codes. 101 | * [`clap`](https://github.com/clap-rs/clap) makes creating CLIs in Rust easy! 102 | 103 |

(back to top)

104 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate colored; 2 | extern crate clap; 3 | 4 | use clap::{App, Arg}; 5 | use colored::*; 6 | use std::{io, env}; 7 | use std::io::Read; 8 | use colored::control::{SHOULD_COLORIZE}; 9 | 10 | const VERSION: &'static str = env!("CARGO_PKG_VERSION"); 11 | 12 | /** 13 | Colors: 14 | black 15 | red 16 | green 17 | yellow 18 | blue 19 | magenta (or purple) 20 | cyan 21 | white 22 | */ 23 | 24 | fn normalize_env(env_res: Result) -> Option { 25 | env_res.ok().map(|s| s != "0") 26 | } 27 | 28 | fn main() -> io::Result<()> { 29 | let args = App::new("dye") 30 | .version(VERSION) 31 | .about("Add color to text. Pass text as arguments (like the echo command), or pass no arguments to read stdin.") 32 | .arg(Arg::new("string").min_values(0)) 33 | 34 | // foreground 35 | .arg("-k, --black 'Set foreground black'") 36 | .arg("-r, --red 'Set foreground red'") 37 | .arg("-g, --green 'Set foreground green'") 38 | .arg("-y, --yellow 'Set foreground yellow'") 39 | .arg("-b, --blue 'Set foreground blue'") 40 | .arg("-p, --purple 'Set foreground purple'") 41 | .arg("-c, --cyan 'Set foreground cyan'") 42 | .arg("-w, --white 'Set foreground white'") 43 | 44 | // style 45 | .arg("-d, --bold 'Add bold style'") 46 | .arg("-i, --italic 'Add italic style'") 47 | .arg("-m, --dimmed 'Add dimmed style'") 48 | .arg("-l, --blink 'Add blink style'") 49 | .arg("-u, --underline 'Add underline style'") 50 | .arg("-v, --reversed 'Add reversed style'") 51 | 52 | // background 53 | .arg("-K, --bgblack 'Set background black'") 54 | .arg("-R, --bgred 'Set background red'") 55 | .arg("-G, --bggreen 'Set background green'") 56 | .arg("-Y, --bgyellow 'Set background yellow'") 57 | .arg("-B, --bgblue 'Set background blue'") 58 | .arg("-P, --bgpurple 'Set background purple'") 59 | .arg("-C, --bgcyan 'Set background cyan'") 60 | .arg("-W, --bgwhite 'Set background white'") 61 | 62 | .get_matches(); 63 | 64 | let mut print_newline = true; 65 | let string = match args.values_of("string") { 66 | Some(x) => x.collect::>().join(" "), 67 | None => { 68 | print_newline = false; 69 | let mut buf = String::new(); 70 | std::io::stdin().read_to_string(&mut buf)?; 71 | buf 72 | } 73 | }; 74 | 75 | let mut colored = if args.is_present("black") { 76 | string.black() 77 | } else if args.is_present("red") { 78 | string.red() 79 | } else if args.is_present("green") { 80 | string.green() 81 | } else if args.is_present("yellow") { 82 | string.yellow() 83 | } else if args.is_present("blue") { 84 | string.blue() 85 | } else if args.is_present("purple") { 86 | string.purple() 87 | } else if args.is_present("cyan") { 88 | string.cyan() 89 | } else if args.is_present("white") { 90 | string.white() 91 | } else { 92 | string.as_str().into() 93 | }; 94 | 95 | if args.is_present("bold") { 96 | colored = colored.bold(); 97 | } 98 | if args.is_present("italic") { 99 | colored = colored.italic(); 100 | } 101 | if args.is_present("dimmed") { 102 | colored = colored.dimmed(); 103 | } 104 | if args.is_present("blink") { 105 | colored = colored.blink(); 106 | } 107 | if args.is_present("underline") { 108 | colored = colored.underline(); 109 | } 110 | if args.is_present("reversed") { 111 | colored = colored.reversed(); 112 | } 113 | 114 | colored = if args.is_present("bgblack") { 115 | colored.on_black() 116 | } else if args.is_present("bgred") { 117 | colored.on_red() 118 | } else if args.is_present("bggreen") { 119 | colored.on_green() 120 | } else if args.is_present("bgyellow") { 121 | colored.on_yellow() 122 | } else if args.is_present("bgblue") { 123 | colored.on_blue() 124 | } else if args.is_present("bgpurple") { 125 | colored.on_purple() 126 | } else if args.is_present("bgcyan") { 127 | colored.on_cyan() 128 | } else if args.is_present("bgwhite") { 129 | colored.on_white() 130 | } else { 131 | colored 132 | }; 133 | 134 | SHOULD_COLORIZE.set_override( 135 | if normalize_env(env::var("CLICOLOR_FORCE")) == Some(true) { 136 | true 137 | } else if normalize_env(env::var("NO_COLOR")).is_some() { 138 | false 139 | } else { 140 | true 141 | } 142 | ); 143 | 144 | // newlines are treated differently when inside escape blocks vs outside, so the newline 145 | // can't be a part of the colored string. 146 | print!("{}{}", colored, if print_newline { "\n" } else { "" }); 147 | Ok(()) 148 | } -------------------------------------------------------------------------------- /usage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kurtbuilds/dye/a264152e36dde38cab5e68262890eef8874a2970/usage.gif --------------------------------------------------------------------------------