2022-04-20 09:59:44 -06:00
|
|
|
// command-prompt: A one-line command entry at the top of the screen for quick commands
|
2014-02-13 14:55:13 -07:00
|
|
|
|
|
|
|
#include "Core.h"
|
2022-04-20 09:59:44 -06:00
|
|
|
#include <ColorText.h>
|
2014-02-13 14:55:13 -07:00
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
2022-05-05 12:52:33 -06:00
|
|
|
#include <MiscUtils.h>
|
2014-02-13 14:55:13 -07:00
|
|
|
#include <PluginManager.h>
|
|
|
|
|
2014-05-14 16:56:30 -06:00
|
|
|
#include <modules/Gui.h>
|
2022-04-20 09:59:44 -06:00
|
|
|
#include <modules/Screen.h>
|
2014-02-13 14:55:13 -07:00
|
|
|
|
2014-03-07 09:52:06 -07:00
|
|
|
#include <list>
|
2022-04-20 09:59:44 -06:00
|
|
|
#include <set>
|
2022-05-05 12:52:33 -06:00
|
|
|
#include <string>
|
2014-03-07 09:52:06 -07:00
|
|
|
#include <utility>
|
2014-06-17 14:49:43 -06:00
|
|
|
#include <vector>
|
2014-02-13 14:55:13 -07:00
|
|
|
|
2022-04-20 09:59:44 -06:00
|
|
|
#include "df/enabler.h"
|
|
|
|
#include "df/graphic.h"
|
2014-02-13 14:55:13 -07:00
|
|
|
#include "df/interface_key.h"
|
|
|
|
#include "df/ui.h"
|
2014-06-09 14:50:01 -06:00
|
|
|
|
2014-02-13 14:55:13 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
2014-12-02 19:44:20 -07:00
|
|
|
DFHACK_PLUGIN("command-prompt");
|
|
|
|
REQUIRE_GLOBAL(ui);
|
|
|
|
REQUIRE_GLOBAL(gps);
|
|
|
|
REQUIRE_GLOBAL(enabler);
|
2014-02-13 14:55:13 -07:00
|
|
|
|
2014-06-17 14:49:43 -06:00
|
|
|
std::vector<std::string> command_history;
|
|
|
|
|
2014-03-07 09:52:06 -07:00
|
|
|
class viewscreen_commandpromptst;
|
|
|
|
class prompt_ostream:public buffered_color_ostream
|
|
|
|
{
|
|
|
|
viewscreen_commandpromptst *parent_;
|
|
|
|
protected:
|
|
|
|
void flush_proxy();
|
|
|
|
public:
|
2022-04-20 09:59:44 -06:00
|
|
|
prompt_ostream(viewscreen_commandpromptst* parent)
|
|
|
|
: parent_(parent)
|
|
|
|
{}
|
|
|
|
bool empty() { return buffer.empty(); }
|
2014-03-07 09:52:06 -07:00
|
|
|
};
|
2014-02-13 14:55:13 -07:00
|
|
|
class viewscreen_commandpromptst : public dfhack_viewscreen {
|
|
|
|
public:
|
|
|
|
void feed(std::set<df::interface_key> *events);
|
|
|
|
|
|
|
|
void logic() {
|
|
|
|
dfhack_viewscreen::logic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void render();
|
2022-04-20 09:59:44 -06:00
|
|
|
void help() {}
|
2014-06-17 10:34:57 -06:00
|
|
|
int8_t movies_okay() { return 0; }
|
2014-02-13 14:55:13 -07:00
|
|
|
|
2014-11-25 18:22:26 -07:00
|
|
|
df::unit* getSelectedUnit() { return Gui::getAnyUnit(parent); }
|
|
|
|
df::item* getSelectedItem() { return Gui::getAnyItem(parent); }
|
|
|
|
df::building* getSelectedBuilding() { return Gui::getAnyBuilding(parent); }
|
2018-05-12 11:02:52 -06:00
|
|
|
df::plant* getSelectedPlant() { return Gui::getAnyPlant(parent); }
|
2014-11-25 18:22:26 -07:00
|
|
|
|
2014-02-13 14:55:13 -07:00
|
|
|
std::string getFocusString() { return "commandprompt"; }
|
2022-04-20 09:59:44 -06:00
|
|
|
viewscreen_commandpromptst(std::string entry)
|
|
|
|
: submitted(false), is_response(false)
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
2022-04-20 09:59:44 -06:00
|
|
|
show_fps = gps->display_frames;
|
|
|
|
gps->display_frames = 0;
|
2015-11-05 17:09:35 -07:00
|
|
|
cursor_pos = entry.size();
|
2014-06-09 17:38:21 -06:00
|
|
|
frame = 0;
|
2014-06-17 14:49:43 -06:00
|
|
|
history_idx = command_history.size();
|
2014-06-17 15:21:30 -06:00
|
|
|
if (history_idx > 0)
|
|
|
|
{
|
|
|
|
if (command_history[history_idx - 1] == "")
|
|
|
|
{
|
|
|
|
command_history.pop_back();
|
|
|
|
history_idx--;
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 14:49:43 -06:00
|
|
|
command_history.push_back(entry);
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
|
|
|
~viewscreen_commandpromptst()
|
|
|
|
{
|
2022-04-20 09:59:44 -06:00
|
|
|
gps->display_frames = show_fps;
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
|
2014-05-15 13:51:03 -06:00
|
|
|
void add_response(color_value v, std::string s)
|
2014-03-07 09:52:06 -07:00
|
|
|
{
|
2014-05-15 13:51:03 -06:00
|
|
|
std::stringstream ss(s);
|
|
|
|
std::string part;
|
|
|
|
while (std::getline(ss, part))
|
|
|
|
{
|
2014-05-15 15:30:42 -06:00
|
|
|
responses.push_back(std::make_pair(v, part + '\n'));
|
2014-05-15 13:51:03 -06:00
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
}
|
2014-06-17 14:49:43 -06:00
|
|
|
std::string get_entry()
|
|
|
|
{
|
|
|
|
return command_history[history_idx];
|
|
|
|
}
|
|
|
|
void set_entry(std::string entry)
|
|
|
|
{
|
|
|
|
command_history[history_idx] = entry;
|
|
|
|
}
|
|
|
|
void back_word()
|
|
|
|
{
|
|
|
|
std::string entry = get_entry();
|
|
|
|
if (cursor_pos == 0)
|
|
|
|
return;
|
|
|
|
cursor_pos--;
|
|
|
|
while (cursor_pos > 0 && !isalnum(entry[cursor_pos]))
|
|
|
|
cursor_pos--;
|
|
|
|
while (cursor_pos > 0 && isalnum(entry[cursor_pos]))
|
|
|
|
cursor_pos--;
|
|
|
|
if (!isalnum(entry[cursor_pos]) && cursor_pos != 0)
|
|
|
|
cursor_pos++;
|
|
|
|
}
|
|
|
|
void forward_word()
|
|
|
|
{
|
|
|
|
std::string entry = get_entry();
|
|
|
|
int len = entry.size();
|
|
|
|
if (cursor_pos == len)
|
|
|
|
return;
|
|
|
|
cursor_pos++;
|
|
|
|
while (cursor_pos <= len && !isalnum(entry[cursor_pos]))
|
|
|
|
cursor_pos++;
|
|
|
|
while (cursor_pos <= len && isalnum(entry[cursor_pos]))
|
|
|
|
cursor_pos++;
|
|
|
|
if (cursor_pos > len)
|
|
|
|
cursor_pos = len;
|
|
|
|
}
|
|
|
|
|
2014-02-13 14:55:13 -07:00
|
|
|
protected:
|
2022-04-20 09:59:44 -06:00
|
|
|
std::list<std::pair<color_value, std::string> > responses;
|
2014-06-09 14:50:01 -06:00
|
|
|
int cursor_pos;
|
2014-06-17 14:49:43 -06:00
|
|
|
int history_idx;
|
2015-12-27 20:35:14 -07:00
|
|
|
bool submitted;
|
2014-03-07 09:52:06 -07:00
|
|
|
bool is_response;
|
2014-02-13 14:55:13 -07:00
|
|
|
bool show_fps;
|
2014-06-09 14:50:01 -06:00
|
|
|
int frame;
|
2014-02-13 14:55:13 -07:00
|
|
|
void submit();
|
|
|
|
};
|
2014-03-07 09:52:06 -07:00
|
|
|
void prompt_ostream::flush_proxy()
|
|
|
|
{
|
|
|
|
if (buffer.empty())
|
|
|
|
return;
|
2022-04-20 09:59:44 -06:00
|
|
|
for(auto it = buffer.begin(); it != buffer.end(); it++)
|
|
|
|
parent_->add_response(it->first, it->second);
|
2014-03-07 09:52:06 -07:00
|
|
|
buffer.clear();
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
void viewscreen_commandpromptst::render()
|
|
|
|
{
|
2014-06-09 14:50:01 -06:00
|
|
|
++frame;
|
2014-08-11 14:07:52 -06:00
|
|
|
if (frame >= enabler->gfps)
|
2014-06-09 14:50:01 -06:00
|
|
|
frame = 0;
|
2014-02-13 14:55:13 -07:00
|
|
|
if (Screen::isDismissed(this))
|
|
|
|
return;
|
|
|
|
|
|
|
|
dfhack_viewscreen::render();
|
|
|
|
|
|
|
|
auto dim = Screen::getWindowSize();
|
|
|
|
parent->render();
|
2022-04-20 09:59:44 -06:00
|
|
|
if (is_response)
|
2014-03-07 09:52:06 -07:00
|
|
|
{
|
2022-05-05 12:52:33 -06:00
|
|
|
int y = 0;
|
|
|
|
for (auto &response : responses)
|
2014-03-07 09:52:06 -07:00
|
|
|
{
|
2022-05-05 12:52:33 -06:00
|
|
|
std::vector<std::string> 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;
|
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
else
|
|
|
|
{
|
2014-06-17 14:49:43 -06:00
|
|
|
std::string entry = get_entry();
|
2022-04-20 09:59:44 -06:00
|
|
|
Screen::fillRect(Screen::Pen(' ', 7, 0), 0, 0, dim.x, 0);
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0, "[DFHack]#");
|
2014-08-11 14:07:52 -06:00
|
|
|
std::string cursor = (frame < enabler->gfps / 2) ? "_" : " ";
|
2022-04-20 09:59:44 -06:00
|
|
|
if (cursor_pos < dim.x - 10)
|
2014-06-09 14:50:01 -06:00
|
|
|
{
|
2022-04-20 09:59:44 -06:00
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 10, 0, entry);
|
2018-04-06 00:18:15 -06:00
|
|
|
if (int16_t(entry.size()) > dim.x - 10)
|
2014-06-09 14:50:01 -06:00
|
|
|
Screen::paintTile(Screen::Pen('\032', 7, 0), dim.x - 1, 0);
|
|
|
|
if (cursor != " ")
|
|
|
|
Screen::paintString(Screen::Pen(' ', 10, 0), 10 + cursor_pos, 0, cursor);
|
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
else
|
|
|
|
{
|
2014-06-09 14:50:01 -06:00
|
|
|
size_t start = cursor_pos - dim.x + 10 + 1;
|
|
|
|
Screen::paintTile(Screen::Pen('\033', 7, 0), 9, 0);
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 10, 0, entry.substr(start));
|
|
|
|
if (cursor != " ")
|
|
|
|
Screen::paintString(Screen::Pen(' ', 10, 0), dim.x - 1, 0, cursor);
|
2014-03-07 09:52:06 -07:00
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void viewscreen_commandpromptst::submit()
|
|
|
|
{
|
|
|
|
CoreSuspendClaimer suspend;
|
2022-04-20 09:59:44 -06:00
|
|
|
if (is_response)
|
2014-03-07 09:52:06 -07:00
|
|
|
{
|
|
|
|
Screen::dismiss(this);
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
if (submitted)
|
2015-12-27 20:35:14 -07:00
|
|
|
return;
|
|
|
|
submitted = true;
|
2014-03-07 09:52:06 -07:00
|
|
|
prompt_ostream out(this);
|
2018-06-20 11:08:35 -06:00
|
|
|
{
|
2021-03-21 18:42:50 -06:00
|
|
|
Screen::Hide hide_guard(this, Screen::Hide::RESTORE_AT_TOP);
|
2018-06-20 11:08:35 -06:00
|
|
|
Core::getInstance().runCommand(out, get_entry());
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
if (out.empty() && responses.empty())
|
2014-03-07 09:52:06 -07:00
|
|
|
Screen::dismiss(this);
|
|
|
|
else
|
|
|
|
{
|
2022-04-20 09:59:44 -06:00
|
|
|
is_response = true;
|
2014-03-07 09:52:06 -07:00
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
|
|
|
void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
|
|
|
|
{
|
2014-06-09 17:38:21 -06:00
|
|
|
int old_pos = cursor_pos;
|
2014-06-17 14:49:43 -06:00
|
|
|
std::string entry = get_entry();
|
2014-02-13 14:55:13 -07:00
|
|
|
bool leave_all = events->count(interface_key::LEAVESCREEN_ALL);
|
|
|
|
if (leave_all || events->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
events->clear();
|
|
|
|
Screen::dismiss(this);
|
|
|
|
if (leave_all)
|
|
|
|
{
|
|
|
|
events->insert(interface_key::LEAVESCREEN);
|
|
|
|
parent->feed(events);
|
|
|
|
events->clear();
|
|
|
|
}
|
2014-06-17 15:21:30 -06:00
|
|
|
//if (command_history.size() && !entry.size())
|
|
|
|
// command_history.pop_back();
|
2014-02-13 14:55:13 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-06-17 10:34:57 -06:00
|
|
|
if (events->count(interface_key::SELECT))
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
|
|
|
submit();
|
|
|
|
return;
|
|
|
|
}
|
2014-06-17 10:34:57 -06:00
|
|
|
if (is_response)
|
2014-03-07 09:52:06 -07:00
|
|
|
return;
|
2014-02-13 14:55:13 -07:00
|
|
|
for (auto it = events->begin(); it != events->end(); ++it)
|
|
|
|
{
|
|
|
|
auto key = *it;
|
2022-04-20 09:59:44 -06:00
|
|
|
if (key == interface_key::STRING_A000) //delete?
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
2022-04-20 09:59:44 -06:00
|
|
|
if (entry.size() && cursor_pos > 0)
|
2014-06-09 14:50:01 -06:00
|
|
|
{
|
|
|
|
entry.erase(cursor_pos - 1, 1);
|
|
|
|
cursor_pos--;
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
if (size_t(cursor_pos) > entry.size())
|
2014-06-09 14:50:01 -06:00
|
|
|
cursor_pos = entry.size();
|
2014-02-13 14:55:13 -07:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-11 04:23:19 -06:00
|
|
|
int charcode = Screen::keyToChar(key);
|
|
|
|
if (charcode > 0)
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
2014-08-11 04:23:19 -06:00
|
|
|
entry.insert(cursor_pos, 1, char(charcode));
|
2014-06-09 14:50:01 -06:00
|
|
|
cursor_pos++;
|
2014-06-17 14:49:43 -06:00
|
|
|
set_entry(entry);
|
2014-06-09 15:00:26 -06:00
|
|
|
return;
|
2014-06-09 14:50:01 -06:00
|
|
|
}
|
2014-06-09 15:00:26 -06:00
|
|
|
}
|
|
|
|
// Prevent number keys from moving cursor
|
2022-04-20 09:59:44 -06:00
|
|
|
if (events->count(interface_key::CURSOR_RIGHT))
|
2014-06-09 15:00:26 -06:00
|
|
|
{
|
|
|
|
cursor_pos++;
|
2018-04-06 00:18:15 -06:00
|
|
|
if (size_t(cursor_pos) > entry.size())
|
2014-06-09 17:38:21 -06:00
|
|
|
cursor_pos = entry.size();
|
2014-06-09 15:00:26 -06:00
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CURSOR_LEFT))
|
2014-06-09 15:00:26 -06:00
|
|
|
{
|
|
|
|
cursor_pos--;
|
2014-06-17 10:34:57 -06:00
|
|
|
if (cursor_pos < 0) cursor_pos = 0;
|
2014-06-09 15:00:26 -06:00
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CURSOR_RIGHT_FAST))
|
2014-06-17 14:49:43 -06:00
|
|
|
{
|
|
|
|
forward_word();
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CURSOR_LEFT_FAST))
|
2014-06-17 14:49:43 -06:00
|
|
|
{
|
|
|
|
back_word();
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CUSTOM_CTRL_A))
|
2014-06-09 15:00:26 -06:00
|
|
|
{
|
|
|
|
cursor_pos = 0;
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CUSTOM_CTRL_E))
|
2014-06-09 15:00:26 -06:00
|
|
|
{
|
|
|
|
cursor_pos = entry.size();
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CURSOR_UP))
|
2014-06-17 14:49:43 -06:00
|
|
|
{
|
|
|
|
history_idx--;
|
|
|
|
if (history_idx < 0)
|
|
|
|
history_idx = 0;
|
|
|
|
entry = get_entry();
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
}
|
2022-04-20 09:59:44 -06:00
|
|
|
else if (events->count(interface_key::CURSOR_DOWN))
|
2014-06-17 14:49:43 -06:00
|
|
|
{
|
2018-04-06 00:18:15 -06:00
|
|
|
if (size_t(history_idx) < command_history.size() - 1)
|
2014-06-17 14:49:43 -06:00
|
|
|
{
|
|
|
|
history_idx++;
|
2018-04-06 00:18:15 -06:00
|
|
|
if (size_t(history_idx) >= command_history.size())
|
2014-06-17 14:49:43 -06:00
|
|
|
history_idx = command_history.size() - 1;
|
|
|
|
entry = get_entry();
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_entry(entry);
|
2014-06-09 17:38:21 -06:00
|
|
|
if (old_pos != cursor_pos)
|
|
|
|
frame = 0;
|
2014-06-17 14:49:43 -06:00
|
|
|
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
2014-12-02 19:44:20 -07:00
|
|
|
|
2014-02-13 14:55:13 -07:00
|
|
|
command_result show_prompt(color_ostream &out, std::vector <std::string> & parameters)
|
|
|
|
{
|
2018-06-20 13:16:23 -06:00
|
|
|
if (Gui::getCurFocus(true) == "dfhack/commandprompt")
|
2014-05-14 16:56:30 -06:00
|
|
|
{
|
|
|
|
Screen::dismiss(Gui::getCurViewscreen(true));
|
2017-06-07 13:00:30 -06:00
|
|
|
return CR_OK;
|
2014-05-14 16:56:30 -06:00
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
std::string params;
|
2022-04-20 09:59:44 -06:00
|
|
|
for(size_t i = 0; i < parameters.size(); i++)
|
|
|
|
params += parameters[i] + " ";
|
2018-06-14 08:12:06 -06:00
|
|
|
Screen::show(dts::make_unique<viewscreen_commandpromptst>(params), plugin_self);
|
2014-02-13 14:55:13 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2022-11-09 15:34:39 -07:00
|
|
|
|
2022-04-20 09:59:44 -06:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands)
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
|
|
|
commands.push_back(PluginCommand(
|
2022-07-22 17:42:36 -06:00
|
|
|
"command-prompt",
|
2022-07-27 16:48:44 -06:00
|
|
|
"Allows you to run a DFHack command from in-game.",
|
2022-07-22 17:42:36 -06:00
|
|
|
show_prompt,
|
2022-11-09 15:34:39 -07:00
|
|
|
Gui::anywhere_hotkey));
|
2014-02-13 14:55:13 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:59:44 -06:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e)
|
2015-08-29 17:38:41 -06:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:59:44 -06:00
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream &out)
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
|
|
|
return CR_OK;
|
2014-08-11 14:07:52 -06:00
|
|
|
}
|