/*
* px *
Copyright (c) 2008 Shawn Hladky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pxheaders.h"
#include "pxclientuser.h"
pxClientUser::pxClientUser(StrPtr* fmtString, Enviro* env)
{
formatString.Set(fmtString);
#ifdef OS_NT
// set the colors
if (env->Get("P4COLORS"))
{
useColor = true;
StrRef colors(env->Get("P4COLORS"));
if (colors.Length() == 3)
{
infoColor = getColor(colors.Text()[0]);
warnColor = getColor(colors.Text()[1]);
erroColor = getColor(colors.Text()[2]);
}
}
else
{
useColor = false;
}
#endif
}
void pxClientUser::Message(Error *err)
{
printMessage(err);
}
void pxClientUser::HandleError(Error* err)
{
printMessage(err);
}
void pxClientUser::OutputStat(StrDict *varList)
{
if (this->formatString.Length() > 0)
{
StrBuf formated;
StrOps::Expand(formated, formatString, *varList);
Error err;
err.Set(E_INFO, formated.Text());
Message(&err);
}
else
{
ClientUser::OutputStat(varList);
}
}
#ifdef OS_NT
void pxClientUser::printMessage(Error *err)
{
if (useColor)
{
StrBuf b;
err->Fmt(b, EF_NEWLINE);
const char* text = b.Text();
if (err->IsWarning())
{
printText(warnColor, text);
}
else if (err->IsInfo())
{
printText(infoColor, text);
}
else
{
printText(erroColor, text);
}
}
else
{
StrBuf b;
if( err->IsInfo() )
{
err->Fmt( b, EF_PLAIN );
OutputInfo( (char)err->GetGeneric() + '0', b.Text() );
}
else
{
err->Fmt(b, EF_NEWLINE);
OutputError(b.Text());
}
}
}
#else
void pxClientUser::printMessage(Error* err)
{
StrBuf b;
if( err->IsInfo() )
{
err->Fmt( b, EF_PLAIN );
OutputInfo( (char)err->GetGeneric() + '0', b.Text() );
}
else
{
err->Fmt(b, EF_NEWLINE);
OutputError(b.Text());
}
}
#endif
#ifdef OS_NT
/*
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
*/
WORD pxClientUser::getColor(char colorCharacter)
{
switch (colorCharacter)
{
case '0':
return 0x0000;
break;
case '1':
return 0x0001;
break;
case '2':
return 0x0002;
break;
case '3':
return 0x0003;
break;
case '4':
return 0x0004;
break;
case '5':
return 0x0005;
break;
case '6':
return 0x0006;
break;
case '7':
return 0x0007;
break;
case '8':
return 0x0008;
break;
case '9':
return 0x0009;
break;
case 'A':
return 0x000A;
break;
case 'B':
return 0x000B;
break;
case 'C':
return 0x000C;
break;
case 'D':
return 0x000D;
break;
case 'E':
return 0x000E;
break;
case 'F':
return 0x000F;
break;
}
return 0x000F;
}
void pxClientUser::printText(WORD color, const char* text)
{
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(h, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes ;
WORD newColor = (wOldColorAttrs & 0xFFF0) | color;
SetConsoleTextAttribute ( h, newColor ) ; //Set the new color information
printf(text);
SetConsoleTextAttribute ( h, wOldColorAttrs ); //Set the new color information
}
#endif