From b8c12f50e3edea1de11093502a59d5f72afc4c67 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Tue, 10 Apr 2018 16:10:56 +0300 Subject: [PATCH] Vertically align list item continuation lines when autowrapping --- spec/help_spec.lua | 28 ++++++++++++++++++++++++++++ src/argparse.lua | 8 +++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/spec/help_spec.lua b/spec/help_spec.lua index 3c87d58..e6fe228 100644 --- a/spec/help_spec.lua +++ b/spec/help_spec.lua @@ -578,6 +578,34 @@ Options: -h, --help Show this help message and exit.]], parser:get_help()) end) + it("preserves indentation of list items", function() + local parser = Parser "foo" + :help_max_width(80) + + parser:option "-f --foo" + :description("Let's start a list:\n\n" .. + "* Here is a list item.\n" .. + "* Here is another one, this one is very long so it needs several lines. More words. Word. Word.\n" .. + " + Here is a nested list item. Word. Word. Word. Word. Word. Bird. Word. Bird. Bird. Bird.\n" .. + "* Back to normal list, this one uses several spaces after the list item mark. Bird. Bird. Bird.") + + + assert.equal([[ +Usage: foo [-f ] [-h] + +Options: + -f , Let's start a list: + --foo + * Here is a list item. + * Here is another one, this one is very long so it + needs several lines. More words. Word. Word. + + Here is a nested list item. Word. Word. Word. Word. + Word. Bird. Word. Bird. Bird. Bird. + * Back to normal list, this one uses several spaces + after the list item mark. Bird. Bird. Bird. + -h, --help Show this help message and exit.]], parser:get_help()) + end) + it("preserves multiple spaces between words", function() local parser = Parser "foo" :help_max_width(80) diff --git a/src/argparse.lua b/src/argparse.lua index 43f85ba..8ece59f 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -807,7 +807,13 @@ local function autowrap_line(line, max_length) local result_lines = {} -- Preserve original indentation of the line, put this at the beginning of each result line. - local indentation = line:match("^( *)") + -- If the first word looks like a list marker ('*', '+', or '-'), add spaces so that starts + -- of the second and the following lines vertically align with the start of the second word. + local indentation = line:match("^ *") + + if line:find("^ *[%*%+%-]") then + indentation = indentation .. " " .. line:match("^ *[%*%+%-]( *)") + end -- Parts of the last line being assembled. local line_parts = {}