Wrap script descriptions in `ls` output and remove description length cap

develop
lethosor 2016-06-14 21:24:27 -04:00
parent a947586cf0
commit 022a1bf9e8
4 changed files with 56 additions and 10 deletions

@ -618,6 +618,27 @@ string getBuiltinCommand(std::string cmd)
return builtin; return builtin;
} }
void ls_helper(color_ostream &con, const string &name, const string &desc)
{
const size_t help_line_length = 80 - 22 - 5;
const string padding = string(80 - help_line_length, ' ');
vector<string> lines;
con.print(" %-22s - ", name.c_str());
word_wrap(&lines, desc, help_line_length);
// print first line, then any additional lines preceded by padding
for (size_t i = 0; i < lines.size(); i++)
con.print("%s%s\n", i ? padding.c_str() : "", lines[i].c_str());
}
void ls_helper(color_ostream &con, const PluginCommand &pcmd)
{
if (pcmd.isHotkeyCommand())
con.color(COLOR_CYAN);
ls_helper(con, pcmd.name, pcmd.description);
con.reset_color();
}
command_result Core::runCommand(color_ostream &con, const std::string &first_, vector<string> &parts) command_result Core::runCommand(color_ostream &con, const std::string &first_, vector<string> &parts)
{ {
std::string first = first_; std::string first = first_;
@ -846,11 +867,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v
} }
else for (size_t j = 0; j < plug->size();j++) else for (size_t j = 0; j < plug->size();j++)
{ {
const PluginCommand & pcmd = (plug->operator[](j)); ls_helper(con, plug->operator[](j));
if (pcmd.isHotkeyCommand())
con.color(COLOR_CYAN);
con.print(" %-22s - %s\n",pcmd.name.c_str(), pcmd.description.c_str());
con.reset_color();
} }
} }
else else
@ -891,7 +908,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v
{ {
if ((*iter).recolor) if ((*iter).recolor)
con.color(COLOR_CYAN); con.color(COLOR_CYAN);
con.print(" %-22s- %s\n",(*iter).name.c_str(), (*iter).description.c_str()); ls_helper(con, iter->name, iter->description);
con.reset_color(); con.reset_color();
} }
std::map<string, string> scripts; std::map<string, string> scripts;
@ -900,7 +917,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v
{ {
con.print("\nscripts:\n"); con.print("\nscripts:\n");
for (auto iter = scripts.begin(); iter != scripts.end(); ++iter) for (auto iter = scripts.begin(); iter != scripts.end(); ++iter)
con.print(" %-22s- %s\n", iter->first.c_str(), iter->second.c_str()); ls_helper(con, iter->first, iter->second);
} }
} }
} }

@ -125,6 +125,34 @@ std::string toLower(const std::string &str)
return rv; return rv;
} }
bool word_wrap(std::vector<std::string> *out, const std::string &str, size_t line_length)
{
out->clear();
std::istringstream input(str);
std::string out_line;
std::string word;
if (input >> word)
{
out_line += word;
// size_t remaining = line_length - std::min(line_length, word.length());
while (input >> word)
{
if (out_line.length() + word.length() + 1 <= line_length)
{
out_line += ' ';
out_line += word;
}
else
{
out->push_back(out_line);
out_line = word;
}
}
if (out_line.length())
out->push_back(out_line);
}
}
bool prefix_matches(const std::string &prefix, const std::string &key, std::string *tail) bool prefix_matches(const std::string &prefix, const std::string &key, std::string *tail)
{ {
size_t ksize = key.size(); size_t ksize = key.size();

@ -321,6 +321,10 @@ DFHACK_EXPORT std::string join_strings(const std::string &separator, const std::
DFHACK_EXPORT std::string toUpper(const std::string &str); DFHACK_EXPORT std::string toUpper(const std::string &str);
DFHACK_EXPORT std::string toLower(const std::string &str); DFHACK_EXPORT std::string toLower(const std::string &str);
DFHACK_EXPORT bool word_wrap(std::vector<std::string> *out,
const std::string &str,
size_t line_length = 80);
inline bool bits_match(unsigned required, unsigned ok, unsigned mask) inline bool bits_match(unsigned required, unsigned ok, unsigned mask)
{ {
return (required & mask) == (required & mask & ok); return (required & mask) == (required & mask & ok);

@ -20,9 +20,6 @@ def check_ls(fname, line):
if line.endswith('=begin') or not line.startswith(comment): if line.endswith('=begin') or not line.startswith(comment):
print('Error: no leading comment in ' + fname) print('Error: no leading comment in ' + fname)
return 1 return 1
if len(line.replace(comment, '').strip()) > 53:
print('Error: leading comment too long in ' + fname)
return 1
return 0 return 0