diff --git a/depends/luacov b/depends/luacov index 87d6ae018..99d068278 160000 --- a/depends/luacov +++ b/depends/luacov @@ -1 +1 @@ -Subproject commit 87d6ae018cb8d288d854f632e9d8d959d75d7db4 +Subproject commit 99d06827848583232dd77afb34cd7ab589567086 diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 56af85afe..6ab63d5eb 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -168,32 +168,58 @@ std::string to_search_normalized(const std::string &str) return result; } -bool word_wrap(std::vector *out, const std::string &str, size_t line_length) + +bool word_wrap(std::vector *out, const std::string &str, + size_t line_length, bool collapse_whitespace) { - out->clear(); - std::istringstream input(str); - std::string out_line; - std::string word; - if (input >> word) + if (line_length == 0) + line_length = SIZE_MAX; + + std::string line; + size_t break_pos = 0; + + for (auto &c : str) { - out_line += word; - // size_t remaining = line_length - std::min(line_length, word.length()); - while (input >> word) + if (c == '\n') + { + out->push_back(line); + line.clear(); + break_pos = 0; + continue; + } + + if (isspace(c)) { - if (out_line.length() + word.length() + 1 <= line_length) + if (break_pos == line.length() && collapse_whitespace) + continue; + + line.push_back(collapse_whitespace ? ' ' : c); + break_pos = line.length(); + } + else { + line.push_back(c); + } + + if (line.length() > line_length) + { + if (break_pos > 0) { - out_line += ' '; - out_line += word; + // Break before last space, and skip that space + out->push_back(line.substr(0, break_pos - 1)); } else { - out->push_back(out_line); - out_line = word; + // Single word is too long, just break it + out->push_back(line.substr(0, line_length)); + break_pos = line_length; } + line = line.substr(break_pos); + break_pos = 0; } - if (out_line.length()) - out->push_back(out_line); } + if (line.length()) + out->push_back(line); + return true; } diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 7a5f050a2..764b11413 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -391,7 +391,8 @@ DFHACK_EXPORT std::string to_search_normalized(const std::string &str); DFHACK_EXPORT bool word_wrap(std::vector *out, const std::string &str, - size_t line_length = 80); + size_t line_length = 80, + bool collapse_whitespace = false); inline bool bits_match(unsigned required, unsigned ok, unsigned mask) { diff --git a/plugins/command-prompt.cpp b/plugins/command-prompt.cpp index a987ea829..ba2fe0e87 100644 --- a/plugins/command-prompt.cpp +++ b/plugins/command-prompt.cpp @@ -1,23 +1,25 @@ -//command-prompt a one line command entry at the top of the screen for quick commands +// command-prompt: A one-line command entry at the top of the screen for quick commands #include "Core.h" +#include #include #include +#include #include -#include -#include #include +#include -#include #include +#include +#include #include #include +#include "df/enabler.h" +#include "df/graphic.h" #include "df/interface_key.h" #include "df/ui.h" -#include "df/graphic.h" -#include "df/enabler.h" using namespace DFHack; using namespace df::enums; @@ -36,8 +38,10 @@ class prompt_ostream:public buffered_color_ostream protected: void flush_proxy(); public: - prompt_ostream(viewscreen_commandpromptst* parent):parent_(parent){} - bool empty(){return buffer.empty();} + prompt_ostream(viewscreen_commandpromptst* parent) + : parent_(parent) + {} + bool empty() { return buffer.empty(); } }; class viewscreen_commandpromptst : public dfhack_viewscreen { public: @@ -48,7 +52,7 @@ public: } void render(); - void help() { } + void help() {} int8_t movies_okay() { return 0; } df::unit* getSelectedUnit() { return Gui::getAnyUnit(parent); } @@ -57,10 +61,11 @@ public: df::plant* getSelectedPlant() { return Gui::getAnyPlant(parent); } std::string getFocusString() { return "commandprompt"; } - viewscreen_commandpromptst(std::string entry):submitted(false), is_response(false) + viewscreen_commandpromptst(std::string entry) + : submitted(false), is_response(false) { - show_fps=gps->display_frames; - gps->display_frames=0; + show_fps = gps->display_frames; + gps->display_frames = 0; cursor_pos = entry.size(); frame = 0; history_idx = command_history.size(); @@ -76,7 +81,7 @@ public: } ~viewscreen_commandpromptst() { - gps->display_frames=show_fps; + gps->display_frames = show_fps; } void add_response(color_value v, std::string s) @@ -125,7 +130,7 @@ public: } protected: - std::list > responses; + std::list > responses; int cursor_pos; int history_idx; bool submitted; @@ -138,8 +143,8 @@ void prompt_ostream::flush_proxy() { if (buffer.empty()) return; - for(auto it=buffer.begin();it!=buffer.end();it++) - parent_->add_response(it->first,it->second); + for(auto it = buffer.begin(); it != buffer.end(); it++) + parent_->add_response(it->first, it->second); buffer.clear(); } void viewscreen_commandpromptst::render() @@ -154,25 +159,31 @@ void viewscreen_commandpromptst::render() auto dim = Screen::getWindowSize(); parent->render(); - if(is_response) + if (is_response) { - auto it=responses.begin(); - for(int i=0;isecond; - Screen::paintString(Screen::Pen(' ',it->first,0),0,i,cur_line.substr(0,cur_line.size()-1)); + std::vector lines; + word_wrap(&lines, response.second, dim.x); + for (auto &line : lines) + { + Screen::fillRect(Screen::Pen(' ', 7, 0), 0, y, dim.x, y); + Screen::paintString(Screen::Pen(' ', response.first, 0), 0, y, line); + if (++y >= dim.y) + return; + } } } else { std::string entry = get_entry(); - Screen::fillRect(Screen::Pen(' ', 7, 0),0,0,dim.x,0); - Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0,"[DFHack]#"); + Screen::fillRect(Screen::Pen(' ', 7, 0), 0, 0, dim.x, 0); + Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0, "[DFHack]#"); std::string cursor = (frame < enabler->gfps / 2) ? "_" : " "; - if(cursor_pos < (dim.x - 10)) + if (cursor_pos < dim.x - 10) { - Screen::paintString(Screen::Pen(' ', 7, 0), 10,0 , entry); + Screen::paintString(Screen::Pen(' ', 7, 0), 10, 0, entry); if (int16_t(entry.size()) > dim.x - 10) Screen::paintTile(Screen::Pen('\032', 7, 0), dim.x - 1, 0); if (cursor != " ") @@ -191,12 +202,12 @@ void viewscreen_commandpromptst::render() void viewscreen_commandpromptst::submit() { CoreSuspendClaimer suspend; - if(is_response) + if (is_response) { Screen::dismiss(this); return; } - if(submitted) + if (submitted) return; submitted = true; prompt_ostream out(this); @@ -204,11 +215,11 @@ void viewscreen_commandpromptst::submit() Screen::Hide hide_guard(this, Screen::Hide::RESTORE_AT_TOP); Core::getInstance().runCommand(out, get_entry()); } - if(out.empty() && responses.empty()) + if (out.empty() && responses.empty()) Screen::dismiss(this); else { - is_response=true; + is_response = true; } } void viewscreen_commandpromptst::feed(std::set *events) @@ -240,14 +251,14 @@ void viewscreen_commandpromptst::feed(std::set *events) for (auto it = events->begin(); it != events->end(); ++it) { auto key = *it; - if (key==interface_key::STRING_A000) //delete? + if (key == interface_key::STRING_A000) //delete? { - if(entry.size() && cursor_pos > 0) + if (entry.size() && cursor_pos > 0) { entry.erase(cursor_pos - 1, 1); cursor_pos--; } - if(size_t(cursor_pos) > entry.size()) + if (size_t(cursor_pos) > entry.size()) cursor_pos = entry.size(); continue; } @@ -261,34 +272,34 @@ void viewscreen_commandpromptst::feed(std::set *events) } } // Prevent number keys from moving cursor - if(events->count(interface_key::CURSOR_RIGHT)) + if (events->count(interface_key::CURSOR_RIGHT)) { cursor_pos++; if (size_t(cursor_pos) > entry.size()) cursor_pos = entry.size(); } - else if(events->count(interface_key::CURSOR_LEFT)) + else if (events->count(interface_key::CURSOR_LEFT)) { cursor_pos--; if (cursor_pos < 0) cursor_pos = 0; } - else if(events->count(interface_key::CURSOR_RIGHT_FAST)) + else if (events->count(interface_key::CURSOR_RIGHT_FAST)) { forward_word(); } - else if(events->count(interface_key::CURSOR_LEFT_FAST)) + else if (events->count(interface_key::CURSOR_LEFT_FAST)) { back_word(); } - else if(events->count(interface_key::CUSTOM_CTRL_A)) + else if (events->count(interface_key::CUSTOM_CTRL_A)) { cursor_pos = 0; } - else if(events->count(interface_key::CUSTOM_CTRL_E)) + else if (events->count(interface_key::CUSTOM_CTRL_E)) { cursor_pos = entry.size(); } - else if(events->count(interface_key::CURSOR_UP)) + else if (events->count(interface_key::CURSOR_UP)) { history_idx--; if (history_idx < 0) @@ -296,7 +307,7 @@ void viewscreen_commandpromptst::feed(std::set *events) entry = get_entry(); cursor_pos = entry.size(); } - else if(events->count(interface_key::CURSOR_DOWN)) + else if (events->count(interface_key::CURSOR_DOWN)) { if (size_t(history_idx) < command_history.size() - 1) { @@ -321,8 +332,8 @@ command_result show_prompt(color_ostream &out, std::vector & param return CR_OK; } std::string params; - for(size_t i=0;i(params), plugin_self); return CR_OK; } @@ -330,21 +341,23 @@ bool hotkey_allow_all(df::viewscreen *top) { return true; } -DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) +DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { commands.push_back(PluginCommand( - "command-prompt","Shows a command prompt on window.",show_prompt,hotkey_allow_all, - "command-prompt [entry] - shows a cmd prompt in df window. Entry is used for default prefix (e.g. ':lua')" + "command-prompt", "Shows a command prompt on window.", + show_prompt, hotkey_allow_all, + "command-prompt [entry] - shows a cmd prompt in df window." + " Entry is used for default prefix (e.g. ':lua')" )); return CR_OK; } -DFhackCExport command_result plugin_onstatechange (color_ostream &out, state_change_event e) +DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e) { return CR_OK; } -DFhackCExport command_result plugin_shutdown ( color_ostream &out ) +DFhackCExport command_result plugin_shutdown(color_ostream &out) { return CR_OK; } diff --git a/scripts b/scripts index b808050d4..64626c814 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit b808050d4a3885aa0e250e726708b2b28fe28260 +Subproject commit 64626c81481a2f938f071b7347d07c10312b223d