Console Color Plugin

Started by habi, Jun 28, 2023, 07:04 PM

Previous topic - Next topic

habi

Did you ever tried this:


Yes, squirrel plugin can output colors. (this i found only while 75% making this plugin). However the "[SCRIPT]" part remain.

This plugin can override the [SCRIPT] part if you wish.



One function: cprint

7+1 COLOR CONSTANTS - CRED, CGREEN, CYELLOW, CBLUE, CMAGENTA, CCYAN, CWHITE and CBOLD

Example: cprint(CYELLOW, "An apple a day keeps the doctor away")



Function Documentation
cprint( color, text )
color - One of the 7 color constants listed in the top which may be combined with intensity constant by '+' sign .
text - the text to print
returns - null

Remark: color can be empty - "".
The function is based on ANSI Escape Sequence and all the wide options under 'SGR parameters' of the wiki may be made use by formatting text accordingly. eg.
cprint("",format("%c[1;4;36mHELLO",27)) will print HELLO with underline in cyan color. The table of the wiki under 'Colors' may be referenced.

For more clarity see the source of the 'cprint' function:
SQInteger fn_cprint(HSQUIRRELVM v)
{
    const SQChar* text, *color;
    sq->getstring(v, 2, &color);
    sq->getstring(v, 3, &text);
    printf("%c[%sm%s%c[0m", 27, color, text,27);
    return 0;
}

Plugins - attached below
Source Code - do

Instruction: Add consolecolor04rel32 or consolecolor04rel64 to plugins line of server.cfg so that you can use the function

PSL

Oh my God, thanks for making this plugin, it's really cool to output colors in the console.
But I don't know if it's my computer, I use examples output this    ␛[;31mAn apple a day keeps the doctor away␛[0m]
I think it's the language of the system.

habi


Xmair

Quote from: habi on Jun 28, 2023, 07:04 PMHowever the "[SCRIPT]" part remain.
Its possible to remove the [SCRIPT] tag by doing something like this:

print("\rTest 123");

However some parts might remain if the total length is less than that of [SCRIPT] but you can write a wrapper function to fill the rest of the parts with spaces

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

habi

#4
Quote from: Xmair on Jun 29, 2023, 12:54 PMIts possible to remove the [SCRIPT] tag by doing something like this:
print("\rTest 123");
Perfect. Let's then print Hello World:
print(format("\r%c[1;32mHello World", 27))

habi

#5
Update
Fallback function 'hprint' added.
This function is aimed at Windows systems which do not support ANSI Color code.

Usage
hprint(cBLUE,"This text is colored.")

Again 7+1 color constants cBLUE, cGREEN, cYELLOW, cRED, cMAGENTA, cCYAN, cWHITE, cBOLD available.

Use +cBOLD for intensity.
hprint( cCYAN + cBOLD, "This is variation of cyan color")

Remark: This is based on the WINAPI function 'SetConsoleTextAttribute'. The first parameter of 'hprint' is passed as wAttributes to this function. So you may use various values of the latter for changing background color etc.

Binaries updated in first post.

Source of 'hprint':
SQInteger fn_hprint(HSQUIRRELVM v)
{
const SQChar* text; SQInteger wattributes;
sq->getinteger(v, 2, &wattributes);
sq->getstring(v, 3, &text);

#ifdef WIN32
//Credits: https://bitbucket.org/stormeus/0.4-squirrel/src/master/ConsoleUtils.cpp
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_SCREEN_BUFFER_INFO csbBefore;
GetConsoleScreenBufferInfo(hstdout, &csbBefore);
SetConsoleTextAttribute(hstdout, wattributes);
fputs(text, stdout);
SetConsoleTextAttribute(hstdout, csbBefore.wAttributes);
#else

printf("%c[%s%sm%s%c[0m", 27, (wattributes&8)==8?"1;":"", getANSIColorCode(wattributes&(~8)), text, 27);

#endif
return 0;
}
and the function used:
const char* getANSIColorCode(int i)
{
switch (i)
{
case 1:  return "34";
case 2: return "32";
case 3: return "36";//cyan
case 4: return "31";//red
case 5: return "35";//magenta
case 6: return "33";//yellow
case 7: return "37";//white
default: return "";
}
}

PSL

Well done, the update is very good, I can run this plugin perfectly, thank you so much. :>

Ridwan Rz

Why I didn't came across this? I also talked about this to you on discord! You're amazing habi! Well done!

Ridwan Rz

Quote from: habi on Jun 29, 2023, 11:26 PMUpdate
Fallback function 'hprint' added.
This function is aimed at Windows systems which do not support ANSI Color code.

Usage
hprint(cBLUE,"This text is colored.")

Again 7+1 color constants cBLUE, cGREEN, cYELLOW, cRED, cMAGENTA, cCYAN, cWHITE, cBOLD available.

Use +cBOLD for intensity.
hprint( cCYAN + cBOLD, "This is variation of cyan color")

Remark: This is based on the WINAPI function 'SetConsoleTextAttribute'. The first parameter of 'hprint' is passed as wAttributes to this function. So you may use various values of the latter for changing background color etc.

Binaries updated in first post.

Source of 'hprint':
SQInteger fn_hprint(HSQUIRRELVM v)
{
    const SQChar* text; SQInteger wattributes;
    sq->getinteger(v, 2, &wattributes);
    sq->getstring(v, 3, &text);

#ifdef WIN32
    //Credits: https://bitbucket.org/stormeus/0.4-squirrel/src/master/ConsoleUtils.cpp
    HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_SCREEN_BUFFER_INFO csbBefore;
    GetConsoleScreenBufferInfo(hstdout, &csbBefore);
    SetConsoleTextAttribute(hstdout, wattributes);
    fputs(text, stdout);
    SetConsoleTextAttribute(hstdout, csbBefore.wAttributes);
#else
   
    printf("%c[%s%sm%s%c[0m", 27, (wattributes&8)==8?"1;":"", getANSIColorCode(wattributes&(~8)), text, 27);

#endif
    return 0;
}
and the function used:
const char* getANSIColorCode(int i)
{
    switch (i)
    {
    case 1:  return "34";
    case 2: return "32";
    case 3: return "36";//cyan
    case 4: return "31";//red
    case 5: return "35";//magenta
    case 6: return "33";//yellow
    case 7: return "37";//white
    default: return "";
    }
}

can you help me out here?

I did like this:
hprint( cCYAN + cBOLD, "This is variation of cyan color "+ cYELLOW + cBOLD +"Yellow");
Output was: This is variation of cyan color 68Yellow

the yellow text is not a yellow colour yet

habi

hprint( cCYAN + cBOLD, "This is variation of cyan color ");
hprint( cYELLOW + cBOLD, "Yellow");

habi

#10
Quote from: Xmair on Jun 29, 2023, 12:54 PMHowever some parts might remain if the total length is less than that of [SCRIPT] but you can write a wrapper function to fill the rest of the parts with spaces
Today i found another way to do this.
print(format("%c[1M%c[1;32mTest", 27,27))
QuoteExplanation: The %c[1M deletes one line. So [SCRIPT] tag gets deleted.
             The %c[1;32m formats the text 1=bold, 32=green, m=separator