Merge remote-tracking branch 'softmoth/command-prompt-wrap' into develop

develop
lethosor 2022-05-09 22:14:31 -04:00
commit 947098e564
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
5 changed files with 107 additions and 67 deletions

@ -1 +1 @@
Subproject commit 87d6ae018cb8d288d854f632e9d8d959d75d7db4 Subproject commit 99d06827848583232dd77afb34cd7ab589567086

@ -168,32 +168,58 @@ std::string to_search_normalized(const std::string &str)
return result; return result;
} }
bool word_wrap(std::vector<std::string> *out, const std::string &str, size_t line_length)
bool word_wrap(std::vector<std::string> *out, const std::string &str,
size_t line_length, bool collapse_whitespace)
{ {
out->clear(); if (line_length == 0)
std::istringstream input(str); line_length = SIZE_MAX;
std::string out_line;
std::string word; std::string line;
if (input >> word) size_t break_pos = 0;
for (auto &c : str)
{ {
out_line += word; if (c == '\n')
// size_t remaining = line_length - std::min(line_length, word.length());
while (input >> word)
{ {
if (out_line.length() + word.length() + 1 <= line_length) out->push_back(line);
line.clear();
break_pos = 0;
continue;
}
if (isspace(c))
{ {
out_line += ' '; if (break_pos == line.length() && collapse_whitespace)
out_line += word; 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)
{
// Break before last space, and skip that space
out->push_back(line.substr(0, break_pos - 1));
} }
else else
{ {
out->push_back(out_line); // Single word is too long, just break it
out_line = word; 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; return true;
} }

@ -391,7 +391,8 @@ DFHACK_EXPORT std::string to_search_normalized(const std::string &str);
DFHACK_EXPORT bool word_wrap(std::vector<std::string> *out, DFHACK_EXPORT bool word_wrap(std::vector<std::string> *out,
const std::string &str, 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) inline bool bits_match(unsigned required, unsigned ok, unsigned mask)
{ {

@ -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 "Core.h"
#include <ColorText.h>
#include <Console.h> #include <Console.h>
#include <Export.h> #include <Export.h>
#include <MiscUtils.h>
#include <PluginManager.h> #include <PluginManager.h>
#include <ColorText.h>
#include <modules/Screen.h>
#include <modules/Gui.h> #include <modules/Gui.h>
#include <modules/Screen.h>
#include <set>
#include <list> #include <list>
#include <set>
#include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "df/enabler.h"
#include "df/graphic.h"
#include "df/interface_key.h" #include "df/interface_key.h"
#include "df/ui.h" #include "df/ui.h"
#include "df/graphic.h"
#include "df/enabler.h"
using namespace DFHack; using namespace DFHack;
using namespace df::enums; using namespace df::enums;
@ -36,7 +38,9 @@ class prompt_ostream:public buffered_color_ostream
protected: protected:
void flush_proxy(); void flush_proxy();
public: public:
prompt_ostream(viewscreen_commandpromptst* parent):parent_(parent){} prompt_ostream(viewscreen_commandpromptst* parent)
: parent_(parent)
{}
bool empty() { return buffer.empty(); } bool empty() { return buffer.empty(); }
}; };
class viewscreen_commandpromptst : public dfhack_viewscreen { class viewscreen_commandpromptst : public dfhack_viewscreen {
@ -57,7 +61,8 @@ public:
df::plant* getSelectedPlant() { return Gui::getAnyPlant(parent); } df::plant* getSelectedPlant() { return Gui::getAnyPlant(parent); }
std::string getFocusString() { return "commandprompt"; } 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; show_fps = gps->display_frames;
gps->display_frames = 0; gps->display_frames = 0;
@ -156,12 +161,18 @@ void viewscreen_commandpromptst::render()
parent->render(); parent->render();
if (is_response) if (is_response)
{ {
auto it=responses.begin(); int y = 0;
for(int i=0;i<dim.y && it!=responses.end();i++,it++) for (auto &response : responses)
{ {
Screen::fillRect(Screen::Pen(' ', 7, 0),0,i,dim.x,i); std::vector<std::string> lines;
std::string cur_line=it->second; word_wrap(&lines, response.second, dim.x);
Screen::paintString(Screen::Pen(' ',it->first,0),0,i,cur_line.substr(0,cur_line.size()-1)); 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 else
@ -170,7 +181,7 @@ void viewscreen_commandpromptst::render()
Screen::fillRect(Screen::Pen(' ', 7, 0), 0, 0, dim.x, 0); Screen::fillRect(Screen::Pen(' ', 7, 0), 0, 0, dim.x, 0);
Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0, "[DFHack]#"); Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0, "[DFHack]#");
std::string cursor = (frame < enabler->gfps / 2) ? "_" : " "; 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) if (int16_t(entry.size()) > dim.x - 10)
@ -333,8 +344,10 @@ bool hotkey_allow_all(df::viewscreen *top)
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands)
{ {
commands.push_back(PluginCommand( commands.push_back(PluginCommand(
"command-prompt","Shows a command prompt on window.",show_prompt,hotkey_allow_all, "command-prompt", "Shows a command prompt on window.",
"command-prompt [entry] - shows a cmd prompt in df window. Entry is used for default prefix (e.g. ':lua')" 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; return CR_OK;
} }

@ -1 +1 @@
Subproject commit b808050d4a3885aa0e250e726708b2b28fe28260 Subproject commit 64626c81481a2f938f071b7347d07c10312b223d