Vice City: Multiplayer

Server Development => Community Plugins => Topic started by: habi on Jun 28, 2023, 07:04 PM

Title: Console Color Plugin
Post by: habi on Jun 28, 2023, 07:04 PM
Did you ever tried this:
(https://i.imgur.com/uCDNizy.png)

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.

(https://i.imgur.com/JFhRZsb.png)

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")

(https://i.imgur.com/tBaMNTh.png)

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 (https://en.wikipedia.org/wiki/ANSI_escape_code) 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
Title: Re: Console Color Plugin
Post by: PSL on Jun 29, 2023, 01:57 AM
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.
Title: Re: Console Color Plugin
Post by: habi on Jun 29, 2023, 08:28 AM
I will make an update.
Title: Re: Console Color Plugin
Post by: Xmair on Jun 29, 2023, 12:54 PM
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
Title: Re: Console Color Plugin
Post by: habi on Jun 29, 2023, 01:00 PM
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))(https://i.imgur.com/I2AGtxE.png)
Title: Re: Console Color Plugin
Post by: habi on Jun 29, 2023, 11:26 PM
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 (https://learn.microsoft.com/en-us/windows/console/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 "";
}
}
Title: Re: Console Color Plugin
Post by: PSL on Jun 30, 2023, 05:50 AM
Well done, the update is very good, I can run this plugin perfectly, thank you so much. :>
Title: Re: Console Color Plugin
Post by: Ridwan Rz on Aug 28, 2023, 07:10 PM
Why I didn't came across this? I also talked about this to you on discord! You're amazing habi! Well done!
Title: Re: Console Color Plugin
Post by: Ridwan Rz on Aug 29, 2023, 06:15 AM
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 (https://learn.microsoft.com/en-us/windows/console/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
Title: Re: Console Color Plugin
Post by: habi on Aug 29, 2023, 06:53 AM
hprint( cCYAN + cBOLD, "This is variation of cyan color ");
hprint( cYELLOW + cBOLD, "Yellow");
Title: Re: Console Color Plugin
Post by: habi on Dec 23, 2023, 05:08 PM
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 (https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting), m=separator
(https://i.imgur.com/rqlAQnT.png)

(https://i.imgur.com/vDjnsGp.png)