Skip to content

Instantly share code, notes, and snippets.

@indraniel
Forked from jabbalaci/colors.nim
Created January 25, 2019 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indraniel/e4e5ce4ccf2001586c07cf4a6e864fba to your computer and use it in GitHub Desktop.
Save indraniel/e4e5ce4ccf2001586c07cf4a6e864fba to your computer and use it in GitHub Desktop.
A small program to make using 256 colors in Nim less painful.
import strformat
import tables
# A small program to make using 256 colors in Nim less painful.
# Original ZSH version from:
# P.C. Shyamshankar <sykora@lucentbeing.com>
# Copied from https://github.com/sykora/etc/blob/master/zsh/functions/spectrum/
# Nim rewrite by Laszlo Szathmary <jabba.laci@gmail.com>
# 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment