mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-12 21:47:00 -04:00
Added raw mode to simplify the interface with other programs
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1056 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -65,11 +65,11 @@ void CLISetInterface::onChangeSet() {
|
|||||||
void CLISetInterface::run() {
|
void CLISetInterface::run() {
|
||||||
// show welcome logo
|
// show welcome logo
|
||||||
if (!quiet) showWelcome();
|
if (!quiet) showWelcome();
|
||||||
|
handle_pending_errors();
|
||||||
// loop
|
// loop
|
||||||
running = true;
|
running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
if (!cli.canGetLine()) break;
|
if (!cli.canGetLine()) break;
|
||||||
handle_pending_errors();
|
|
||||||
// show prompt
|
// show prompt
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
cli << GRAY << _("> ") << NORMAL;
|
cli << GRAY << _("> ") << NORMAL;
|
||||||
@@ -79,6 +79,7 @@ void CLISetInterface::run() {
|
|||||||
String command = cli.getLine();
|
String command = cli.getLine();
|
||||||
handleCommand(command);
|
handleCommand(command);
|
||||||
cli.flush();
|
cli.flush();
|
||||||
|
cli.flushRaw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+40
-10
@@ -56,7 +56,9 @@ void TextIOHandler::init() {
|
|||||||
have_stderr = true;
|
have_stderr = true;
|
||||||
escapes = true; // TODO: detect output redirection
|
escapes = true; // TODO: detect output redirection
|
||||||
#endif
|
#endif
|
||||||
|
// write to standard output
|
||||||
stream = stdout;
|
stream = stdout;
|
||||||
|
raw_mode = false;
|
||||||
// always write to stderr if possible
|
// always write to stderr if possible
|
||||||
if (have_console) {
|
if (have_console) {
|
||||||
write_errors_to_cli = true;
|
write_errors_to_cli = true;
|
||||||
@@ -71,8 +73,8 @@ bool TextIOHandler::haveConsole() const {
|
|||||||
// ----------------------------------------------------------------------------- : Output
|
// ----------------------------------------------------------------------------- : Output
|
||||||
|
|
||||||
TextIOHandler& TextIOHandler::operator << (const Char* str) {
|
TextIOHandler& TextIOHandler::operator << (const Char* str) {
|
||||||
if (escapes || str[0] != 27) {
|
if ((escapes && !raw_mode) || str[0] != 27) {
|
||||||
if (have_console) {
|
if (have_console && !raw_mode) {
|
||||||
IF_UNICODE(fwprintf,fprintf)(stream,str);
|
IF_UNICODE(fwprintf,fprintf)(stream,str);
|
||||||
} else {
|
} else {
|
||||||
buffer += str;
|
buffer += str;
|
||||||
@@ -82,17 +84,11 @@ TextIOHandler& TextIOHandler::operator << (const Char* str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TextIOHandler& TextIOHandler::operator << (const String& str) {
|
TextIOHandler& TextIOHandler::operator << (const String& str) {
|
||||||
if (escapes || str.empty() || str.GetChar(0) != 27) {
|
return *this << str.c_str();
|
||||||
if (have_console) {
|
|
||||||
IF_UNICODE(fwprintf,fprintf)(stream,str.c_str());
|
|
||||||
} else {
|
|
||||||
buffer += str;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextIOHandler::flush() {
|
void TextIOHandler::flush() {
|
||||||
|
if (raw_mode) return;
|
||||||
if (have_console) {
|
if (have_console) {
|
||||||
fflush(stream);
|
fflush(stream);
|
||||||
} else if (!buffer.empty()) {
|
} else if (!buffer.empty()) {
|
||||||
@@ -124,6 +120,38 @@ bool TextIOHandler::canGetLine() {
|
|||||||
return !feof(stdin);
|
return !feof(stdin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------- : Raw mode
|
||||||
|
|
||||||
|
void TextIOHandler::enableRaw() {
|
||||||
|
raw_mode = true;
|
||||||
|
raw_mode_status = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextIOHandler::flushRaw() {
|
||||||
|
if (!raw_mode) return;
|
||||||
|
// always end in a newline
|
||||||
|
if (!buffer.empty() && buffer.GetChar(buffer.size()-1) != _('\n')) {
|
||||||
|
buffer += _('\n');
|
||||||
|
}
|
||||||
|
// count newlines
|
||||||
|
int newline_count = 0;
|
||||||
|
FOR_EACH_CONST(c,buffer) if (c==_('\n')) newline_count++;
|
||||||
|
// write record
|
||||||
|
printf("%d\n%d\n", raw_mode_status, newline_count);
|
||||||
|
if (!buffer.empty()) {
|
||||||
|
#ifdef UNICODE
|
||||||
|
wxCharBuffer buf = buffer.mb_str(wxConvUTF8);
|
||||||
|
puts(buf);
|
||||||
|
#else
|
||||||
|
puts(buffer.c_str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
fflush(stdout);
|
||||||
|
// clear
|
||||||
|
buffer.clear();
|
||||||
|
raw_mode_status = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Errors
|
// ----------------------------------------------------------------------------- : Errors
|
||||||
|
|
||||||
void TextIOHandler::showError(const String& message) {
|
void TextIOHandler::showError(const String& message) {
|
||||||
@@ -131,6 +159,7 @@ void TextIOHandler::showError(const String& message) {
|
|||||||
*this << RED << _("ERROR: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
|
*this << RED << _("ERROR: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
|
||||||
flush();
|
flush();
|
||||||
stream = stdout;
|
stream = stdout;
|
||||||
|
if (raw_mode) raw_mode_status = max(raw_mode_status, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextIOHandler::showWarning(const String& message) {
|
void TextIOHandler::showWarning(const String& message) {
|
||||||
@@ -138,4 +167,5 @@ void TextIOHandler::showWarning(const String& message) {
|
|||||||
*this << YELLOW << _("WARNING: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
|
*this << YELLOW << _("WARNING: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
|
||||||
flush();
|
flush();
|
||||||
stream = stdout;
|
stream = stdout;
|
||||||
|
if (raw_mode) raw_mode_status = max(raw_mode_status, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,11 +41,19 @@ class TextIOHandler {
|
|||||||
/// Show a warning message
|
/// Show a warning message
|
||||||
void showWarning(const String& message);
|
void showWarning(const String& message);
|
||||||
|
|
||||||
|
/// Enable raw mode
|
||||||
|
void enableRaw();
|
||||||
|
/// Output a single raw-mode record
|
||||||
|
/// Has no effect unless enableRaw() was called
|
||||||
|
void flushRaw();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool have_console;
|
bool have_console;
|
||||||
bool escapes;
|
bool escapes;
|
||||||
FILE* stream;
|
FILE* stream;
|
||||||
String buffer; ///< Buffer when not writing to console
|
String buffer; ///< Buffer when not writing to console
|
||||||
|
bool raw_mode;
|
||||||
|
int raw_mode_status;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The global TextIOHandler object
|
/// The global TextIOHandler object
|
||||||
|
|||||||
+13
-1
@@ -191,9 +191,18 @@ int MSE::OnRun() {
|
|||||||
cli << _("\n \tIMAGE is the same format as for 'export all card images'.");
|
cli << _("\n \tIMAGE is the same format as for 'export all card images'.");
|
||||||
cli << _("\n\n ") << BRIGHT << _("--cli") << NORMAL << _(" [")
|
cli << _("\n\n ") << BRIGHT << _("--cli") << NORMAL << _(" [")
|
||||||
<< PARAM << _("FILE") << NORMAL << _("] [")
|
<< PARAM << _("FILE") << NORMAL << _("] [")
|
||||||
<< BRIGHT << _("--quiet") << NORMAL << _("]");
|
<< BRIGHT << _("--quiet") << NORMAL << _("] [")
|
||||||
|
<< BRIGHT << _("--raw") << NORMAL << _("]");
|
||||||
cli << _("\n \tStart the command line interface for performing commands on the set file.");
|
cli << _("\n \tStart the command line interface for performing commands on the set file.");
|
||||||
cli << _("\n \tUse ") << BRIGHT << _("-q") << NORMAL << _(" or ") << BRIGHT << _("--quiet") << NORMAL << _(" to supress the startup banner and prompts.");
|
cli << _("\n \tUse ") << BRIGHT << _("-q") << NORMAL << _(" or ") << BRIGHT << _("--quiet") << NORMAL << _(" to supress the startup banner and prompts.");
|
||||||
|
cli << _("\n \tUse ") << BRIGHT << _("-raw") << NORMAL << _(" for raw output mode.");
|
||||||
|
cli << _("\n\nRaw output mode is intended for use by other programs:");
|
||||||
|
cli << _("\n - The only output is only in response to commands.");
|
||||||
|
cli << _("\n - For each command a single 'record' is written to the standard output.");
|
||||||
|
cli << _("\n - The record consists of:");
|
||||||
|
cli << _("\n - A line with an integer status code, 0 for ok, 1 for warnings, 2 for errors");
|
||||||
|
cli << _("\n - A line containing an integer k, the number of lines to follow");
|
||||||
|
cli << _("\n - k lines, each containing UTF-8 encoded string data.");
|
||||||
cli << ENDL;
|
cli << ENDL;
|
||||||
cli.flush();
|
cli.flush();
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
@@ -214,6 +223,9 @@ int MSE::OnRun() {
|
|||||||
set = import_set(arg);
|
set = import_set(arg);
|
||||||
} else if (arg == _("-q") || arg == _("--quiet")) {
|
} else if (arg == _("-q") || arg == _("--quiet")) {
|
||||||
quiet = true;
|
quiet = true;
|
||||||
|
} else if (arg == _("-r") || arg == _("--raw")) {
|
||||||
|
quiet = true;
|
||||||
|
cli.enableRaw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CLISetInterface cli_interface(set,quiet);
|
CLISetInterface cli_interface(set,quiet);
|
||||||
|
|||||||
Reference in New Issue
Block a user