From 022a1bf9e88b51e92f02f3f6a73a12373401d3e8 Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 14 Jun 2016 21:24:27 -0400 Subject: [PATCH] Wrap script descriptions in `ls` output and remove description length cap --- library/Core.cpp | 31 ++++++++++++++++++++++++------- library/MiscUtils.cpp | 28 ++++++++++++++++++++++++++++ library/include/MiscUtils.h | 4 ++++ travis/script-in-readme.py | 3 --- 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index f7f057e19..f5bc9d4e7 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -618,6 +618,27 @@ string getBuiltinCommand(std::string cmd) 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 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 &parts) { 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++) { - const PluginCommand & pcmd = (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(); + ls_helper(con, plug->operator[](j)); } } else @@ -891,7 +908,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v { if ((*iter).recolor) 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(); } std::map scripts; @@ -900,7 +917,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, v { con.print("\nscripts:\n"); 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); } } } diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 823b676eb..43fb5ef48 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -125,6 +125,34 @@ std::string toLower(const std::string &str) return rv; } +bool word_wrap(std::vector *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) { size_t ksize = key.size(); diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 653e37f47..917b67489 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -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 toLower(const std::string &str); +DFHACK_EXPORT bool word_wrap(std::vector *out, + const std::string &str, + size_t line_length = 80); + inline bool bits_match(unsigned required, unsigned ok, unsigned mask) { return (required & mask) == (required & mask & ok); diff --git a/travis/script-in-readme.py b/travis/script-in-readme.py index 72b7db86a..8227dea8d 100644 --- a/travis/script-in-readme.py +++ b/travis/script-in-readme.py @@ -20,9 +20,6 @@ def check_ls(fname, line): if line.endswith('=begin') or not line.startswith(comment): print('Error: no leading comment in ' + fname) return 1 - if len(line.replace(comment, '').strip()) > 53: - print('Error: leading comment too long in ' + fname) - return 1 return 0