mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
If possible all errors are sent to stderr
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1055 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -69,6 +69,7 @@ void CLISetInterface::run() {
|
||||
running = true;
|
||||
while (running) {
|
||||
if (!cli.canGetLine()) break;
|
||||
handle_pending_errors();
|
||||
// show prompt
|
||||
if (!quiet) {
|
||||
cli << GRAY << _("> ") << NORMAL;
|
||||
@@ -153,7 +154,7 @@ void CLISetInterface::handleCommand(const String& command) {
|
||||
vector<ScriptParseError> errors;
|
||||
ScriptP script = parse(command,nullptr,false,errors);
|
||||
if (!errors.empty()) {
|
||||
FOR_EACH(error,errors) showError(error.what());
|
||||
FOR_EACH(error,errors) cli.showError(error.what());
|
||||
return;
|
||||
}
|
||||
// execute command
|
||||
@@ -164,10 +165,6 @@ void CLISetInterface::handleCommand(const String& command) {
|
||||
cli << result->toCode() << ENDL;
|
||||
}
|
||||
} catch (const Error& e) {
|
||||
showError(e.what());
|
||||
cli.showError(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void CLISetInterface::showError(const String& error) {
|
||||
cli << RED << _("ERROR: ") << NORMAL << replace_all(error,_("\n"),_("\n ")) << ENDL;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ class CLISetInterface : public SetView {
|
||||
void showWelcome();
|
||||
void showUsage();
|
||||
void handleCommand(const String& command);
|
||||
void showError(const String& error);
|
||||
|
||||
/// our own context, when no set is loaded
|
||||
Context& getContext();
|
||||
|
||||
+49
-19
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <cli/text_io_handler.hpp>
|
||||
#include <util/error.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Text I/O handler
|
||||
|
||||
@@ -17,22 +18,29 @@ const Char* PARAM = _("\x1B[33m");
|
||||
const Char* FILE_EXT = _("\x1B[0;1m");
|
||||
const Char* GRAY = _("\x1B[1;30m");
|
||||
const Char* RED = _("\x1B[1;31m");
|
||||
const Char* YELLOW = _("\x1B[1;33m");
|
||||
const Char* ENDL = _("\n");
|
||||
|
||||
TextIOHandler cli;
|
||||
|
||||
#ifdef __WXMSW__
|
||||
bool StdHandleOk(DWORD std_handle) {
|
||||
// GetStdHandle sometimes returns an invalid handle instead of INVALID_HANDLE_VALUE
|
||||
// check with GetHandleInformation
|
||||
HANDLE h = GetStdHandle(std_handle);
|
||||
DWORD flags;
|
||||
return GetHandleInformation(h,&flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
void TextIOHandler::init() {
|
||||
bool have_stderr;
|
||||
#ifdef __WXMSW__
|
||||
have_console = false;
|
||||
escapes = false;
|
||||
// Detect whether to use console output
|
||||
// GetStdHandle sometimes returns an invalid handle instead of INVALID_HANDLE_VALUE
|
||||
// check with GetHandleInformation
|
||||
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
DWORD flags;
|
||||
bool ok = GetHandleInformation(h,&flags);
|
||||
if (ok) have_console = true;
|
||||
have_console = StdHandleOk(STD_OUTPUT_HANDLE);
|
||||
have_stderr = StdHandleOk(STD_ERROR_HANDLE);
|
||||
// Detect the --color flag, indicating we should allow escapes
|
||||
if (have_console) {
|
||||
for (int i = 1 ; i < wxTheApp->argc ; ++i) {
|
||||
@@ -45,8 +53,14 @@ void TextIOHandler::init() {
|
||||
#else
|
||||
// always use console on *nix (?)
|
||||
have_console = true;
|
||||
escapes = true;
|
||||
have_stderr = true;
|
||||
escapes = true; // TODO: detect output redirection
|
||||
#endif
|
||||
stream = stdout;
|
||||
// always write to stderr if possible
|
||||
if (have_console) {
|
||||
write_errors_to_cli = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool TextIOHandler::haveConsole() const {
|
||||
@@ -56,20 +70,10 @@ bool TextIOHandler::haveConsole() const {
|
||||
|
||||
// ----------------------------------------------------------------------------- : Output
|
||||
|
||||
void TextIOHandler::flush() {
|
||||
if (have_console) {
|
||||
fflush(stdout);
|
||||
} else if (!buffer.empty()) {
|
||||
// Show message box
|
||||
wxMessageBox(buffer, _("Magic Set Editor"), wxOK | wxICON_INFORMATION);
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
TextIOHandler& TextIOHandler::operator << (const Char* str) {
|
||||
if (escapes || str[0] != 27) {
|
||||
if (have_console) {
|
||||
IF_UNICODE(wprintf,printf)(str);
|
||||
IF_UNICODE(fwprintf,fprintf)(stream,str);
|
||||
} else {
|
||||
buffer += str;
|
||||
}
|
||||
@@ -80,7 +84,7 @@ TextIOHandler& TextIOHandler::operator << (const Char* str) {
|
||||
TextIOHandler& TextIOHandler::operator << (const String& str) {
|
||||
if (escapes || str.empty() || str.GetChar(0) != 27) {
|
||||
if (have_console) {
|
||||
IF_UNICODE(wprintf,printf)(str.c_str());
|
||||
IF_UNICODE(fwprintf,fprintf)(stream,str.c_str());
|
||||
} else {
|
||||
buffer += str;
|
||||
}
|
||||
@@ -88,6 +92,16 @@ TextIOHandler& TextIOHandler::operator << (const String& str) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void TextIOHandler::flush() {
|
||||
if (have_console) {
|
||||
fflush(stream);
|
||||
} else if (!buffer.empty()) {
|
||||
// Show message box
|
||||
wxMessageBox(buffer, _("Magic Set Editor"), wxOK | wxICON_INFORMATION);
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Input
|
||||
|
||||
String TextIOHandler::getLine() {
|
||||
@@ -109,3 +123,19 @@ String TextIOHandler::getLine() {
|
||||
bool TextIOHandler::canGetLine() {
|
||||
return !feof(stdin);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Errors
|
||||
|
||||
void TextIOHandler::showError(const String& message) {
|
||||
stream = stdout;
|
||||
*this << RED << _("ERROR: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
|
||||
flush();
|
||||
stream = stdout;
|
||||
}
|
||||
|
||||
void TextIOHandler::showWarning(const String& message) {
|
||||
stream = stdout;
|
||||
*this << YELLOW << _("WARNING: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
|
||||
flush();
|
||||
stream = stdout;
|
||||
}
|
||||
|
||||
@@ -36,9 +36,15 @@ class TextIOHandler {
|
||||
/// Flush output
|
||||
void flush();
|
||||
|
||||
/// Show an error message
|
||||
void showError(const String& message);
|
||||
/// Show a warning message
|
||||
void showWarning(const String& message);
|
||||
|
||||
private:
|
||||
bool have_console;
|
||||
bool escapes;
|
||||
FILE* stream;
|
||||
String buffer; ///< Buffer when not writing to console
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user