Browse Source

Adjust jessibot for object::string_type change

master
Jessica James 3 years ago
parent
commit
3a9d8ee278
  1. 11
      src/bot/base_commands.cpp
  2. 8
      src/bot/console/console.cpp
  3. 6
      src/bot/console/console_command_context.cpp
  4. 2
      src/bot/main.cpp
  5. 6
      src/common/io/command.cpp
  6. 38
      src/common/io/command_context.cpp
  7. 4
      src/common/io/message.cpp
  8. 2
      src/include/jessilib/io/ansi/ansi_text.hpp
  9. 8
      src/include/jessilib/io/command.hpp
  10. 19
      src/include/jessilib/io/command_context.hpp
  11. 7
      src/include/jessilib/io/message.hpp
  12. 4
      src/include/jessilib/unicode.hpp

11
src/bot/base_commands.cpp

@ -24,21 +24,22 @@ namespace jessibot {
namespace io { namespace io {
using namespace jessilib::io; using namespace jessilib::io;
using namespace std::literals;
command quit_command{ [](command_context& context) { command quit_command{ [](command_context& context) {
using namespace jessilib::io; using namespace jessilib::io;
text quit_text{ "Closing jessibot", text::property::bold, color{ 0xFF0000 } }; // TODO: localize text quit_text{ u8"Closing jessibot", text::property::bold, color{ 0xFF0000 } }; // TODO: localize
context.publicReply(formatted_message{ "{}", quit_text }); context.publicReply(formatted_message{ "{}", quit_text });
notify_shutdown(); notify_shutdown();
}, "quit" }; }, u8"quit" };
// ISSUE: help command has no way to know what commands exist for the given context // ISSUE: help command has no way to know what commands exist for the given context
command help_command{ [](command_context& context) { command help_command{ [](command_context& context) {
auto details = context.details(); auto details = context.details();
auto table_name = details["table"].get<std::string>(); auto table_name = details[u8"table"s].get<std::u8string>();
if (table_name.empty()) { if (table_name.empty()) {
text error_text{ "ERROR", text::property::bold, color{ 0xFF0000 } }; // TODO: localize text error_text{ u8"ERROR", text::property::bold, color{ 0xFF0000 } }; // TODO: localize
context.publicReply(formatted_message{ "{} command context is missing permission table name", error_text }); context.publicReply(formatted_message{ "{} command context is missing permission table name", error_text });
return; return;
} }
@ -54,7 +55,7 @@ command help_command{ [](command_context& context) {
context.publicReply(formatted_message{ "{}", in_command->label() }); context.publicReply(formatted_message{ "{}", in_command->label() });
return true; return true;
}); });
}, "help" }; }, u8"help" };
} // namespace io } // namespace io
} // namespace jessibot } // namespace jessibot

8
src/bot/console/console.cpp

@ -30,13 +30,13 @@ namespace io {
void console_input_loop() { void console_input_loop() {
using namespace jessilib::io; using namespace jessilib::io;
std::string input; std::wstring input;
auto shutdown_future = get_shutdown_future(); auto shutdown_future = get_shutdown_future();
while (shutdown_future.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready) { while (shutdown_future.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready) {
std::getline(std::cin, input); // TODO: use a non-bloicking call and poll running periodically std::getline(std::wcin, input); // TODO: use a non-bloicking call and poll running periodically?
jessibot::io::console_command_context context{ input }; jessibot::io::console_command_context context{ jessilib::string_cast<char8_t>(input) };
if (!command_manager::instance().execute_command(context)) { if (!command_manager::instance().execute_command(context)) {
text error_text{ "ERROR", text::property::bold, color{ 0xFF0000 }}; text error_text{ u8"ERROR", text::property::bold, color{ 0xFF0000 }};
text keyword_text{ context.keyword(), text::property::bold, color{ 0x0000FF }}; text keyword_text{ context.keyword(), text::property::bold, color{ 0x0000FF }};
auto result = process_message<ansi::text_wrapper>(formatted_message{"{} Command \"{}\" not found", error_text, keyword_text}); auto result = process_message<ansi::text_wrapper>(formatted_message{"{} Command \"{}\" not found", error_text, keyword_text});
std::cout << result << std::endl; std::cout << result << std::endl;

6
src/bot/console/console_command_context.cpp

@ -27,7 +27,9 @@ namespace io {
/** Reply */ /** Reply */
bool console_command_context::privateReply(const jessilib::io::formatted_message& in_message) { bool console_command_context::privateReply(const jessilib::io::formatted_message& in_message) {
auto result = jessilib::io::process_message<jessilib::io::ansi::text_wrapper>(in_message); auto result = jessilib::io::process_message<jessilib::io::ansi::text_wrapper>(in_message);
std::cout << result << std::endl; // TODO check locale before printing to see if console is using UTF-8; if so we can just chuck this straight to cout
// instead of leveraging to wchar_t
std::wcout << jessilib::string_cast<wchar_t>(result) << std::endl;
return true; return true;
} }
@ -39,7 +41,7 @@ bool console_command_context::publicReply(const jessilib::io::formatted_message&
/** Additional contextual details */ /** Additional contextual details */
jessilib::object console_command_context::details() const { jessilib::object console_command_context::details() const {
static jessilib::object s_details { static jessilib::object s_details {
jessilib::object::map_type{ { "table", "console" } } jessilib::object::map_type{ { u8"table", u8"console" } }
}; };
return s_details; return s_details;

2
src/bot/main.cpp

@ -24,7 +24,7 @@
int main(int argc, char** argv) { int main(int argc, char** argv) {
jessilib::app_parameters parameters{ argc, argv }; jessilib::app_parameters parameters{ argc, argv };
if (parameters.has_switch("echoParameters")) { if (parameters.has_switch(u8"echoParameters")) {
// TODO: Write pretty JSON serializer based on JSON serializer // TODO: Write pretty JSON serializer based on JSON serializer
std::cout << std::endl << jessilib::json_parser{}.serialize(parameters) << std::endl; std::cout << std::endl << jessilib::json_parser{}.serialize(parameters) << std::endl;
} }

6
src/common/io/command.cpp

@ -22,7 +22,7 @@
namespace jessilib { namespace jessilib {
namespace io { namespace io {
command::command(callback_t in_callback, std::string_view in_label) command::command(callback_t in_callback, std::u8string_view in_label)
: m_callback{ std::move(in_callback) }, : m_callback{ std::move(in_callback) },
m_label{ std::move(in_label) } { m_label{ std::move(in_label) } {
// Register command // Register command
@ -34,7 +34,7 @@ command::~command() {
command_manager::instance().unregister_command(*this); command_manager::instance().unregister_command(*this);
} }
std::string_view command::label() const { std::u8string_view command::label() const {
return m_label; return m_label;
} }
@ -44,7 +44,7 @@ void command::execute(command_context& in_context) const {
command test_command{ [](command_context&) { command test_command{ [](command_context&) {
// do stuff // do stuff
}, "test" }; }, u8"test" };
} // namespace io } // namespace io
} // namespace jessilib } // namespace jessilib

38
src/common/io/command_context.cpp

@ -21,52 +21,54 @@
namespace jessilib { namespace jessilib {
namespace io { namespace io {
command_context::command_context(std::string_view in_input) command_context::command_context(string_type in_input)
: m_input{ in_input } { : m_input{ std::move(in_input) } {
std::u8string_view input_view = m_input;
// Strip leading whitespace // Strip leading whitespace
std::size_t pos = in_input.find_first_not_of(' '); std::size_t pos = input_view.find_first_not_of(u8' ');
if (pos != std::string_view::npos) { if (pos != std::string_view::npos) {
in_input.remove_prefix(pos); input_view.remove_prefix(pos);
// Whitespace is stripped; parse keyword from input // Whitespace is stripped; parse keyword from input
pos = in_input.find(' '); pos = input_view.find(u8' ');
if (pos == std::string_view::npos) { if (pos == std::string_view::npos) {
pos = in_input.size(); pos = input_view.size();
} }
m_keyword = in_input.substr(0, pos); m_keyword = input_view.substr(0, pos);
in_input.remove_prefix(pos); input_view.remove_prefix(pos);
// Strip leading whitespace and parse parameter from remaining input // Strip leading whitespace and parse parameter from remaining input
pos = in_input.find_first_not_of(' '); pos = input_view.find_first_not_of(u8' ');
if (pos != std::string_view::npos) { if (pos != std::string_view::npos) {
in_input.remove_prefix(pos); input_view.remove_prefix(pos);
m_parameter = in_input; m_parameter = input_view;
} }
} }
} }
command_context::command_context(std::string_view in_input, std::string_view in_keyword, std::string_view in_parameter) command_context::command_context(string_type in_input, string_type in_keyword, string_type in_parameter)
: m_input{ std::move(in_input) }, : m_input{ std::move(in_input) },
m_keyword{ std::move(in_keyword) }, m_keyword{ std::move(in_keyword) },
m_parameter{ std::move(in_parameter) } { m_parameter{ std::move(in_parameter) } {
// Empty ctor body // Empty ctor body
} }
std::string_view command_context::input() const { const command_context::string_type& command_context::input() const {
return m_input; return m_input;
} }
std::string_view command_context::keyword() const { const command_context::string_type& command_context::keyword() const {
return m_keyword; return m_keyword;
} }
std::string_view command_context::parameter() const { const command_context::string_type& command_context::parameter() const {
return m_parameter; return m_parameter;
} }
std::vector<std::string_view> command_context::paramaters() const { std::vector<std::u8string_view> command_context::paramaters() const {
std::vector<std::string_view> result; std::vector<std::u8string_view> result;
std::string_view parameter = m_parameter; std::u8string_view parameter = m_parameter;
// Strip leading whitespace // Strip leading whitespace
std::size_t pos = parameter.find_first_not_of(' '); std::size_t pos = parameter.find_first_not_of(' ');

4
src/common/io/message.cpp

@ -65,11 +65,11 @@ void text::set_color_bg(color in_color) {
/** Text */ /** Text */
const std::string& text::string() const { const std::u8string& text::string() const {
return m_string; return m_string;
} }
void text::set_string(std::string_view in_string) { void text::set_string(std::u8string_view in_string) {
m_string = in_string; m_string = in_string;
} }

2
src/include/jessilib/io/ansi/ansi_text.hpp

@ -100,7 +100,7 @@ inline std::string text_to_string<ansi::text_wrapper>(const ansi::text_wrapper&
} }
// Append textual string // Append textual string
result += in_text.string(); result += jessilib::ustring_to_mbstring(std::u8string_view{in_text.string()}).second;
// Reset (if needed) // Reset (if needed)
if (in_text.properties() != text::property::normal) { if (in_text.properties() != text::property::normal) {

8
src/include/jessilib/io/command.hpp

@ -28,7 +28,7 @@ namespace io {
class basic_command { class basic_command {
public: public:
virtual ~basic_command() = default; virtual ~basic_command() = default;
virtual std::string_view label() const = 0; virtual std::u8string_view label() const = 0;
virtual void execute(command_context& in_context) const = 0; virtual void execute(command_context& in_context) const = 0;
}; // class basic_command }; // class basic_command
@ -45,20 +45,20 @@ public:
command& operator=(command&&) = delete; command& operator=(command&&) = delete;
// Instantiates and self-registers a command // Instantiates and self-registers a command
command(callback_t in_callback, std::string_view in_label); command(callback_t in_callback, std::u8string_view in_label);
// Cleans up and unregisters the command // Cleans up and unregisters the command
virtual ~command(); virtual ~command();
// Unique label associated with command // Unique label associated with command
virtual std::string_view label() const override; virtual std::u8string_view label() const override;
// Executes the command // Executes the command
virtual void execute(command_context& in_context) const override; virtual void execute(command_context& in_context) const override;
private: private:
callback_t m_callback; callback_t m_callback;
std::string_view m_label; std::u8string_view m_label;
}; // class command }; // class command
} // namespace io } // namespace io

19
src/include/jessilib/io/command_context.hpp

@ -27,14 +27,15 @@ namespace io {
class command_context { class command_context {
public: public:
command_context(std::string_view in_input); using string_type = std::u8string;
command_context(std::string_view in_input, std::string_view in_keyword, std::string_view in_parameter); command_context(string_type in_input);
command_context(string_type in_input, string_type in_keyword, string_type in_parameter);
/** User input */ /** User input */
std::string_view input() const; const string_type& input() const;
std::string_view keyword() const; const string_type& keyword() const;
std::string_view parameter() const; const string_type& parameter() const;
std::vector<std::string_view> paramaters() const; std::vector<std::u8string_view> paramaters() const;
/** Reply */ /** Reply */
virtual bool privateReply(const formatted_message& in_message) = 0; // Reply to invoker privately (i.e: PM) virtual bool privateReply(const formatted_message& in_message) = 0; // Reply to invoker privately (i.e: PM)
@ -45,9 +46,9 @@ public:
virtual std::string getText(std::string_view tag) const = 0; // Get localized text virtual std::string getText(std::string_view tag) const = 0; // Get localized text
private: private:
std::string_view m_input; string_type m_input;
std::string_view m_keyword; string_type m_keyword;
std::string_view m_parameter; string_type m_parameter;
}; // class command_context }; // class command_context
} // namespace io } // namespace io

7
src/include/jessilib/io/message.hpp

@ -23,6 +23,7 @@
#include <string_view> #include <string_view>
#include <type_traits> #include <type_traits>
#include <fmt/format.h> #include <fmt/format.h>
#include "jessilib/unicode.hpp"
#include "color.hpp" #include "color.hpp"
namespace jessilib { namespace jessilib {
@ -114,11 +115,11 @@ public:
void set_color_bg(color in_color); void set_color_bg(color in_color);
/** Text */ /** Text */
const std::string& string() const; const std::u8string& string() const;
void set_string(std::string_view in_string); void set_string(std::u8string_view in_string);
private: private:
std::string m_string; std::u8string m_string;
property m_properties{ property::normal }; property m_properties{ property::normal };
color m_color{}; color m_color{};
color m_color_bg{}; color m_color_bg{};

4
src/include/jessilib/unicode.hpp

@ -220,7 +220,9 @@ std::pair<bool, std::string> ustring_to_mbstring(std::basic_string_view<CharT> i
std::mbstate_t mbstate{}; std::mbstate_t mbstate{};
decode_result decode; decode_result decode;
while ((decode = decode_codepoint(in_string).units != 0)) { while ((decode = decode_codepoint(in_string)).units != 0) {
in_string.remove_prefix(decode.units);
char buffer[MB_CUR_MAX]; // MB_LEN_MAX char buffer[MB_CUR_MAX]; // MB_LEN_MAX
size_t bytes_written = std::c32rtomb(buffer, decode.codepoint, &mbstate); size_t bytes_written = std::c32rtomb(buffer, decode.codepoint, &mbstate);
if (bytes_written > MB_CUR_MAX) { if (bytes_written > MB_CUR_MAX) {

Loading…
Cancel
Save