Last active
September 3, 2023 11:48
-
-
Save jabbalaci/700fd51757b1c0a1edac4ef61946bc3d to your computer and use it in GitHub Desktop.
A small program to make using 256 colors in Nim less painful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strformat | |
# A small program to make using 256 colors in Nim less painful. | |
# Original ZSH version from: | |
# P.C. Shyamshankar <[email protected]> | |
# Copied from https://github.com/sykora/etc/blob/master/zsh/functions/spectrum/ | |
# | |
# Nim rewrite by Laszlo Szathmary <[email protected]> | |
# thanks to narimiran and kickeroo for making the code more idiomatic Nim code | |
# | |
# 16 Terminal Colors | |
# -- --------------- | |
# 0 black | |
# 1 red | |
# 2 green | |
# 3 yellow | |
# 4 blue | |
# 5 magenta | |
# 6 cyan | |
# 7 white | |
# 8 bright black | |
# 9 bright red | |
# 10 bright green | |
# 11 bright yellow | |
# 12 bright blue | |
# 13 bright magenta | |
# 14 bright cyan | |
# 15 bright white | |
type | |
FX = enum | |
fxReset = "\x1b[0m" | |
fxBold = "\x1b[1m" | |
fxNoBold = "\x1b[22m" | |
fxItalic = "\x1b[3m" | |
fxNoItalic = "\x1b[23m" | |
fxUnderline = "\x1b[4m" | |
fxNoUnderline = "\x1b[24m" | |
fxBlink = "\x1b[5m" | |
fxNoBlink = "\x1b[25m" | |
fxReverse = "\x1b[7m" | |
fxNoReverse = "\x1b[27m" | |
const | |
reset_color = fxReset | |
FG = block: | |
var res: array[256, string] | |
for color in 0 .. 255: | |
res[color] = &"\x1b[38;5;{color}m" | |
res | |
BG = block: | |
var res: array[256, string] | |
for color in 0 .. 255: | |
res[color] = &"\x1b[48;5;{color}m" | |
res | |
TEXT = "Arma virumque cano Troiae qui primus ab oris" | |
# const | |
# Show all 256 colors with color number | |
proc spectrum_ls() = | |
for code in 0 .. 255: | |
echo &"{code}: {FG[code]}{TEXT}{reset_color}" | |
# Show all 256 colors where the background is set to specific color | |
proc spectrum_bls() = | |
for code in 0 .. 255: | |
echo &"{code}: {BG[code]}{TEXT}{reset_color}" | |
proc main() = | |
spectrum_ls() | |
spectrum_bls() | |
let text = "the Nim programming language" | |
echo &"{FG[153]}{text}{reset_color}" | |
echo &"{fxBold}{FG[153]}{text}{reset_color}" | |
echo &"{fxItalic}{FG[153]}{text}{reset_color}" | |
echo &"{fxUnderline}{FG[153]}{text}{reset_color}" | |
echo &"{fxBlink}{FG[153]}{text}{reset_color}" | |
echo &"{fxReverse}{FG[153]}{text}{reset_color}" | |
# ############################################################################ | |
when isMainModule: | |
main() |
Author
jabbalaci
commented
Nov 25, 2018
•
- Screenshot: https://i.imgur.com/C1SSZF0.png
- Blog post: https://ubuntuincident.wordpress.com/2018/11/25/nim-using-the-xterm-256-colors-in-the-terminal/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment