🚀 add colorful error info print.

This commit is contained in:
ValKmjolnir 2022-08-31 00:56:19 +08:00
parent 27ceeb517d
commit b022b25cea
5 changed files with 30 additions and 15 deletions

View File

@ -61,7 +61,7 @@ void logo()
<<" / \\/ / _` / __|/ _` | |\n"
<<" / /\\ / (_| \\__ \\ (_| | |\n"
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
<<"nasal ver : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"
<<"version : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"
<<"c++ std : "<<__cplusplus<<"\n"
<<"thanks to : https://github.com/andyross/nasal\n"
<<"code repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n"
@ -135,7 +135,7 @@ i32 main(i32 argc,const char* argv[])
{
string s(argv[1]);
if(s=="-v" || s=="--version")
logo();
std::clog<<"nasal "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n";
else if(s=="-h" || s=="--help")
help();
else if(s[0]!='-')

View File

@ -138,11 +138,7 @@ string rawstr(const string& str,const usize maxlen=0)
case '\v': ret+="\\v"; break;
case '\f': ret+="\\f"; break;
case '\r': ret+="\\r"; break;
#ifdef _MSC_VER
case '\033':ret+="\\e";break;
#else
case '\e': ret+="\\e"; break;
#endif
case '\"': ret+="\\\"";break;
case '\'': ret+="\\\'";break;
case '\\': ret+="\\\\";break;

View File

@ -6,6 +6,10 @@
#include <sstream> // MSVC need this to use std::getline
#include <cstring>
#ifdef _WIN32
#include <windows.h> // use SetConsoleTextAttribute
#endif
class fstreamline
{
protected:
@ -45,18 +49,36 @@ class nasal_err:public fstreamline
{
private:
u32 error;
void printstg(const char* stage)
{
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO scrinfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE),&scrinfo);
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x03);
std::cerr<<"[";
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x0c);
std::cerr<<stage;
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x03);
std::cerr<<"] ";
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),scrinfo.wAttributes);
#else
std::cerr<<"\033[36m[\033[91m"<<stage<<"\033[36m]\033[0m ";
#endif
}
public:
nasal_err():error(0){}
void err(const char* stage,const string& info)
{
++error;
std::cerr<<"["<<stage<<"] "<<info<<"\n";
printstg(stage);
std::cerr<<info<<"\n";
}
void err(const char* stage,u32 line,u32 column,const string& info)
{
++error;
const string& code=res[line-1];
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
printstg(stage);
std::cerr<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
for(i32 i=0;i<(i32)column-1;++i)
std::cerr<<char(" \t"[code[i]=='\t']);
std::cerr<<"^\n";
@ -64,7 +86,8 @@ public:
void err(const char* stage,u32 line,const string& info)
{
++error;
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
printstg(stage);
std::cerr<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
}
void chkerr(){if(error)std::exit(1);}
};

View File

@ -298,11 +298,7 @@ string nasal_lexer::str_gen()
case '0': str+='\0'; break;
case 'a': str+='\a'; break;
case 'b': str+='\b'; break;
#ifdef _MSC_VER
case 'e': str+='\033'; break;
#else
case 'e': str+='\e'; break;
#endif
case 't': str+='\t'; break;
case 'n': str+='\n'; break;
case 'v': str+='\v'; break;

View File

@ -18,7 +18,7 @@ var prt=func(s,path){
foreach(var j;s)
print("\e[34m",j,"\e[0m");
if(unix.isdir(path~"/"~f)){
println("\e[34m",i==last?" └─":" ├─","\e[0m\e[36m[",f,"]>\e[0m");
println("\e[34m",i==last?" └─":" ├─","\e[0m\e[33m[",f,"]\e[36m>\e[0m");
append(s,i==last?" ":" │ ");
prt(s,path~"/"~f);
pop(s);
@ -32,5 +32,5 @@ var prt=func(s,path){
if(os.platform()=="windows")
system("chcp 65001");
println("\e[36m[",unix.getcwd(),"]>\e[0m");
println("\e[33m[",unix.getcwd(),"]\e[36m>\e[0m");
prt([""],".");