Skip to content

Instantly share code, notes, and snippets.

@nanjizal
Last active June 6, 2022 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanjizal/e85865da7b19da080ae5d2516f82c322 to your computer and use it in GitHub Desktop.
Save nanjizal/e85865da7b19da080ae5d2516f82c322 to your computer and use it in GitHub Desktop.
Demo of 256 color in neko
# assumes putting classes in subdirectory 'src'
-cp src
--neko hello.n
--main Main
-cmd neko hello.n
import SysPrint256;
class Main {
static final text = 'Hello';
/**
* Demo of 256 color in terminal
**/
static public function main():Void {
var cyan = 51;
var purple = 57;
var colortxt = fontColor( text, cyan, purple );
Sys.println('$begin$colortxt$end');
var str = '';
for( i in 0...12 ){
colortxt = fontColor( text, Std.random(256) );
str += '$colortxt $begin';
}
str = str.substr(0,str.length-begin.length);
println( str );
}
}
// MIT license
// see pallette library
// https://github.com/nanjizal/pallette/blob/master/LICENSE
final begin = '\033[';
final end = '\033[0m';
final fore = '38;5;';
final back = '48;5;';
function println( txt: String ){
Sys.println('$begin$txt$end');
}
function fontColor( txt: String, foreIndex: Int, ?backIndex: Int = -1 ){
var foreColor = txColor( foreIndex );
return if( backIndex == -1 ){
'${foreColor}m$txt';
} else {
var backColor = bgColor( backIndex );
'$foreColor;${backColor}m$txt';
}
}
function txColor( colorIndex: Int ){
var color = Std.string( colorIndex );
return '$fore$color';
}
function bgColor( colorIndex: Int ){
var color = Std.string( colorIndex );
return '$back$color';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment