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

90 lines
2.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"
class LuaConsole {
public:
2021-02-02 16:29:07 -05:00
using StringVec = std::vector<std::string>;
2021-01-23 16:10:29 -05:00
private:
lua_State *lua_state_;
2021-02-02 16:29:07 -05:00
std::string raw_input_;
2021-01-23 16:10:29 -05:00
int lines_;
2021-10-07 14:58:20 -04:00
std::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-11-16 12:20:11 -05:00
void simplify(const StringVec &words);
void synerr(const std::string &msg);
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 std::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.
//
// Note that the command words may not be exactly what
// the user typed. Typically, the LuaConsole simplifies
// the command before returning it to the caller.
//
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
//
2021-01-23 16:10:29 -05:00
void add(std::string line);
2021-02-02 16:29:07 -05:00
2021-01-23 16:10:29 -05:00
};
2021-01-23 20:12:21 -05:00
#endif // LUACONSOLE_HPP