Commit 89eecc3c authored by twanvl's avatar twanvl

Beginnings of a simple command line interface

parent 5b0671c2
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/error.hpp>
#include <cli/cli_main.hpp>
#include <cli/text_io_handler.hpp>
// ----------------------------------------------------------------------------- : Command line interface
CLISetInterface::CLISetInterface()
: quiet(false)
{
if (!cli.haveConsole()) {
throw Error(_("Can not run command line interface without a console;\nstart MSE with \"mse.com --cli\""));
}
run();
}
void CLISetInterface::run() {
// show welcome logo
if (!quiet) showWelcome();
// loop
running = true;
while (running) {
// show prompt
if (!quiet) {
cli << GRAY << _("> ") << NORMAL;
cli.flush();
}
// read line from stdin
String command = cli.getLine();
handleCommand(command);
}
}
void CLISetInterface::showWelcome() {
cli << _(" ___ \n")
_(" __ __ _ ___ _ ___ _ _ _ |__ \\ \n")
_(" | \\/ |__ _ __ _(_)__ / __|___| |_ | __|__| (_) |_ ___ _ _ ) |\n")
_(" | |\\/| / _` / _` | / _| \\__ | -_) _| | _|/ _` | | _/ _ \\ '_| / / \n")
_(" |_| |_\\__,_\\__, |_\\__| |___|___|\\__| |___\\__,_|_|\\__\\___/_| / /_ \n")
_(" |___/ |____|\n\n");
cli.flush();
}
void CLISetInterface::handleCommand(const String& command) {
if (command.empty()) {
// empty, ignore
} else if (command == _(":q") || command == _(":quit")) {
if (!quiet) {
cli << _("Goodbye\n"); cli.flush();
}
running = false;
} else if (command == _(":?") || command == _(":help")) {
// TODO show help
} else if (command == _("exit") || command == _("quit")) {
cli << _("Use :quit to quit\n"); cli.flush();
} else if (command.GetChar(0) == _(':')) {
cli << _("Unknown command, type :help for help.\n"); cli.flush();
} else {
// execute command
// TODO
cli << _("You said:\n") << command << ENDL; cli.flush();
}
}
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_CLI_MAIN
#define HEADER_CLI_MAIN
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
// ----------------------------------------------------------------------------- : Command line interface
class CLISetInterface {
public:
CLISetInterface();
private:
bool quiet;
bool running;
void run();
void showWelcome();
void handleCommand(const String& command);
};
// ----------------------------------------------------------------------------- : EOF
#endif
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <cli/text_io_handler.hpp>
// ----------------------------------------------------------------------------- : Text I/O handler
const Char* BRIGHT = _("\x1B[1m");
const Char* NORMAL = _("\x1B[0m");
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* ENDL = _("\n");
TextIOHandler cli;
void TextIOHandler::init() {
#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;
// Detect the --color flag, indicating we should allow escapes
if (have_console) {
for (int i = 1 ; i < wxTheApp->argc ; ++i) {
if (String(wxTheApp->argv[i]) == _("--color")) {
escapes = true;
break;
}
}
}
#else
// always use console on *nix (?)
have_console = true;
escapes = true;
#endif
}
bool TextIOHandler::haveConsole() const {
return have_console;
}
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);
} else {
buffer += str;
}
}
return *this;
}
TextIOHandler& TextIOHandler::operator << (const String& str) {
if (escapes || str.empty() || str.GetChar(0) != 27) {
if (have_console) {
IF_UNICODE(wprintf,printf)(str.c_str());
} else {
buffer += str;
}
}
return *this;
}
String TextIOHandler::getLine() {
String result;
Char buffer[2048];
while (true) {
if (!IF_UNICODE(fgetws,fgets)(buffer, 2048, stdin)) {
return result; // error
}
result += buffer;
if (result.GetChar(result.size()-1) == _('\n')) {
// drop newline, done
result.resize(result.size() - 1);
return result;
}
}
}
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_CLI_TEXT_IO_HANDLER
#define HEADER_CLI_TEXT_IO_HANDLER
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
// ----------------------------------------------------------------------------- : Text I/O handler
// color codes
extern const Char *BRIGHT, *NORMAL, *PARAM, *FILE_EXT, *GRAY, *RED, *ENDL;
/// Command line input / output handler
class TextIOHandler {
public:
void init();
/// Do we have a console to read/write from/to?
bool haveConsole() const;
/// Output text to the console
TextIOHandler& operator << (const Char*);
TextIOHandler& operator << (const String&);
/// Read a line from stdin
String getLine();
/// Flush output
void flush();
private:
bool have_console;
bool escapes;
String buffer; ///< Buffer when not writing to console
};
/// The global TextIOHandler object
extern TextIOHandler cli;
// ----------------------------------------------------------------------------- : EOF
#endif
...@@ -41,6 +41,7 @@ struct Transfer { ...@@ -41,6 +41,7 @@ struct Transfer {
DWORD WINAPI TransferThread(Transfer*); DWORD WINAPI TransferThread(Transfer*);
BOOL WINAPI HandleCtrlEvent(DWORD type); BOOL WINAPI HandleCtrlEvent(DWORD type);
void InitEscapeTranslation(HANDLE console);
void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]); void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]);
/// The child process /// The child process
...@@ -59,7 +60,7 @@ int main(int argc, char** argv) { ...@@ -59,7 +60,7 @@ int main(int argc, char** argv) {
// determine whether we need to wrap console i/o // determine whether we need to wrap console i/o
bool need_redirection = false; bool need_redirection = false;
for (int i = 1 ; i < argc ; ++i) { for (int i = 1 ; i < argc ; ++i) {
for (int j = 1 ; j < sizeof(redirect_flags)/sizeof(redirect_flags[0]) ; ++j) { for (int j = 0 ; j < sizeof(redirect_flags)/sizeof(redirect_flags[0]) ; ++j) {
if (strcmp(argv[i],redirect_flags[j]) == 0) { if (strcmp(argv[i],redirect_flags[j]) == 0) {
need_redirection = true; need_redirection = true;
goto break_2; goto break_2;
...@@ -116,6 +117,7 @@ int main(int argc, char** argv) { ...@@ -116,6 +117,7 @@ int main(int argc, char** argv) {
in_real = GetStdHandle(STD_INPUT_HANDLE); in_real = GetStdHandle(STD_INPUT_HANDLE);
out_real = GetStdHandle(STD_OUTPUT_HANDLE); out_real = GetStdHandle(STD_OUTPUT_HANDLE);
err_real = GetStdHandle(STD_ERROR_HANDLE); err_real = GetStdHandle(STD_ERROR_HANDLE);
InitEscapeTranslation(out_real);
// start threads // start threads
Transfer tranfer_in = {in_real, in_mine, false}; Transfer tranfer_in = {in_real, in_mine, false};
...@@ -149,9 +151,6 @@ int main(int argc, char** argv) { ...@@ -149,9 +151,6 @@ int main(int argc, char** argv) {
return exit_code; return exit_code;
} }
// ----------------------------------------------------------------------------- : Running
// ----------------------------------------------------------------------------- : Terminating // ----------------------------------------------------------------------------- : Terminating
/// Handle Ctrl+C /// Handle Ctrl+C
...@@ -189,10 +188,10 @@ void CopyFileBufferWithEscape(HANDLE output, char* buffer, DWORD size, bool hand ...@@ -189,10 +188,10 @@ void CopyFileBufferWithEscape(HANDLE output, char* buffer, DWORD size, bool hand
} }
DWORD pos = 0; DWORD pos = 0;
while (pos < size) { while (pos < size) {
// find next escape code, "\27[" // find next escape code, "\x1B["
DWORD next_pos = pos; DWORD next_pos = pos;
while (next_pos < size && while (next_pos < size &&
(buffer[next_pos] != '\27' || (buffer[next_pos] != '\x1B' ||
(next_pos + 1 >= size || buffer[next_pos+1] != '['))) ++next_pos; (next_pos + 1 >= size || buffer[next_pos+1] != '['))) ++next_pos;
// copy part before next escape // copy part before next escape
CopyFileBuffer(output, buffer+pos, next_pos-pos); CopyFileBuffer(output, buffer+pos, next_pos-pos);
...@@ -236,6 +235,13 @@ DWORD WINAPI TransferThread(Transfer* transfer) { ...@@ -236,6 +235,13 @@ DWORD WINAPI TransferThread(Transfer* transfer) {
WORD original_attributes; WORD original_attributes;
/// Initialization for escape translation
void InitEscapeTranslation(HANDLE console) {
CONSOLE_SCREEN_BUFFER_INFO screen_buffer;
GetConsoleScreenBufferInfo(console, &screen_buffer);
original_attributes = screen_buffer.wAttributes;
}
/// Perform an escape code /// Perform an escape code
void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]) { void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]) {
switch (command) { switch (command) {
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <data/locale.hpp> #include <data/locale.hpp>
#include <data/installer.hpp> #include <data/installer.hpp>
#include <data/format/formats.hpp> #include <data/format/formats.hpp>
#include <cli/cli_main.hpp>
#include <cli/text_io_handler.hpp>
#include <gui/welcome_window.hpp> #include <gui/welcome_window.hpp>
#include <gui/update_checker.hpp> #include <gui/update_checker.hpp>
#include <gui/images_export_window.hpp> #include <gui/images_export_window.hpp>
...@@ -102,6 +104,7 @@ int MSE::OnRun() { ...@@ -102,6 +104,7 @@ int MSE::OnRun() {
wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker
init_script_variables(); init_script_variables();
init_file_formats(); init_file_formats();
cli.init();
package_manager.init(); package_manager.init();
settings.read(); settings.read();
the_locale = Locale::byName(settings.locale); the_locale = Locale::byName(settings.locale);
...@@ -155,33 +158,51 @@ int MSE::OnRun() { ...@@ -155,33 +158,51 @@ int MSE::OnRun() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else if (arg == _("--help") || arg == _("-?")) { } else if (arg == _("--help") || arg == _("-?")) {
// command line help // command line help
write_stdout( String(_("Magic Set Editor\n\n")) cli << _("Magic Set Editor\n\n");
+ _("Usage: ") + argv[0] + _(" [OPTIONS]\n\n") cli << _("Usage: ") << BRIGHT << argv[0] << NORMAL << _(" [") << PARAM << _("OPTIONS") << NORMAL << _("]");
+ _(" no options\n") cli << _("\n\n no options");
+ _(" \tStart the MSE user interface showing the welcome window.\n\n") cli << _("\n \tStart the MSE user interface showing the welcome window.");
+ _(" -? or --help\n") cli << _("\n\n ") << BRIGHT << _("-?") << NORMAL << _(", ")
+ _(" \tShows this help screen.\n\n") << BRIGHT << _("--help") << NORMAL;
+ _(" -v or --version\n") cli << _("\n \tShows this help screen.");
+ _(" \tShow version information.\n\n") cli << _("\n\n ") << BRIGHT << _("-v") << NORMAL << _(", ")
+ _(" FILE.mse-set, FILE.set, FILE.mse\n") << BRIGHT << _("--version") << NORMAL;
+ _(" \tLoad the set file in the MSE user interface.\n\n") cli << _("\n \tShow version information.");
+ _(" FILE.mse-symbol\n") cli << _("\n\n ") << PARAM << _("FILE") << FILE_EXT << _(".mse-set") << NORMAL << _(", ")
+ _(" \tLoad the symbol into the MSE symbol editor.\n\n") << PARAM << _("FILE") << FILE_EXT << _(".set") << NORMAL << _(", ")
+ _(" FILE.mse-installer [--local]\n") << PARAM << _("FILE") << FILE_EXT << _(".mse") << NORMAL;
+ _(" \tInstall the packages from the installer.\n") cli << _("\n \tLoad the set file in the MSE user interface.");
+ _(" \tIf the --local flag is passed, install packages for this user only.\n\n") cli << _("\n\n ") << PARAM << _("FILE") << FILE_EXT << _(".mse-symbol") << NORMAL;
+ _(" --symbol-editor\n") cli << _("\n \tLoad the symbol into the MSE symbol editor.");
+ _(" \tShow the symbol editor instead of the welcome window.\n\n") cli << _("\n\n ") << BRIGHT << _("FILE") << FILE_EXT << _(".mse-installer")
+ _(" --create-installer [OUTFILE.mse-installer] [PACKAGE [PACKAGE ...]]\n") << NORMAL << _(" [") << BRIGHT << _("--local") << NORMAL << _("]");
+ _(" \tCreate an instaler, containing the listed packages.\n") cli << _("\n \tInstall the packages from the installer.");
+ _(" \tIf no filename is specified, the name of the first package is used.\n\n") cli << _("\n \tIf the ") << BRIGHT << _("--local") << NORMAL << _(" flag is passed, install packages for this user only.");
+ _(" --export FILE [IMAGE]\n") cli << _("\n\n ") << BRIGHT << _("--symbol-editor") << NORMAL;
+ _(" \tExport the cards in a set to image files,\n") cli << _("\n \tShow the symbol editor instead of the welcome window.");
+ _(" \tIMAGE is the same format as for 'export all card images'.\n") ); cli << _("\n\n ") << BRIGHT << _("--create-installer") << NORMAL << _(" [")
<< PARAM << _("OUTFILE") << FILE_EXT << _(".mse-installer") << NORMAL << _("] [")
<< PARAM << _("PACKAGE") << NORMAL << _(" [") << PARAM << _("PACKAGE") << NORMAL << _(" ...]]");
cli << _("\n \tCreate an instaler, containing the listed packages.");
cli << _("\n \tIf no output filename is specified, the name of the first package is used.");
cli << _("\n\n ") << BRIGHT << _("--export") << NORMAL << PARAM << _(" FILE") << NORMAL << _(" [") << PARAM << _("IMAGE") << NORMAL << _("]");
cli << _("\n \tExport the cards in a set to image files,");
cli << _("\n \tIMAGE is the same format as for 'export all card images'.");
cli << _("\n\n ") << BRIGHT << _("--cli") << NORMAL << _(" [")
<< PARAM << _("FILE") << NORMAL << _("]");
cli << _("\n \tStart the command line interface for performing commands on the set file.");
cli << ENDL;
cli.flush();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else if (arg == _("--version") || arg == _("-v")) { } else if (arg == _("--version") || arg == _("-v")) {
// dump version // dump version
write_stdout( _("Magic Set Editor\nVersion ") + app_version.toString() + version_suffix ); cli << _("Magic Set Editor\n");
cli << _("Version ") << app_version.toString() << version_suffix << ENDL;
cli.flush();
return EXIT_SUCCESS;
} else if (arg == _("--cli")) {
// command line interface
CLISetInterface cli_interface;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else if (arg == _("--export")) { } else if (arg == _("--export")) {
if (argc <= 2) { if (argc <= 2) {
......
...@@ -3819,6 +3819,18 @@ ...@@ -3819,6 +3819,18 @@
<Filter <Filter
Name="cli" Name="cli"
Filter=""> Filter="">
<File
RelativePath=".\cli\cli_main.cpp">
</File>
<File
RelativePath=".\cli\cli_main.hpp">
</File>
<File
RelativePath=".\cli\text_io_handler.cpp">
</File>
<File
RelativePath=".\cli\text_io_handler.hpp">
</File>
</Filter> </Filter>
<File <File
RelativePath=".\code_template.cpp"> RelativePath=".\code_template.cpp">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment