From 44a5082aafc055086f6001c16a4265e4610f8d2e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 28 Feb 2018 15:17:51 +1100 Subject: [PATCH] Commands: Refactor generate_switches_doc --- src/parameters_parser.cc | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/parameters_parser.cc b/src/parameters_parser.cc index 7638c246..a4456db4 100644 --- a/src/parameters_parser.cc +++ b/src/parameters_parser.cc @@ -7,24 +7,19 @@ namespace Kakoune String generate_switches_doc(const SwitchMap& switches) { - Vector lengths(switches.size()); - int i = 0; - for (auto& sw : switches) { - lengths[i++] = (int)sw.key.length() + (sw.value.takes_arg ? 5: 0); - } - int maxlen = *std::max_element(lengths.begin(), lengths.end()); - String res; - i = 0; + if (switches.empty()) + return res; + + auto switch_len = [](auto& sw) { return sw.key.column_length() + (sw.value.takes_arg ? 5 : 0); }; + auto switches_len = switches | transform(switch_len); + const ColumnCount maxlen = *std::max_element(switches_len.begin(), switches_len.end()); + for (auto& sw : switches) { - int len = lengths[i++]; - String pad = " "; - while (len++ < maxlen) - pad += ' '; res += format(" -{} {}{}{}\n", sw.key, sw.value.takes_arg ? "" : "", - pad, + String{' ', maxlen - switch_len(sw) + 1}, sw.value.description); } return res;