[command-prompt] cosmetics: whitespace, sort headers

develop
Tim Siegel 2022-04-20 11:59:44 -04:00
parent 55e4008925
commit c3347d465f
No known key found for this signature in database
GPG Key ID: 856AC2B87AB8474E
1 changed files with 54 additions and 48 deletions

@ -1,23 +1,23 @@
//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 <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 <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,8 +36,10 @@ 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)
bool empty(){return buffer.empty();} : parent_(parent)
{}
bool empty() { return buffer.empty(); }
}; };
class viewscreen_commandpromptst : public dfhack_viewscreen { class viewscreen_commandpromptst : public dfhack_viewscreen {
public: public:
@ -48,7 +50,7 @@ public:
} }
void render(); void render();
void help() { } void help() {}
int8_t movies_okay() { return 0; } int8_t movies_okay() { return 0; }
df::unit* getSelectedUnit() { return Gui::getAnyUnit(parent); } df::unit* getSelectedUnit() { return Gui::getAnyUnit(parent); }
@ -57,10 +59,11 @@ 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;
cursor_pos = entry.size(); cursor_pos = entry.size();
frame = 0; frame = 0;
history_idx = command_history.size(); history_idx = command_history.size();
@ -76,7 +79,7 @@ public:
} }
~viewscreen_commandpromptst() ~viewscreen_commandpromptst()
{ {
gps->display_frames=show_fps; gps->display_frames = show_fps;
} }
void add_response(color_value v, std::string s) void add_response(color_value v, std::string s)
@ -125,7 +128,7 @@ public:
} }
protected: protected:
std::list<std::pair<color_value,std::string> > responses; std::list<std::pair<color_value, std::string> > responses;
int cursor_pos; int cursor_pos;
int history_idx; int history_idx;
bool submitted; bool submitted;
@ -138,8 +141,8 @@ void prompt_ostream::flush_proxy()
{ {
if (buffer.empty()) if (buffer.empty())
return; return;
for(auto it=buffer.begin();it!=buffer.end();it++) for(auto it = buffer.begin(); it != buffer.end(); it++)
parent_->add_response(it->first,it->second); parent_->add_response(it->first, it->second);
buffer.clear(); buffer.clear();
} }
void viewscreen_commandpromptst::render() void viewscreen_commandpromptst::render()
@ -154,25 +157,26 @@ void viewscreen_commandpromptst::render()
auto dim = Screen::getWindowSize(); auto dim = Screen::getWindowSize();
parent->render(); parent->render();
if(is_response) if (is_response)
{ {
auto it=responses.begin(); auto it = responses.begin();
for(int i=0;i<dim.y && it!=responses.end();i++,it++) for (int i = 0; i < dim.y && it != responses.end(); i++, it++)
{ {
Screen::fillRect(Screen::Pen(' ', 7, 0),0,i,dim.x,i); Screen::fillRect(Screen::Pen(' ', 7, 0), 0, i, dim.x, i);
std::string cur_line=it->second; std::string cur_line = it->second;
Screen::paintString(Screen::Pen(' ',it->first,0),0,i,cur_line.substr(0,cur_line.size()-1)); Screen::paintString(Screen::Pen(' ', it->first, 0), 0, i,
cur_line.substr(0, cur_line.size() - 1));
} }
} }
else else
{ {
std::string entry = get_entry(); std::string entry = get_entry();
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)
Screen::paintTile(Screen::Pen('\032', 7, 0), dim.x - 1, 0); Screen::paintTile(Screen::Pen('\032', 7, 0), dim.x - 1, 0);
if (cursor != " ") if (cursor != " ")
@ -191,12 +195,12 @@ void viewscreen_commandpromptst::render()
void viewscreen_commandpromptst::submit() void viewscreen_commandpromptst::submit()
{ {
CoreSuspendClaimer suspend; CoreSuspendClaimer suspend;
if(is_response) if (is_response)
{ {
Screen::dismiss(this); Screen::dismiss(this);
return; return;
} }
if(submitted) if (submitted)
return; return;
submitted = true; submitted = true;
prompt_ostream out(this); prompt_ostream out(this);
@ -204,11 +208,11 @@ void viewscreen_commandpromptst::submit()
Screen::Hide hide_guard(this, Screen::Hide::RESTORE_AT_TOP); Screen::Hide hide_guard(this, Screen::Hide::RESTORE_AT_TOP);
Core::getInstance().runCommand(out, get_entry()); Core::getInstance().runCommand(out, get_entry());
} }
if(out.empty() && responses.empty()) if (out.empty() && responses.empty())
Screen::dismiss(this); Screen::dismiss(this);
else else
{ {
is_response=true; is_response = true;
} }
} }
void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events) void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
@ -240,14 +244,14 @@ void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
for (auto it = events->begin(); it != events->end(); ++it) for (auto it = events->begin(); it != events->end(); ++it)
{ {
auto key = *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); entry.erase(cursor_pos - 1, 1);
cursor_pos--; cursor_pos--;
} }
if(size_t(cursor_pos) > entry.size()) if (size_t(cursor_pos) > entry.size())
cursor_pos = entry.size(); cursor_pos = entry.size();
continue; continue;
} }
@ -261,34 +265,34 @@ void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
} }
} }
// Prevent number keys from moving cursor // Prevent number keys from moving cursor
if(events->count(interface_key::CURSOR_RIGHT)) if (events->count(interface_key::CURSOR_RIGHT))
{ {
cursor_pos++; cursor_pos++;
if (size_t(cursor_pos) > entry.size()) if (size_t(cursor_pos) > entry.size())
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--; cursor_pos--;
if (cursor_pos < 0) cursor_pos = 0; 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(); forward_word();
} }
else if(events->count(interface_key::CURSOR_LEFT_FAST)) else if (events->count(interface_key::CURSOR_LEFT_FAST))
{ {
back_word(); back_word();
} }
else if(events->count(interface_key::CUSTOM_CTRL_A)) else if (events->count(interface_key::CUSTOM_CTRL_A))
{ {
cursor_pos = 0; 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(); cursor_pos = entry.size();
} }
else if(events->count(interface_key::CURSOR_UP)) else if (events->count(interface_key::CURSOR_UP))
{ {
history_idx--; history_idx--;
if (history_idx < 0) if (history_idx < 0)
@ -296,7 +300,7 @@ void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
entry = get_entry(); entry = get_entry();
cursor_pos = entry.size(); 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) if (size_t(history_idx) < command_history.size() - 1)
{ {
@ -321,8 +325,8 @@ command_result show_prompt(color_ostream &out, std::vector <std::string> & param
return CR_OK; return CR_OK;
} }
std::string params; std::string params;
for(size_t i=0;i<parameters.size();i++) for(size_t i = 0; i < parameters.size(); i++)
params+=parameters[i]+" "; params += parameters[i] + " ";
Screen::show(dts::make_unique<viewscreen_commandpromptst>(params), plugin_self); Screen::show(dts::make_unique<viewscreen_commandpromptst>(params), plugin_self);
return CR_OK; return CR_OK;
} }
@ -330,21 +334,23 @@ bool hotkey_allow_all(df::viewscreen *top)
{ {
return true; return true;
} }
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;
} }
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; return CR_OK;
} }
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) DFhackCExport command_result plugin_shutdown(color_ostream &out)
{ {
return CR_OK; return CR_OK;
} }