Skip to content

Commit

Permalink
Infer short completion descriptions for commandline flags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PJungkamp committed Feb 10, 2023
1 parent e4a2a5c commit a537095
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/libutil/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a537095

Please sign in to comment.