Library : private.nim
Status : stable
License : MIT opensource
Version : 0.9.2
ProjectStart: 2015-06-20
Compiler : Nim 0.12.0 dev
OS : Linux
Description :
private.nim is a public library with a collection of simple procs and templates
for easy colored display in a linux terminal , date handling and more
some procs may mirror functionality found in other moduls for convenience
Usage : import private
Project : https://github.com/qqtop/NimCx
Docs : http://qqtop.github.io/private.html
Tested : on Ubuntu 14.04 , OpenSuse 13.2 , OpenSuse Leap42.1 , Mint 17
Related :
- demos : privateDemo.nim
- tests : privateTest.nim
Programming : qqTop
Note : may be improved at any time
Required : see imports for modules currently expected to be available
Lets
nimsx* = @ [NIMX1, NIMX2, NIMX3, NIMX4, NIMX5]
emojis* = @ [check, xmark, heart, sun, star, darkstar, umbrella, flag, snowflake, music, scissors, trademark, copyright, roof, skull, smile, smiley, innocent, lol, tongue, blush, sad, cry, rage, cat, kitty, monkey, cow]
colorNames* = @ [("aliceblue", aliceblue), ("antiquewhite", antiquewhite), ("aqua", aqua), ("aquamarine", aquamarine), ("azure", azure), ("beige", beige), ("bisque", bisque), ("black", black), ("blanchedalmond", blanchedalmond), ("blue", blue), ("blueviolet", blueviolet), ("brown", brown), ("burlywood", burlywood), ("cadetblue", cadetblue), ("chartreuse", chartreuse), ("chocolate", chocolate), ("coral", coral), ("cornflowerblue", cornflowerblue), ("cornsilk", cornsilk), ("crimson", crimson), ("cyan", cyan), ("darkblue", darkblue), ("darkcyan", darkcyan), ("darkgoldenrod", darkgoldenrod), ("darkgray", darkgray), ("darkgreen", darkgreen), ("darkkhaki", darkkhaki), ("darkmagenta", darkmagenta), ("darkolivegreen", darkolivegreen), ("darkorange", darkorange), ("darkorchid", darkorchid), ("darkred", darkred), ("darksalmon", darksalmon), ("darkseagreen", darkseagreen), ("darkslateblue", darkslateblue), ("darkslategray", darkslategray), ("darkturquoise", darkturquoise), ("darkviolet", darkviolet), ("deeppink", deeppink), ("deepskyblue", deepskyblue), ("dimgray", dimgray), ("dodgerblue", dodgerblue), ("firebrick", firebrick), ("floralwhite", floralwhite), ("forestgreen", forestgreen), ("fuchsia", fuchsia), ("gainsboro", gainsboro), ("ghostwhite", ghostwhite), ("gold", gold), ("goldenrod", goldenrod), ("gray", gray), ("green", green), ("greenyellow", greenyellow), ("honeydew", honeydew), ("hotpink", hotpink), ("indianred", indianred), ("indigo", indigo), ("ivory", ivory), ("khaki", khaki), ("lavender", lavender), ("lavenderblush", lavenderblush), ("lawngreen", lawngreen), ("lemonchiffon", lemonchiffon), ("lightblue", lightblue), ("lightcoral", lightcoral), ("lightcyan", lightcyan), ("lightgoldenrodyellow", lightgoldenrodyellow), ("lightgrey", lightgrey), ("lightgreen", lightgreen), ("lightpink", lightpink), ("lightsalmon", lightsalmon), ("lightseagreen", lightseagreen), ("lightskyblue", lightskyblue), ("lightslategray", lightslategray), ("lightsteelblue", lightsteelblue), ("lightyellow", lightyellow), ("lime", lime), ("limegreen", limegreen), ("linen", linen), ("magenta", magenta), ("maroon", maroon), ("mediumaquamarine", mediumaquamarine), ("mediumblue", mediumblue), ("mediumorchid", mediumorchid), ("mediumpurple", mediumpurple), ("mediumseagreen", mediumseagreen), ("mediumslateblue", mediumslateblue), ("mediumspringgreen", mediumspringgreen), ("mediumturquoise", mediumturquoise), ("mediumvioletred", mediumvioletred), ("midnightblue", midnightblue), ("mintcream", mintcream), ("mistyrose", mistyrose), ("moccasin", moccasin), ("navajowhite", navajowhite), ("navy", navy), ("oldlace", oldlace), ("olive", olive), ("olivedrab", olivedrab), ("orange", orange), ("orangered", orangered), ("orchid", orchid), ("palegoldenrod", palegoldenrod), ("palegreen", palegreen), ("paleturquoise", paleturquoise), ("palevioletred", palevioletred), ("papayawhip", papayawhip), ("peachpuff", peachpuff), ("peru", peru), ("pink", pink), ("plum", plum), ("powderblue", powderblue), ("purple", purple), ("red", red), ("rosybrown", rosybrown), ("royalblue", royalblue), ("saddlebrown", saddlebrown), ("salmon", salmon), ("sandybrown", sandybrown), ("seagreen", seagreen), ("seashell", seashell), ("sienna", sienna), ("silver", silver), ("skyblue", skyblue), ("slateblue", slateblue), ("slategray", slategray), ("snow", snow), ("springgreen", springgreen), ("steelblue", steelblue), ("tan", tan), ("teal", teal), ("thistle", thistle), ("tomato", tomato), ("turquoise", turquoise), ("violet", violet), ("wheat", wheat), ("white", white), ("whitesmoke", whitesmoke), ("yellow", yellow), ("yellowgreen", yellowgreen), ("pastelbeige", pastelbeige), ("pastelblue", pastelblue), ("pastelgreen", pastelgreen), ("pastelorange", pastelorange), ("pastelpink", pastelpink), ("pastelwhite", pastelwhite), ("pastelyellow", pastelyellow), ("pastelyellowgreen", pastelyellowgreen), ("truetomato", truetomato)]
rxCol* = toSeq(colorNames.low .. colorNames.high)
- index into colorNames
cards* = @ ["\xF0\x9F\x82\xA1", "\xF0\x9F\x82\xB1", "\xF0\x9F\x83\x81", "\xF0\x9F\x83\x91", "\xF0\x9F\x82\xA2", "\xF0\x9F\x82\xB2", "\xF0\x9F\x83\x82", "\xF0\x9F\x83\x92", "\xF0\x9F\x82\xA3", "\xF0\x9F\x82\xB3", "\xF0\x9F\x83\x83", "\xF0\x9F\x83\x93", "\xF0\x9F\x82\xA4", "\xF0\x9F\x82\xB4", "\xF0\x9F\x83\x84", "\xF0\x9F\x83\x94", "\xF0\x9F\x82\xA5", "\xF0\x9F\x82\xB5", "\xF0\x9F\x83\x85", "\xF0\x9F\x83\x95", "\xF0\x9F\x82\xA6", "\xF0\x9F\x82\xB6", "\xF0\x9F\x83\x86", "\xF0\x9F\x83\x96", "\xF0\x9F\x82\xA7", "\xF0\x9F\x82\xB7", "\xF0\x9F\x83\x87", "\xF0\x9F\x83\x97", "\xF0\x9F\x82\xA8", "\xF0\x9F\x82\xB8", "\xF0\x9F\x83\x88", "\xF0\x9F\x83\x98", "\xF0\x9F\x82\xA9", "\xF0\x9F\x82\xB9", "\xF0\x9F\x83\x89", "\xF0\x9F\x83\x99", "\xF0\x9F\x82\xAA", "\xF0\x9F\x82\xBA", "\xF0\x9F\x83\x8A", "\xF0\x9F\x83\x9A", "\xF0\x9F\x82\xAB", "\xF0\x9F\x82\xBB", "\xF0\x9F\x83\x8B", "\xF0\x9F\x83\x9B", "\xF0\x9F\x82\xAC", "\xF0\x9F\x82\xBC", "\xF0\x9F\x83\x8C", "\xF0\x9F\x83\x9C", "\xF0\x9F\x82\xAD", "\xF0\x9F\x82\xBD", "\xF0\x9F\x83\x8D", "\xF0\x9F\x83\x9D", "\xF0\x9F\x82\xAE", "\xF0\x9F\x82\xBE", "\xF0\x9F\x83\x8E", "\xF0\x9F\x83\x9E", "\xF0\x9F\x82\xA0", "\xF0\x9F\x82\xBF", "\xF0\x9F\x83\x8F", "\xF0\x9F\x83\x9F"]
rxCards* = toSeq(cards.low .. cards.high)
- index into cards
start* = epochTime()
- check execution timing with one line see doFinish
Consts
PRIVATLIBVERSION* = "0.9.2"
termred* = getfg(fgRed)
termgreen* = getfg(fgGreen)
termblue* = getfg(fgBlue)
termcyan* = getfg(fgCyan)
termyellow* = getfg(fgYellow)
termwhite* = getfg(fgWhite)
termblack* = getfg(fgBlack)
termmagenta* = getfg(fgMagenta)
brightred* = fbright(fgRed)
brightgreen* = fbright(fgGreen)
brightblue* = fbright(fgBlue)
brightcyan* = fbright(fgCyan)
brightyellow* = fbright(fgYellow)
brightwhite* = fbright(fgWhite)
brightmagenta* = fbright(fgMagenta)
brightblack* = fbright(fgBlack)
clrainbow* = "clrainbow"
bred* = getbg(bgRed)
bgreen* = getbg(bgGreen)
bblue* = getbg(bgBlue)
bcyan* = getbg(bgCyan)
byellow* = getbg(bgYellow)
bwhite* = getbg(bgWhite)
bblack* = getbg(bgBlack)
bmagenta* = getbg(bgMagenta)
bbrightred* = bbright(bgRed)
bbrightgreen* = bbright(bgGreen)
bbrightblue* = bbright(bgBlue)
bbrightcyan* = bbright(bgCyan)
bbrightyellow* = bbright(bgYellow)
bbrightwhite* = bbright(bgWhite)
bbrightmagenta* = bbright(bgMagenta)
bbrightblack* = bbright(bgBlack)
pastelgreen* = "\x1B[38;2;179;226;205m"
pastelorange* = "\x1B[38;2;253;205;172m"
pastelblue* = "\x1B[38;2;203;213;232m"
pastelpink* = "\x1B[38;2;244;202;228m"
pastelyellowgreen* = "\x1B[38;2;230;245;201m"
pastelyellow* = "\x1B[38;2;255;242;174m"
pastelbeige* = "\x1B[38;2;241;226;204m"
pastelwhite* = "\x1B[38;2;204;204;204m"
truetomato* = "\x1B[38;2;255;100;0m"
aliceblue* = "\x1B[38;2;240;248;255m"
antiquewhite* = "\x1B[38;2;250;235;215m"
aqua* = "\x1B[38;2;0;255;255m"
aquamarine* = "\x1B[38;2;127;255;212m"
azure* = "\x1B[38;2;240;255;255m"
beige* = "\x1B[38;2;245;245;220m"
bisque* = "\x1B[38;2;255;228;196m"
black* = "\x1B[38;2;0;0;0m"
blanchedalmond* = "\x1B[38;2;255;235;205m"
blue* = "\x1B[38;2;0;0;255m"
blueviolet* = "\x1B[38;2;138;43;226m"
brown* = "\x1B[38;2;165;42;42m"
burlywood* = "\x1B[38;2;222;184;135m"
cadetblue* = "\x1B[38;2;95;158;160m"
chartreuse* = "\x1B[38;2;127;255;0m"
chocolate* = "\x1B[38;2;210;105;30m"
coral* = "\x1B[38;2;255;127;80m"
cornflowerblue* = "\x1B[38;2;100;149;237m"
cornsilk* = "\x1B[38;2;255;248;220m"
crimson* = "\x1B[38;2;220;20;60m"
cyan* = "\x1B[38;2;0;255;255m"
darkblue* = "\x1B[38;2;0;0;139m"
darkcyan* = "\x1B[38;2;0;139;139m"
darkgoldenrod* = "\x1B[38;2;184;134;11m"
darkgray* = "\x1B[38;2;169;169;169m"
darkgreen* = "\x1B[38;2;0;100;0m"
darkkhaki* = "\x1B[38;2;189;183;107m"
darkmagenta* = "\x1B[38;2;139;0;139m"
darkolivegreen* = "\x1B[38;2;85;107;47m"
darkorange* = "\x1B[38;2;255;140;0m"
darkorchid* = "\x1B[38;2;153;50;204m"
darkred* = "\x1B[38;2;139;0;0m"
darksalmon* = "\x1B[38;2;233;150;122m"
darkseagreen* = "\x1B[38;2;143;188;143m"
darkslateblue* = "\x1B[38;2;72;61;139m"
darkslategray* = "\x1B[38;2;47;79;79m"
darkturquoise* = "\x1B[38;2;0;206;209m"
darkviolet* = "\x1B[38;2;148;0;211m"
deeppink* = "\x1B[38;2;255;20;147m"
deepskyblue* = "\x1B[38;2;0;191;255m"
dimgray* = "\x1B[38;2;105;105;105m"
dodgerblue* = "\x1B[38;2;30;144;255m"
firebrick* = "\x1B[38;2;178;34;34m"
floralwhite* = "\x1B[38;2;255;250;240m"
forestgreen* = "\x1B[38;2;34;139;34m"
fuchsia* = "\x1B[38;2;255;0;255m"
gainsboro* = "\x1B[38;2;220;220;220m"
ghostwhite* = "\x1B[38;2;248;248;255m"
gold* = "\x1B[38;2;255;215;0m"
goldenrod* = "\x1B[38;2;218;165;32m"
gray* = "\x1B[38;2;128;128;128m"
green* = "\x1B[38;2;0;128;0m"
greenyellow* = "\x1B[38;2;173;255;47m"
honeydew* = "\x1B[38;2;240;255;240m"
hotpink* = "\x1B[38;2;255;105;180m"
indianred* = "\x1B[38;2;205;92;92m"
indigo* = "\x1B[38;2;75;0;130m"
ivory* = "\x1B[38;2;255;255;240m"
khaki* = "\x1B[38;2;240;230;140m"
lavender* = "\x1B[38;2;230;230;250m"
lavenderblush* = "\x1B[38;2;255;240;245m"
lawngreen* = "\x1B[38;2;124;252;0m"
lemonchiffon* = "\x1B[38;2;255;250;205m"
lightblue* = "\x1B[38;2;173;216;230m"
lightcoral* = "\x1B[38;2;240;128;128m"
lightcyan* = "\x1B[38;2;224;255;255m"
lightgoldenrodyellow* = "\x1B[38;2;250;250;210m"
lightgrey* = "\x1B[38;2;211;211;211m"
lightgreen* = "\x1B[38;2;144;238;144m"
lightpink* = "\x1B[38;2;255;182;193m"
lightsalmon* = "\x1B[38;2;255;160;122m"
lightseagreen* = "\x1B[38;2;32;178;170m"
lightskyblue* = "\x1B[38;2;135;206;250m"
lightslategray* = "\x1B[38;2;119;136;153m"
lightsteelblue* = "\x1B[38;2;176;196;222m"
lightyellow* = "\x1B[38;2;255;255;224m"
lime* = "\x1B[38;2;0;255;0m"
limegreen* = "\x1B[38;2;50;205;50m"
linen* = "\x1B[38;2;250;240;230m"
magenta* = "\x1B[38;2;255;0;255m"
maroon* = "\x1B[38;2;128;0;0m"
mediumaquamarine* = "\x1B[38;2;102;205;170m"
mediumblue* = "\x1B[38;2;0;0;205m"
mediumorchid* = "\x1B[38;2;186;85;211m"
mediumpurple* = "\x1B[38;2;147;112;216m"
mediumseagreen* = "\x1B[38;2;60;179;113m"
mediumslateblue* = "\x1B[38;2;123;104;238m"
mediumspringgreen* = "\x1B[38;2;0;250;154m"
mediumturquoise* = "\x1B[38;2;72;209;204m"
mediumvioletred* = "\x1B[38;2;199;21;133m"
midnightblue* = "\x1B[38;2;25;25;112m"
mintcream* = "\x1B[38;2;245;255;250m"
mistyrose* = "\x1B[38;2;255;228;225m"
moccasin* = "\x1B[38;2;255;228;181m"
oldlace* = "\x1B[38;2;253;245;230m"
olive* = "\x1B[38;2;128;128;0m"
olivedrab* = "\x1B[38;2;107;142;35m"
orange* = "\x1B[38;2;255;165;0m"
orangered* = "\x1B[38;2;255;69;0m"
orchid* = "\x1B[38;2;218;112;214m"
palegoldenrod* = "\x1B[38;2;238;232;170m"
palegreen* = "\x1B[38;2;152;251;152m"
paleturquoise* = "\x1B[38;2;175;238;238m"
palevioletred* = "\x1B[38;2;216;112;147m"
papayawhip* = "\x1B[38;2;255;239;213m"
peachpuff* = "\x1B[38;2;255;218;185m"
peru* = "\x1B[38;2;205;133;63m"
pink* = "\x1B[38;2;255;192;203m"
plum* = "\x1B[38;2;221;160;221m"
powderblue* = "\x1B[38;2;176;224;230m"
purple* = "\x1B[38;2;128;0;128m"
red* = "\x1B[38;2;255;0;0m"
rosybrown* = "\x1B[38;2;188;143;143m"
royalblue* = "\x1B[38;2;65;105;225m"
saddlebrown* = "\x1B[38;2;139;69;19m"
salmon* = "\x1B[38;2;250;128;114m"
sandybrown* = "\x1B[38;2;244;164;96m"
seagreen* = "\x1B[38;2;46;139;87m"
seashell* = "\x1B[38;2;255;245;238m"
sienna* = "\x1B[38;2;160;82;45m"
silver* = "\x1B[38;2;192;192;192m"
skyblue* = "\x1B[38;2;135;206;235m"
slateblue* = "\x1B[38;2;106;90;205m"
slategray* = "\x1B[38;2;112;128;144m"
snow* = "\x1B[38;2;255;250;250m"
springgreen* = "\x1B[38;2;0;255;127m"
steelblue* = "\x1B[38;2;70;130;180m"
tan* = "\x1B[38;2;210;180;140m"
teal* = "\x1B[38;2;0;128;128m"
thistle* = "\x1B[38;2;216;191;216m"
tomato* = "\x1B[38;2;255;99;71m"
turquoise* = "\x1B[38;2;64;224;208m"
violet* = "\x1B[38;2;238;130;238m"
wheat* = "\x1B[38;2;245;222;179m"
white* = "\x1B[38;2;255;255;255m"
whitesmoke* = "\x1B[38;2;245;245;245m"
yellow* = "\x1B[38;2;255;255;0m"
yellowgreen* = "\x1B[38;2;154;205;50m"
check* = "\xE2\x9C\x93"
xmark* = "\xE2\x9C\x98"
heart* = "\xE2\x9D\xA4"
sun* = "\xE2\x98\x80"
star* = "\xE2\x98\x85"
darkstar* = "\xE2\x98\x86"
umbrella* = "\xE2\x98\x82"
flag* = "\xE2\x9A\x91"
snowflake* = "\xE2\x9D\x84"
music* = "\xE2\x99\xAB"
scissors* = "\xE2\x9C\x82"
trademark* = "\xE2\x84\xA2"
copyright* = "\xC2\xA9"
roof* = "\xEF\xA3\xBF"
skull* = "\xE2\x98\xA0"
smile* = "\xE2\x98\xBA"
smiley* = "\xF0\x9F\x98\x83"
innocent* = "\xF0\x9F\x98\x87"
lol* = "\xF0\x9F\x98\x82"
tongue* = "\xF0\x9F\x98\x9B"
blush* = "\xF0\x9F\x98\x8A"
sad* = "\xF0\x9F\x98\x9F"
cry* = "\xF0\x9F\x98\xA2"
rage* = "\xF0\x9F\x98\xA1"
cat* = "\xF0\x9F\x98\xBA"
kitty* = "\xF0\x9F\x90\xB1"
monkey* = "\xF0\x9F\x90\xB5"
cow* = "\xF0\x9F\x90\xAE"
wideDot* = "\xE2\x9A\xAB" & " "
Procs
proc getTerminalWidth*(): int
-
getTerminalWidth
and get linux terminal width in columns
proc getTerminalHeight*(): int
-
getTerminalHeight
get linux terminal height in rows
proc printColStr*(colstr: string; astr: string)
- forward declaration
proc printLnColStr*(colstr: string; mvastr: varargs[string, `$`])
- forward declaration
proc printBiCol*(s: string; sep: string; colLeft: string = yellowgreen; colRight: string = termwhite)
- forward declaration
proc printLnBiCol*(s: string; sep: string; colLeft: string = yellowgreen; colRight: string = termwhite)
- forward declaration
proc rainbow*[T](s: T)
- forward declaration
proc printStyledsimple*[T](ss: T; fg: string; astyle: set[Style])
- forward declaration
proc printStyled*[T](ss: T; substr: string; col: string; astyle: set[Style])
- forward declaration
proc print*[T](astring: T; fgr: string = white; bgr: string = black)
proc hline*(n: int = tw; col: string = white)
- forward declaration
proc hlineLn*(n: int = tw; col: string = white)
- forward declaration
proc cleanScreen*()
-
cleanScreen
clear screen with escape seqs
similar to terminal.eraseScreen() but seems to clean the terminal window completely
proc centerPos*(astring: string)
-
centerpos
tries to move cursor so that string is centered when printing
var s = "Hello I am centered" centerPos(s) printLn(s,gray)
proc checkColor*(colname: string): bool
-
checkColor
returns true if colname is a known color name , obviously
proc hline*(n: int = tw; col: string = white)
-
hline
draw a full line in stated length and color no linefeed will be issued
defaults full terminal width and white
hline(30,green)
proc hlineLn*(n: int = tw; col: string = white)
-
hlineLn
draw a full line in stated length and color a linefeed will be issued
defaults full terminal width and white
hlineLn(30,green)
proc dline*(n: int = tw; lt: string = "-"; col: string = termwhite)
-
dline
draw a dashed line with given length in current terminal font color line char can be changed
dline(30) dline(30,"/+") dline(30,col= yellow)
proc dlineLn*(n: int = tw; lt: string = "-"; col: string = termwhite)
-
dlineLn
draw a dashed line with given length in current terminal font color line char can be changed
and issue a new line
dline(50,":",green) dlineLn(30) dlineLn(30,"/+/") dlineLn(60,col = salmon)
proc decho*(z: int = 1)
-
decho
blank lines creator
decho(10)
to create 10 blank lines
proc sleepy*[T: float | int](s: T)
-
sleepy
imitates sleep but in seconds
proc rainbow*[T](s: T)
-
rainbow
multicolored string
may not work with certain Rune
proc print*[T](astring: T; fgr: string = white; bgr: string = black)
-
print
same as printLn without new line
for extended colorset background colors use printStyled with styleReverse
also see cecho
proc printLn*[T](astring: T; fgr: string = white; bgr: string = black)
-
printLn
similar to echo but with foregroundcolor and backgroundcolor
selection.
see testPrintLn.nim for usage examples
all colornames are supported for font color:
color names supported for background color:
white,red,green,blue,yellow,cyan,magenta,black
brightwhite,brightred,brightgreen,brightblue,brightyellow,
brightcyan,brightmagenta,brightblack
printLn("Yes , we made it.",clrainbow,brightyellow) # background has no effect with font in clrainbow printLn("Yes , we made it.",green,brightyellow)
As a side effect we also can do this now :
echo(yellowgreen,"aha nice",termwhite) echo(rosybrown) echo("grizzly bear") echo(termwhite) # reset to usual terminal white color
that is we print the string in yellowgreen , but need to reset the color manually
also see cechoLn
proc printPos*[T](astring: T; fgr: string = white; bgr: string = black; xpos = - 1; fitLine = true)
-
print
same as print but allows positioning
fitLine = true will try to write the text into the current line irrespective of xpos
proc printLnPos*[T](astring: T; fgr: string = white; bgr: string = black; xpos = - 1; fitLine = true)
-
printLnPos
same as printPos but with new line
printLnPos("This is nice",xpos=30) printLnPos("This is nice",lime) printLnPos("This is nice",bgr = red,xpos = 135,fitLine = false) printLnPos("This is nice",bgr = green,xpos = 135,fitLine = true) printLnPos("This is nice", clrainbow,xpos = tw div 2 -10)
proc printTK*[T](st: T; cols: varargs[string, `$`] = @ [white])
-
printTK
echoing of colored tokenized strings without newline
strings will be tokenized and colored according to colors in cols
- NOTE : this proc does not play well with Nimborg/high_level.nim
- if using nimborg have another module with all nimborg related processing there and import procs from this module into the main prog.
import private,strfmt printTK("test",@[clrainbow,white,red,cyan,yellow]) printTK("{} {} {} --> {}".fmt(123,"Nice",456,768.5),green,white,red,cyan) printTK("{} : {} {} --> {}".fmt(123,"Nice",456,768.5),green,brightwhite,clrainbow,red,cyan) printTK("blah",green,white,red,cyan) printTK(@[123,123,123],white,green,white) printTK("blah yep 1234 333122.12 [12,45] wahahahaha",@[green,brightred,black,yellow,cyan,clrainbow])
another way to achieve a similar effect is to use the build in styledEcho template directly like so:
styledEcho(green,"Nice ","try ",pastelgreen,"1234 ",steelblue," yep blue")
styledEcho also supports styles so this also works
styledEcho(green,"Nice ","try ",pastelgreen,styleUnderscore,"1234 ",steelblue," yep blue")
proc printLnTK*[T](st: T; cols: varargs[string, `$`])
-
printLnTK
displays colored tokenized strings and issues a newline when finished
strings will be tokenized and colored according to colors in cols
- NOTE : this proc does not play well with Nimborg/high_level.nim
- if using nimborg have another module with all nimborg related processing there and import procs from this module into the main prog.
printLnTK(@[123,456,789],@[clrainbow,white,red,cyan,yellow]) printLnTK("{} {} {} --> {}".fmt(123,"Nice",456,768.5),green,white,red,cyan) printLnTK("{} : {} {} --> {}".fmt(123,"Nice",456,768.5),green,brightwhite,clrainbow,red,cyan) printLnTK("blah",green,white,red,cyan) printLnTK("blah yep 1234 333122.12 [12,45] wahahahaha",@[green,brightred,black,yellow,cyan,clrainbow])
proc printRainbow*[T](s: T; astyle: set[Style])
-
printRainbow
print multicolored string with styles , for available styles see printStyled
may not work with certain Rune
printRainBow("WoW So Nice",{styleUnderScore}) printRainBow(" --> No Style",{})
proc printLnRainbow*[T](s: T; astyle: set[Style])
-
printLnRainbow
print multicolored string with styles , for available styles see printStyled
and issues a new line
may not work with certain Rune
printLnRainBow("WoW So Nice",{styleUnderScore}) printLnRainBow("Aha --> No Style",{})
proc printColStr*(colstr: string; astr: string)
-
printColStr
prints a string with a named color in colstr
proc printLnColStr*(colstr: string; mvastr: varargs[string, `$`])
-
printLnColStr
similar to printColStr but issues a writeLine() command that is
every item will be shown on a new line in the same given color
and most everything passed in will be converted to string
printLnColStr lightsalmon,"Nice try 1", 2.52234, @[4, 5, 6]
proc printBiCol*(s: string; sep: string; colLeft: string = yellowgreen; colRight: string = termwhite)
-
printBiCol
echos a line in 2 colors based on a seperators first occurance
import private,strutils,strfmt for x in 0.. <3: # here use default colors for left and right side of the seperator printBiCol("Test $1 : Ok this was $1 : what" % $x,":") for x in 4.. <6: # here we change the default colors printBiCol("Test $1 : Ok this was $1 : what" % $x,":",cyan,red) # following requires strfmt module printBiCol("{} : {} {}".fmt("Good Idea","Number",50),":",yellow,green)
proc printLnBiCol*(s: string; sep: string; colLeft: string = yellowgreen; colRight: string = termwhite)
-
printLnBiCol
same as printBiCol but issues a new line
import private,strutils,strfmt for x in 0.. <3: # here use default colors for left and right side of the seperator printLnBiCol("Test $1 : Ok this was $1 : what" % $x,":") for x in 4.. <6: # here we change the default colors printLnBiCol("Test $1 : Ok this was $1 : what" % $x,":",cyan,red) # following requires strfmt module printLnBiCol("{} : {} {}".fmt("Good Idea","Number",50),":",yellow,green)
proc printHl*(s: string; substr: string; col: string = termwhite)
-
printHl
print and highlight all appearances of a char or substring of a string
with a certain color
printHl("HELLO THIS IS A TEST","T",green)
this would highlight all T in green
proc printLnHl*(s: string; substr: string; col: string = termwhite)
-
printLnHl
print and highlight all appearances of a char or substring of a string
with a certain color and issue a new line
printLnHl("HELLO THIS IS A TEST","T",yellowgreen)
this would highlight all T in yellowgreen
proc printStyledSimple*[T](ss: T; fg: string; astyle: set[Style])
-
printStyledsimple
an extended version of writestyled to enable colors
proc printStyled*[T](ss: T; substr: string; col: string; astyle: set[Style])
-
printStyled
extended version of writestyled and printHl to allow color and styles
to print and highlight all appearances of a substring
styles may and in some cases not have the desired effect
available styles :
styleBright = 1, # bright text
styleDim, # dim text
styleUnknown, # unknown
styleUnderscore = 4, # underscored text
styleBlink, # blinking/bold text
styleReverse = 7, # reverses currentforground and backgroundcolor
styleHidden # hidden text
# this highlights all T in green and underscore them printStyled("HELLO THIS IS A TEST","T",green,{styleUnderScore}) # this highlights all T in rainbow colors underscore and blink them printStyled("HELLO THIS IS A TEST","T",clrainbow,{styleUnderScore,styleBlink}) # this highlights all T in rainbow colors , no style is applied printStyled("HELLO THIS IS A TEST","T",clrainbow,{})
proc printLnStyled*[T](ss: T; substr: string; col: string; astyle: set[Style])
-
printLnStyled
extended version of writestyled and printHl to allow color and styles
to print and highlight all appearances of a substring and issue a new line
styles may and in some cases not have the desired effect
# this highlights all T in green and underscore them printLnStyled("HELLO THIS IS A TEST","T",green,{styleUnderScore}) # this highlights all T in rainbow colors underscore and blink them printLnStyled("HELLO THIS IS A TEST","T",clrainbow,{styleUnderScore,styleBlink}) # this highlights all T in rainbow colors , no style is applied printLnStyled("HELLO THIS IS A TEST","T",clrainbow,{})
proc cecho*(col: string; ggg: varargs[string, `$`] = @ [""])
-
cecho
color echo w/o new line this also automically resets the color attribute
import private,strfmt cechoLn(salmon,"{:<10} : {} ==> {} --> {}".fmt("this ", "zzz ",123 ," color is something else")) echo("ok") # color resetted echo(salmon,"{:<10} : {} ==> {} --> {}".fmt("this ", "zzz ",123 ," color is something else")) echo("ok") # still salmon
proc cechoLn*(col: string; ggg: varargs[string, `$`] = @ [""])
-
cechoLn
color echo with new line
so it is easy to color your output by just replacing
echo something with cechoLn yellowgreen,something
in your exisiting projects.
import private,strutils cechoLn(steelblue,"We made it in $1 hours !" % $5)
proc showColors*()
-
showColors
display all colorNames in color !
proc doty*(d: int; col: string = white; bgr = black)
-
doty
prints number d of ⏺ style dots in given fore/background color
each dot is of char length 4 added a space in the back to avoid half drawn dots
if it is available on your system otherwise a rectangle may be shown
import private printLnBiCol("Test for : doty\n",":",truetomato,lime) dotyLn(22 ,lime) dotyLn(18 ,salmon,blue) dotyLn(tw div 2,red) # full line
color clrainbow is not supported and will be in white
proc dotyLn*(d: int; col: string = white; bgr = black)
-
dotyLn
prints number d of ⏺ style widedots given fore/background color and issues new line
each dot is of char length 4
color clrainbow is not supported and will be in white
proc printDotPos*(xpos: int; dotCol: string; blink: bool)
-
printDotPos
prints a widedot at xpos in col dotCol and may blink ...
proc drawRect*(h: int = 0; w: int = 3; frhLine: string = "_"; frVLine: string = "|"; frCol: string = darkgreen; dotCol = truetomato; xpos: int = 1; blink: bool = false)
-
drawRect
a simple proc to draw a rectangle with corners marked with widedots. widedots are of len 4.
h height w width frhLine framechar horizontal frVLine framechar vertical frCol color of line dotCol color of corner dotCol xpos topleft start position blink true or false to blink the dots
import private clearUp(18) curSet() drawRect(15,24,frhLine = "+",frvLine = wideDot , frCol = randCol(),xpos = 8) curup(12) drawRect(9,20,frhLine = "=",frvLine = wideDot , frCol = randCol(),xpos = 10,blink = true) curup(12) drawRect(9,20,frhLine = "=",frvLine = wideDot , frCol = randCol(),xpos = 35,blink = true) curup(10) drawRect(6,14,frhLine = "~",frvLine = "$" , frCol = randCol(),xpos = 70,blink = true) decho(5) doFinish()
proc validdate*(adate: string): bool
-
validdate
try to ensure correct dates of form yyyy-MM-dd
correct : 2015-08-15
wrong : 2015-08-32 , 201508-15, 2015-13-10 etc.
proc day*(aDate: string): string
-
day,month year extracts the relevant part from
a date string of format yyyy-MM-dd
proc month*(aDate: string): string
proc year*(aDate: string): string
- Format yyyy
proc intervalsecs*(startDate, endDate: string): float
- interval procs returns time elapsed between two dates in secs,hours etc.
proc intervalmins*(startDate, endDate: string): float
proc intervalhours*(startDate, endDate: string): float
proc intervaldays*(startDate, endDate: string): float
proc intervalweeks*(startDate, endDate: string): float
proc intervalmonths*(startDate, endDate: string): float
proc intervalyears*(startDate, endDate: string): float
proc compareDates*(startDate, endDate: string): int
proc dayOfWeekJulianA*(day, month, year: int): WeekDay
proc dayOfWeekJulian*(datestr: string): string
-
dayOfWeekJulian
returns the day of the week of a date given in format yyyy-MM-dd as string
valid for dates up to 2099-12-31
proc plusDays*(aDate: string; days: int): string
-
plusDays
adds days to date string of format yyyy-MM-dd or result of getDateStr()
and returns a string of format yyyy-MM-dd
the passed in date string must be a valid date or an error message will be returned
proc minusDays*(aDate: string; days: int): string
-
minusDays
subtracts days from a date string of format yyyy-MM-dd or result of getDateStr()
and returns a string of format yyyy-MM-dd
the passed in date string must be a valid date or an error message will be returned
proc getFirstMondayYear*(ayear: string): string
-
getFirstMondayYear
returns date of first monday of any given year should be ok for the next years but after 2100-02-28 all bets are off
echo getFirstMondayYear("2015")
proc getFirstMondayYearMonth*(aym: string): string
-
getFirstMondayYearMonth
returns date of first monday in given year and month
echo getFirstMondayYearMonth("2015-12") echo getFirstMondayYearMonth("2015-06") echo getFirstMondayYearMonth("2015-2")
in case of invalid dates nil will be returned should be ok for the next years but after 2100-02-28 all bets are off
proc getNextMonday*(adate: string): string
-
getNextMonday
echo getNextMonday(getDateStr())
import private # get next 10 mondays var dw = "2015-08-10" for x in 1.. 10: dw = getNextMonday(dw) echo dw
in case of invalid dates nil will be returned
proc printBigNumber*(anumber: string; fgr: string = yellowgreen; bgr: string = black; xpos: int = 1)
-
printBigNumber
prints a string in big block font
available 1234567890:
usufull for big counter etc , a clock can also be build easily but running in a tight while loop just uses up cpu cycles needlessly.
for x in 990.. 1105: cleanScreen() printBigNumber($x) sleepy(0.008) cleanScreen() printBigNumber($23456345,steelblue)
import private for x in countdown(10,0): cleanScreen() if x == 5: for y in countup(10,25): cleanScreen() printBigNumber($y,tomato) sleepy(0.5) cleanScreen() printBigNumber($x) sleepy(1) doFinish()
proc printSlimNumber*(anumber: string; fgr: string = yellowgreen; bgr: string = black; xpos: int = 1)
-
printSlimNumber
prints an string in big slim font
available chars 123456780,.:
usufull for big counter etc , a clock can also be build easily but running in a tight while loop just uses up cpu cycles needlessly.
for x in 990.. 1005: cleanScreen() printSlimNumber($x) sleepy(0.075) echo() printSlimNumber($23456345,blue) decho(2) printSlimNumber("1234567:345,23.789",fgr=salmon,xpos=20) sleepy(1.5) import times cleanScreen() decho(2) printSlimNumber($getClockStr(),fgr=salmon,xpos=20) decho(5) for x in rxCol: printSlimNumber($x,colorNames[x][1]) curup(3) sleepy(0.05) curdn(3)
proc superHeader*(bstring: string)
-
superheader
a framed header display routine
suitable for one line headers , overlong lines will
be cut to terminal window width without ceremony
proc superHeader*(bstring: string; strcol: string; frmcol: string)
-
superheader
a framed header display routine
suitable for one line headers , overlong lines will
be cut to terminal window size without ceremony
the color of the string can be selected, available colors
green,red,cyan,white,yellow and for going completely bonkers the frame
can be set to clrainbow too .
import private superheader("Ok That's it for Now !",clrainbow,white) echo()
proc superHeaderA*(bb: string = ""; strcol: string = white; frmcol: string = green; anim: bool = true; animcount: int = 1)
-
superHeaderA
attempt of an animated superheader , some defaults are given
parameters for animated superheaderA :
headerstring, txt color, frame color, left/right animation : true/false ,animcount
Example :
import private cleanScreen() let bb = "NIM the system language for the future, which extends to as far as you need !!" superHeaderA(bb,white,red,true,1) clearup(3) superheader("Ok That's it for Now !",salmon,yellowgreen) doFinish()
proc getWanIp*(): string
-
getWanIp
get your wan ip from heroku
problems ? check : https://status.heroku.com/
proc showWanIp*()
-
showWanIp
show your current wan ip
proc getIpInfo*(ip: string): JsonNode
-
getIpInfo
use ip-api.com free service limited to abt 250 requests/min
exceeding this you will need to unlock your wan ip manually at their site
the JsonNode is returned for further processing if needed
and can be queried like so
var jz = getIpInfo("208.80.152.201") echo getfields(jz) echo jz["city"].getstr
proc showIpInfo*(ip: string)
-
showIpInfo
Displays details for a given IP
Example:
showIpInfo("208.80.152.201") showIpInfo(getHosts("bbc.com")[0])
proc getHosts*(dm: string): seq[string]
-
getHosts
returns IP addresses inside a seq[string] for a domain name and
may resolve multiple IP pointing to same domain
import private var z = getHosts("bbc.co.uk") for x in z: echo x doFinish()
proc showHosts*(dm: string)
-
showHosts
displays IP addresses for a domain name and
may resolve multiple IP pointing to same domain
import private showHosts("bbc.co.uk") doFinish()
proc getRandomInt*(mi: int = 0; ma: int = int.high): int
-
getRandomInt
convenience proc so we do not need to import random in calling prog
import private,math var ps : Runningstat loopy(0.. 1000000,ps.push(getRandomInt(0,10000))) showStats(ps) doFinish()
proc createSeqInt*(n: int = 10; mi: int = 0; ma: int = int.high): seq[int]
-
createSeqInt
convenience proc to create a seq of random int with
default length 10
form @[4556,455,888,234,...] or similar
# create a seq with 50 random integers ,of set 100 .. 2000 # including the limits 100 and 2000 echo createSeqInt(50,100,2000)
proc sum*[T](aseq: seq[T]): T
-
sum code per idea from http://rnduja.github.io/2015/10/21/scientific-nim/
returns sum of float or int seqs
proc ff*(zz: float; n: int64 = 5): string
-
ff
formats a float to string with n decimals
proc getRandomFloat*(): float
-
getRandomFloat
convenience proc so we do not need to import random in calling prog
proc createSeqFloat*(n: int = 10): seq[float]
-
createSeqFloat
convenience proc to create a seq of random floats with
default length 10
form @[0.34,0.056,...] or similar
# create a seq with 50 random floats echo createSeqFloat(50)
proc getRandomPointInCircle*(radius: float): seq[float]
-
getRandomPointInCircle
based on answers found in
http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly
import private,math,strfmt # get randompoints in a circle var crad:float = 1 for x in 0.. 100: var k = getRandomPointInCircle(crad) assert k[0] <= crad and k[1] <= crad printLnBiCol("{:<25} : {}".fmt($k[0],$k[1]),":") doFinish()
proc centerMark*(showpos: bool = false)
-
centerMark
draws a red dot in the middle of the screen xpos only and also can show pos
proc tupleToStr*(xs: tuple): string
-
tupleToStr
tuple to string unpacker , returns a string
code ex nim forum
echo tupleToStr((1,2)) # prints (1, 2) echo tupleToStr((3,4)) # prints (3, 4) echo tupleToStr(("A","B","C")) # prints (A, B, C)
proc shift*[T](x: var seq[T]; zz: Natural = 0): T
-
shift takes a seq and returns the first , and deletes it from the seq
build in pop does the same from the other side
var a: seq[float] = @[1.5, 23.3, 3.4] echo shift(a) echo a
proc showStats*(x: Runningstat)
-
showStats
quickly display runningStat data
import private,math var rs:Runningstat var z = createSeqFloat(500000) for x in z: rs.push(x) showStats(rs) doFinish()
proc newDir*(dirname: string)
-
newDir
creates a new directory and provides some feedback
proc remDir*(dirname: string)
-
remDir
deletes an existing directory , all subdirectories and files and provides some feedback
root and home directory removal is disallowed
proc newWordCJK*(maxwl: int = 10): string
-
newWordCJK
creates a new random string consisting of n chars default = max 10
with chars from the cjk unicode set
http://unicode-table.com/en/#cjk-unified-ideographs
requires unicode
# create a string of chinese or CJK chars # with max length 20 and show it in green msgg() do : echo newWordCJK(20)
proc newWord*(minwl: int = 3; maxwl: int = 10): string
-
newWord
creates a new lower case random word with chars from Letters set
default min word length minwl = 3
default max word length maxwl = 10
proc newWord2*(minwl: int = 3; maxwl: int = 10): string
-
newWord2
creates a new lower case random word with chars from IdentChars set
default min word length minwl = 3
default max word length maxwl = 10
proc newWord3*(minwl: int = 3; maxwl: int = 10; nflag: bool = true): string
-
newWord3
creates a new lower case random word with chars from AllChars set if nflag = true
creates a new anycase word with chars from AllChars set if nflag = false
default min word length minwl = 3
default max word length maxwl = 10
proc newHiragana*(minwl: int = 3; maxwl: int = 10): string
-
newHiragana
creates a random hiragana word without meaning from the hiragana unicode set
default min word length minwl = 3
default max word length maxwl = 10
proc newKatakana*(minwl: int = 3; maxwl: int = 10): string
-
newKatakana
creates a random katakana word without meaning from the katakana unicode set
default min word length minwl = 3
default max word length maxwl = 10
proc iching*(): seq[string]
-
iching
returns a seq containing iching unicode chars
proc ada*(): seq[string]
-
ada
returns a seq containing ada language symbols
proc hiragana*(): seq[string]
-
hiragana
returns a seq containing hiragana unicode chars
proc katakana*(): seq[string]
-
full width katakana
returns a seq containing full width katakana unicode chars
proc fastsplit*(s: string; sep: char): seq[string]
-
fastsplit
code by jehan lifted from Nim Forum
maybe best results compile prog with : nim cc -d:release --gc:markandsweep
seperator must be a char type
proc splitty*(txt: string; sep: string): seq[string]
-
splitty
same as build in split function but this retains the
separator on the left side of the split
z = splitty("Nice weather in : Djibouti",":")
will yield:
Nice weather in : Djibouti
rather than:
Nice weather in Djibouti
with the original split()
proc showTerminalSize*()
-
showTerminalSize
displays current terminal dimensions
width is always available via tw
height is always available via th
proc qqTop*()
-
qqTop
prints qqTop in custom color
proc doInfo*()
-
doInfo
A more than you want to know information proc
proc infoLine*()
-
infoLine
prints some info for current application
proc doFinish*()
-
doFinish
a end of program routine which displays some information
can be changed to anything desired
and should be the last line of the application
proc handler*() {.noconv.}
-
handler
this runs if ctrl-c is pressed
and provides some feedback upon exit
just by using this module your project will have an automatic
exit handler via ctrl-c
this handler may not work if code compiled into a .dll or .so file
or under some circumstances like being called during readLineFromStdin
Templates
template randCol*(): string
-
randCol
get a randomcolor from colorNames
# print a string 6 times in a random color selected from colorNames loopy(0..5,printLn("Hello Random Color",randCol()))
template tw*(): int
- latest terminalwidth always available in tw
template th*(): int
- latest terminalheight always available in th
template msgg*(code: stmt): stmt
-
msgX templates convenience templates for colored text output the assumption is that the terminal is white text and black background naming of the templates is like msg+color so msgy => yellow use like : msgg() do : echo "How nice, it's in green" these templates have by large been superceded by various print and echo procs but are useful to have ins ome circumstances like draw a yellow line with width 40
msgy() do: echo "yellow"
template msggb*(code: stmt): stmt
template msgy*(code: stmt): stmt
template msgyb*(code: stmt): stmt
template msgr*(code: stmt): stmt
template msgrb*(code: stmt): stmt
template msgc*(code: stmt): stmt
template msgcb*(code: stmt): stmt
template msgw*(code: stmt): stmt
template msgwb*(code: stmt): stmt
template msgb*(code: stmt): stmt
template msgbb*(code: stmt): stmt
template msgbl*(code: stmt): stmt
template msgblb*(code: stmt): stmt
template msgm*(code: stmt): stmt
template msgmb*(code: stmt): stmt
template hdx*(code: stmt): stmt
template withFile*(f: expr; filename: string; mode: FileMode; body: stmt): stmt {. immediate.}
-
withFile
file open close utility template
Example 1
let curFile="/data5/notes.txt" # some file withFile(txt, curFile, fmRead): while true: try: printLnW(txt.readLine()) # do something with the lines except: break echo() printLn("Finished",clRainbow) doFinish()
Example 2
import private,strutils,strfmt let curFile="/data5/notes.txt" var lc = 0 var oc = 0 withFile(txt, curFile, fmRead): while 1 == 1: try: inc lc var al = $txt.readline() var sw = "the" # find all lines containing : the if al.contains(sw) == true: inc oc msgy() do: write(stdout,"{:<8}{:>6} {:<7}{:>6} ".fmt("Line :",lc,"Count :",oc)) printhl(al,sw,green) echo() except: break echo() printLn("Finished",clRainbow) doFinish()
template curUp*(x: int = 1)
-
curUp
mirrors terminal cursorUp
template curDn*(x: int = 1)
-
curDn
mirrors terminal cursorDown
template curBk*(x: int = 1)
-
curBkn
mirrors terminal cursorBackward
template curFw*(x: int = 1)
-
curFw
mirrors terminal cursorForward
template curSetx*(x: int)
-
curSetx
mirrors terminal setCursorXPos
template curSet*(x: int = 0; y: int = 0)
-
curSet
mirrors terminal setCursorPos
template clearup*(x: int = 80)
-
clearup
a convenience proc to clear monitor x rows
template clearLine*()
-
clearLine
mirrors terminal eraseLine
template getCard*(): auto
-
getCard
gets a random card from the Cards seq
import private printPos(getCard(),randCol(),xpos = tw div 2) # get card and print in random color at xpos doFinish()
template loopy*[T](ite: T; st: stmt)
-
loopy
the lazy programmer's quick simple for-loop template
loopy(0.. 100,printLnTK("The house is in the back.",brightwhite,brightblack,salmon,yellowgreen))