Scala Terminal String Styling
🪶 Dependency free
🤹♂️ Apply multiple text styles
🎨 Supports 256-color and Truecolor
🏳️🌈 Gradient text styling
🕵️♂️ Automatically detects color support and style accordingly
To include Brush in your Scala project, add the following dependency:
libraryDependencies += "io.github.ranyitz" %% "brush" % "0.3.0"
To start styling your terminal strings, simply import brush._
in your Scala file:
import brush._
"bold".bold
"green".green
"inverted".blue.inverse
"strikethrough".bgYellow.italic.strikethrough
"use any css color".color("darkorchid")
"use a specific rgb color"
.bgRgb(0, 66, 77)
.rgb(255, 190, 255)
.bold
.underline
"use a specific hex color".hex("#D2691E").bgHex("#E6E6FA"))
"gradient background with rgb colors"
.gradient(
(255, 102, 102), // Red
(255, 204, 102), // Orange
(255, 255, 102), // Yellow
(204, 255, 102) // Green
)
"gradient background with named css colors"
.bgGradient(
"lavender",
"peachpuff",
"mintcream",
"aliceblue",
"palegoldenrod",
"lightgray",
"paleturquoise"
)
.black
"gradient with hex triplet colors"
.gradient(
"#008080", // Teal
"#FFFF66", // Yellow
"#FFCC66" // Orange
).black
Another option if you prefer not to use the implicit API:
import brush.Brush
val redText = Brush.decorate("red text").red
After creating a decorated string, you can apply any of the colors and modifiers.
Brush provides various text modifiers for customization:
bold
- Makes the text bold.dim
- Applies lower opacity to the text.italic
- Renders the text in italics.underline
- Adds a horizontal line below the text.inverse
- Inverts background and foreground colors.strikethrough
- Strikes a horizontal line through the text's center.overline
- Adds a horizontal line above the text.reset
- Resets the current style.hidden
- Prints the text invisibly.
black
| red
| green
| yellow
| blue
| magenta
| cyan
| white
| blackBright
| gray
| redBright
| greenBright
| yellowBright
| blueBright
| magentaBright
| cyanBright
| whiteBright
bgBlack
| bgRed
| bgGreen
| bgYellow
| bgBlue
| bgMagenta
| bgCyan
| bgWhite
| bgBlackBright
| bgGray
| bgRedBright
| bgGreenBright
| bgYellowBright
| bgBlueBright
| bgMagentaBright
| bgCyanBright
| bgWhiteBright
rgb
|truecolor
- choose a color by providing red, green, and blue values between 0 and 255.bgRgb
|bgTruecolor
- same as rgb, but for background colors.
println("Yellow Green".rgb(154, 205, 50))
println("Steel Blue".bgRgb(70, 130, 180))
css
|color
- choose a color by providing a CSS color name.bgCss
|bgColor
- same as css, but for background colors.
println("(154, 205, 50)".css("yellowgreen"))
println("(70, 130, 180)".bgCss("steelblue"))
For a complete list of available colors, refer to the full named colors list
hex
- choose a color by providing a hex triplet.bgHex
- same as hex, but for background colors.
println("Yellow Green".hex("#9acd32"))
println("Steel Blue".bgHex("#4682b4"))
Note: Gradients are only supported in terminals that support 256-colors or Truecolor.
gradient
- apply a gradient to the text by providing a list of colors.bgGradient
- same as gradient, but for background colors.
A color can be provided in any of the following formats:
- CSS color name
- Hex triplet
- RGB tuple
println("Gradient".gradient("red", "orange", "yellow"))
println("Gradient".bgGradient("#ff0000", "#ffa500", "#ffff00"))
println("Gradient".gradient((255, 0, 0), (255, 165, 0), (255, 255, 0)))
NO_COLOR
- If set to any value, disables all colors.FORCE_COLOR
- If set to any value between0
-3
, forces the following color level:0
- All colors disabled.1
- Basic 16 colors support.2
- ANSI 256 colors support.3
- Truecolor 16 million colors support.