Files
integration/luprex/cpp/core/luaconsole.hpp

119 lines
3.6 KiB
C++
Raw Normal View History

2021-01-23 16:10:29 -05:00
//////////////////////////////////////////////////////
//
// LuaConsole:
//
2021-02-02 16:29:07 -05:00
// Used to parse commands that are being interactively typed
// in by a user. The common usage pattern is:
2021-01-23 20:12:21 -05:00
//
// 1. Print the prompt suggested by 'get_prompt'.
// 2. Read a line of text from stdin.
// 3. Add the line to the LuaConsole using 'add'.
2021-11-16 12:20:11 -05:00
// 4. Get the command word using get_command.
2021-11-04 14:49:25 -04:00
// 5. If the command is empty, do nothing.
2021-11-16 12:20:11 -05:00
// 6. If the command is nonempty, get the args and execute it.
2021-01-23 20:12:21 -05:00
//
2021-11-16 12:20:11 -05:00
// The LuaConsole expects you to type a lua command, or one
// of the following slash-commands:
2021-01-23 20:12:21 -05:00
//
2021-11-16 12:20:11 -05:00
// /quit - exit the program
// /view - display the nearby tangibles
// /menu [tanid] - display the menu for tangible
// /tick [timevalue] - advance the simulation clock
// /choose [number] - choose menu item number
2021-11-16 12:20:11 -05:00
// /1234 - choose menu item 1234
2021-02-02 16:29:07 -05:00
//
2021-11-16 12:20:11 -05:00
// If you type anything else, the LuaConsole will generate a
// syntax error. Note that not all of the commands above are
// supported in all interpreters.
//
// Lua commands can be one of the following:
//
// <expression> - invoke "<expression>""
// = <expression> - invoke "return <expression>"
// ? <expression> - probe "<expression>"
// ?= <expression> - probe "return <expression>"
//
2021-11-16 12:20:11 -05:00
// Once a command has been typed (or a syntax error has been
// typed), the LuaConsole will return the command. The command
// can be fetched using command(), int_arg(), and str_arg()
2021-01-23 20:12:21 -05:00
//
2021-01-23 16:10:29 -05:00
//////////////////////////////////////////////////////
#ifndef LUACONSOLE_HPP
#define LUACONSOLE_HPP
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
2021-01-23 16:10:29 -05:00
#include "luastack.hpp"
2022-03-02 14:52:51 -05:00
class LuaConsole : public eng::nevernew {
2021-01-23 16:10:29 -05:00
public:
using StringVec = eng::vector<eng::string>;
2021-02-02 16:29:07 -05:00
2021-01-23 16:10:29 -05:00
private:
lua_State *lua_state_;
eng::string raw_input_;
2021-01-23 16:10:29 -05:00
int lines_;
eng::string prompt_;
2021-11-16 12:20:11 -05:00
StringVec words_;
2021-01-23 16:10:29 -05:00
2021-11-04 14:49:25 -04:00
void clear_raw_input();
2021-02-02 16:29:07 -05:00
2021-01-23 16:10:29 -05:00
public:
LuaConsole();
~LuaConsole();
2021-11-04 14:49:25 -04:00
// Fetch the recommended prompt.
//
// You should update the prompt immediately after 'add'.
//
const eng::string &get_prompt() { return prompt_; }
2021-10-07 14:58:20 -04:00
2021-11-16 12:20:11 -05:00
// Get the command words.
//
2021-11-04 14:49:25 -04:00
StringVec get_command();
2021-11-16 12:20:11 -05:00
2021-02-02 16:29:07 -05:00
// Add a line of text that was just read from the console.
2021-11-04 14:49:25 -04:00
//
void add(eng::string line);
};
//////////////////////////////////////////////////////
//
// CommonCommands is parses slash-commands that are understood by
// lpxclient and lpxserver. That way, these two separate
// sourcefiles don't have to duplicate the code for parsing.
//
// After parsing, this module calls a virtual function
// to execute the command. lpxserver and lpxclient
// implement these virtual functions in different ways.
//
//////////////////////////////////////////////////////
class CommonCommands {
public:
using StringVec = eng::vector<eng::string>;
2021-02-02 16:29:07 -05:00
protected:
virtual void do_syntax_error(std::string_view error) = 0;
virtual void do_unknown_command(std::string_view name) = 0;
virtual void do_view_command() = 0;
virtual void do_moveto_command(int x, int y) = 0;
virtual void do_quit_command() = 0;
virtual void do_cpl_command() = 0;
virtual void do_work_command() = 0;
virtual void do_display_command() = 0;
virtual void do_aborthttp_command() = 0;
virtual void do_connect_command(std::string_view hostname) = 0;
virtual void do_luainvoke_command(std::string_view cmd) = 0;
virtual void do_luaprobe_command(std::string_view cmd) = 0;
public:
// Parse a command and call one of the virtual command functions above.
//
void do_command(const StringVec &command);
2021-01-23 16:10:29 -05:00
};
2021-01-23 20:12:21 -05:00
#endif // LUACONSOLE_HPP