From a537095e1f42d3de726e3c4d8bfc018a4c43ad86 Mon Sep 17 00:00:00 2001 From: Philipp Jungkamp Date: Fri, 10 Feb 2023 18:03:19 +0100 Subject: [PATCH 1/2] Infer short completion descriptions for commandline flags Descriptions for commandline flags may not include newlines and should be rather short for display in a shell. Truncate the description string of a flag on '\n' or '.' to and add an ellipsis if needed. --- src/libutil/args.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 2930913d6..348f1bca0 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -29,7 +29,16 @@ void Args::removeFlag(const std::string & longName) void Completions::add(std::string completion, std::string description) { - assert(description.find('\n') == std::string::npos); + // strip whitespace/empty lines from the front of the description + description.erase(0, description.find_first_not_of(" \t\n")); + // ellipsize overflowing content on the back of the description + auto end_index = description.find_first_of(".\n"); + if (end_index != std::string::npos) { + auto needs_ellipsis = end_index != description.size() - 1; + description.resize(end_index); + if (needs_ellipsis) + description.append(" [...]"); + } insert(Completion { .completion = completion, .description = description From 30edd7af532bb893f76d5d4e7ec0fadfcdc941c6 Mon Sep 17 00:00:00 2001 From: Philipp Jungkamp Date: Fri, 10 Feb 2023 22:17:09 +0100 Subject: [PATCH 2/2] Completions::add use libutil trim() --- src/libutil/args.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 348f1bca0..35686a8aa 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -29,8 +29,7 @@ void Args::removeFlag(const std::string & longName) void Completions::add(std::string completion, std::string description) { - // strip whitespace/empty lines from the front of the description - description.erase(0, description.find_first_not_of(" \t\n")); + description = trim(description); // ellipsize overflowing content on the back of the description auto end_index = description.find_first_of(".\n"); if (end_index != std::string::npos) {