We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Single-character wrapped lines with long options
When I run the script at the bottom I get the following output. I would, obviously, expect ALSOLONG to be all together on the second row.

```
Usage: example.py relatively_long example [OPTIONS] FIRST SECOND LONGISH
                                          A
                                          L
                                          S
                                          O
                                          L
                                          O
                                          N
                                          G

Error: Missing argument "first".
```
## 

``` py
import click


@click.group()
def foo():
    pass


@foo.group()
def relatively_long():
    pass


@relatively_long.command()
@click.argument('first')
@click.argument('second')
@click.argument('longish')
@click.argument('alsolong')
def example():
    pass


foo()
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `click/formatting.py`
Content:
```
1 from contextlib import contextmanager
2 from .termui import get_terminal_size
3 from .parser import split_opt
4 from ._compat import term_len
5 
6 
7 def measure_table(rows):
8     widths = {}
9     for row in rows:
10         for idx, col in enumerate(row):
11             widths[idx] = max(widths.get(idx, 0), term_len(col))
12     return tuple(y for x, y in sorted(widths.items()))
13 
14 
15 def iter_rows(rows, col_count):
16     for row in rows:
17         row = tuple(row)
18         yield row + ('',) * (col_count - len(row))
19 
20 
21 def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
22               preserve_paragraphs=False):
23     """A helper function that intelligently wraps text.  By default, it
24     assumes that it operates on a single paragraph of text but if the
25     `preserve_paragraphs` parameter is provided it will intelligently
26     handle paragraphs (defined by two empty lines).
27 
28     If paragraphs are handled, a paragraph can be prefixed with an empty
29     line containing the ``\\b`` character (``\\x08``) to indicate that
30     no rewrapping should happen in that block.
31 
32     :param text: the text that should be rewrapped.
33     :param width: the maximum width for the text.
34     :param initial_indent: the initial indent that should be placed on the
35                            first line as a string.
36     :param subsequent_indent: the indent string that should be placed on
37                               each consecutive line.
38     :param preserve_paragraphs: if this flag is set then the wrapping will
39                                 intelligently handle paragraphs.
40     """
41     from ._textwrap import TextWrapper
42     text = text.expandtabs()
43     wrapper = TextWrapper(width, initial_indent=initial_indent,
44                           subsequent_indent=subsequent_indent,
45                           replace_whitespace=False)
46     if not preserve_paragraphs:
47         return wrapper.fill(text)
48 
49     p = []
50     buf = []
51     indent = None
52 
53     def _flush_par():
54         if not buf:
55             return
56         if buf[0].strip() == '\b':
57             p.append((indent or 0, True, '\n'.join(buf[1:])))
58         else:
59             p.append((indent or 0, False, ' '.join(buf)))
60         del buf[:]
61 
62     for line in text.splitlines():
63         if not line:
64             _flush_par()
65             indent = None
66         else:
67             if indent is None:
68                 orig_len = term_len(line)
69                 line = line.lstrip()
70                 indent = orig_len - term_len(line)
71             buf.append(line)
72     _flush_par()
73 
74     rv = []
75     for indent, raw, text in p:
76         with wrapper.extra_indent(' ' * indent):
77             if raw:
78                 rv.append(wrapper.indent_only(text))
79             else:
80                 rv.append(wrapper.fill(text))
81 
82     return '\n\n'.join(rv)
83 
84 
85 class HelpFormatter(object):
86     """This class helps with formatting text-based help pages.  It's
87     usually just needed for very special internal cases, but it's also
88     exposed so that developers can write their own fancy outputs.
89 
90     At present, it always writes into memory.
91 
92     :param indent_increment: the additional increment for each level.
93     :param width: the width for the text.  This defaults to the terminal
94                   width clamped to a maximum of 78.
95     """
96 
97     def __init__(self, indent_increment=2, width=None, max_width=None):
98         self.indent_increment = indent_increment
99         if max_width is None:
100             max_width = 80
101         if width is None:
102             width = max(min(get_terminal_size()[0], max_width) - 2, 50)
103         self.width = width
104         self.current_indent = 0
105         self.buffer = []
106 
107     def write(self, string):
108         """Writes a unicode string into the internal buffer."""
109         self.buffer.append(string)
110 
111     def indent(self):
112         """Increases the indentation."""
113         self.current_indent += self.indent_increment
114 
115     def dedent(self):
116         """Decreases the indentation."""
117         self.current_indent -= self.indent_increment
118 
119     def write_usage(self, prog, args='', prefix='Usage: '):
120         """Writes a usage line into the buffer.
121 
122         :param prog: the program name.
123         :param args: whitespace separated list of arguments.
124         :param prefix: the prefix for the first line.
125         """
126         prefix = '%*s%s' % (self.current_indent, prefix, prog)
127         self.write(prefix)
128 
129         text_width = max(self.width - self.current_indent - term_len(prefix), 10)
130         indent = ' ' * (term_len(prefix) + 1)
131         self.write(wrap_text(args, text_width,
132                              initial_indent=' ',
133                              subsequent_indent=indent))
134 
135         self.write('\n')
136 
137     def write_heading(self, heading):
138         """Writes a heading into the buffer."""
139         self.write('%*s%s:\n' % (self.current_indent, '', heading))
140 
141     def write_paragraph(self):
142         """Writes a paragraph into the buffer."""
143         if self.buffer:
144             self.write('\n')
145 
146     def write_text(self, text):
147         """Writes re-indented text into the buffer.  This rewraps and
148         preserves paragraphs.
149         """
150         text_width = max(self.width - self.current_indent, 11)
151         indent = ' ' * self.current_indent
152         self.write(wrap_text(text, text_width,
153                              initial_indent=indent,
154                              subsequent_indent=indent,
155                              preserve_paragraphs=True))
156         self.write('\n')
157 
158     def write_dl(self, rows, col_max=30, col_spacing=2):
159         """Writes a definition list into the buffer.  This is how options
160         and commands are usually formatted.
161 
162         :param rows: a list of two item tuples for the terms and values.
163         :param col_max: the maximum width of the first column.
164         :param col_spacing: the number of spaces between the first and
165                             second column.
166         """
167         rows = list(rows)
168         widths = measure_table(rows)
169         if len(widths) != 2:
170             raise TypeError('Expected two columns for definition list')
171 
172         first_col = min(widths[0], col_max) + col_spacing
173 
174         for first, second in iter_rows(rows, len(widths)):
175             self.write('%*s%s' % (self.current_indent, '', first))
176             if not second:
177                 self.write('\n')
178                 continue
179             if term_len(first) <= first_col - col_spacing:
180                 self.write(' ' * (first_col - term_len(first)))
181             else:
182                 self.write('\n')
183                 self.write(' ' * (first_col + self.current_indent))
184 
185             text_width = max(self.width - first_col - 2, 10)
186             lines = iter(wrap_text(second, text_width).splitlines())
187             if lines:
188                 self.write(next(lines) + '\n')
189                 for line in lines:
190                     self.write('%*s%s\n' % (
191                         first_col + self.current_indent, '', line))
192             else:
193                 self.write('\n')
194 
195     @contextmanager
196     def section(self, name):
197         """Helpful context manager that writes a paragraph, a heading,
198         and the indents.
199 
200         :param name: the section name that is written as heading.
201         """
202         self.write_paragraph()
203         self.write_heading(name)
204         self.indent()
205         try:
206             yield
207         finally:
208             self.dedent()
209 
210     @contextmanager
211     def indentation(self):
212         """A context manager that increases the indentation."""
213         self.indent()
214         try:
215             yield
216         finally:
217             self.dedent()
218 
219     def getvalue(self):
220         """Returns the buffer contents."""
221         return ''.join(self.buffer)
222 
223 
224 def join_options(options):
225     """Given a list of option strings this joins them in the most appropriate
226     way and returns them in the form ``(formatted_string,
227     any_prefix_is_slash)`` where the second item in the tuple is a flag that
228     indicates if any of the option prefixes was a slash.
229     """
230     rv = []
231     any_prefix_is_slash = False
232     for opt in options:
233         prefix = split_opt(opt)[0]
234         if prefix == '/':
235             any_prefix_is_slash = True
236         rv.append((len(prefix), opt))
237 
238     rv.sort(key=lambda x: x[0])
239 
240     rv = ', '.join(x[1] for x in rv)
241     return rv, any_prefix_is_slash
242
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/click/formatting.py b/click/formatting.py
--- a/click/formatting.py
+++ b/click/formatting.py
@@ -18,6 +18,12 @@
         yield row + ('',) * (col_count - len(row))
 
 
+def add_subsequent_indent(text, subsequent_indent):
+    lines = text.splitlines()
+    lines = [lines[0]] + [subsequent_indent + line for line in lines[1:]]
+    return '\n'.join(lines)
+
+
 def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
               preserve_paragraphs=False):
     """A helper function that intelligently wraps text.  By default, it
@@ -40,11 +46,13 @@
     """
     from ._textwrap import TextWrapper
     text = text.expandtabs()
+    post_wrap_indent = subsequent_indent[:-1]
+    subsequent_indent = subsequent_indent[-1:]
     wrapper = TextWrapper(width, initial_indent=initial_indent,
                           subsequent_indent=subsequent_indent,
                           replace_whitespace=False)
     if not preserve_paragraphs:
-        return wrapper.fill(text)
+        return add_subsequent_indent(wrapper.fill(text), post_wrap_indent)
 
     p = []
     buf = []
@@ -75,9 +83,11 @@
     for indent, raw, text in p:
         with wrapper.extra_indent(' ' * indent):
             if raw:
-                rv.append(wrapper.indent_only(text))
+                rv.append(add_subsequent_indent(wrapper.indent_only(text),
+                                                post_wrap_indent))
             else:
-                rv.append(wrapper.fill(text))
+                rv.append(add_subsequent_indent(wrapper.fill(text),
+                                                post_wrap_indent))
 
     return '\n\n'.join(rv)

{"golden_diff": "diff --git a/click/formatting.py b/click/formatting.py\n--- a/click/formatting.py\n+++ b/click/formatting.py\n@@ -18,6 +18,12 @@\n         yield row + ('',) * (col_count - len(row))\n \n \n+def add_subsequent_indent(text, subsequent_indent):\n+    lines = text.splitlines()\n+    lines = [lines[0]] + [subsequent_indent + line for line in lines[1:]]\n+    return '\\n'.join(lines)\n+\n+\n def wrap_text(text, width=78, initial_indent='', subsequent_indent='',\n               preserve_paragraphs=False):\n     \"\"\"A helper function that intelligently wraps text.  By default, it\n@@ -40,11 +46,13 @@\n     \"\"\"\n     from ._textwrap import TextWrapper\n     text = text.expandtabs()\n+    post_wrap_indent = subsequent_indent[:-1]\n+    subsequent_indent = subsequent_indent[-1:]\n     wrapper = TextWrapper(width, initial_indent=initial_indent,\n                           subsequent_indent=subsequent_indent,\n                           replace_whitespace=False)\n     if not preserve_paragraphs:\n-        return wrapper.fill(text)\n+        return add_subsequent_indent(wrapper.fill(text), post_wrap_indent)\n \n     p = []\n     buf = []\n@@ -75,9 +83,11 @@\n     for indent, raw, text in p:\n         with wrapper.extra_indent(' ' * indent):\n             if raw:\n-                rv.append(wrapper.indent_only(text))\n+                rv.append(add_subsequent_indent(wrapper.indent_only(text),\n+                                                post_wrap_indent))\n             else:\n-                rv.append(wrapper.fill(text))\n+                rv.append(add_subsequent_indent(wrapper.fill(text),\n+                                                post_wrap_indent))\n \n     return '\\n\\n'.join(rv)\n", "issue": "Single-character wrapped lines with long options\nWhen I run the script at the bottom I get the following output. I would, obviously, expect ALSOLONG to be all together on the second row.\n\n```\nUsage: example.py relatively_long example [OPTIONS] FIRST SECOND LONGISH\n                                          A\n                                          L\n                                          S\n                                          O\n                                          L\n                                          O\n                                          N\n                                          G\n\nError: Missing argument \"first\".\n```\n## \n\n``` py\nimport click\n\n\n@click.group()\ndef foo():\n    pass\n\n\n@foo.group()\ndef relatively_long():\n    pass\n\n\n@relatively_long.command()\n@click.argument('first')\n@click.argument('second')\n@click.argument('longish')\n@click.argument('alsolong')\ndef example():\n    pass\n\n\nfoo()\n```\n\n", "before_files": [{"content": "from contextlib import contextmanager\nfrom .termui import get_terminal_size\nfrom .parser import split_opt\nfrom ._compat import term_len\n\n\ndef measure_table(rows):\n    widths = {}\n    for row in rows:\n        for idx, col in enumerate(row):\n            widths[idx] = max(widths.get(idx, 0), term_len(col))\n    return tuple(y for x, y in sorted(widths.items()))\n\n\ndef iter_rows(rows, col_count):\n    for row in rows:\n        row = tuple(row)\n        yield row + ('',) * (col_count - len(row))\n\n\ndef wrap_text(text, width=78, initial_indent='', subsequent_indent='',\n              preserve_paragraphs=False):\n    \"\"\"A helper function that intelligently wraps text.  By default, it\n    assumes that it operates on a single paragraph of text but if the\n    `preserve_paragraphs` parameter is provided it will intelligently\n    handle paragraphs (defined by two empty lines).\n\n    If paragraphs are handled, a paragraph can be prefixed with an empty\n    line containing the ``\\\\b`` character (``\\\\x08``) to indicate that\n    no rewrapping should happen in that block.\n\n    :param text: the text that should be rewrapped.\n    :param width: the maximum width for the text.\n    :param initial_indent: the initial indent that should be placed on the\n                           first line as a string.\n    :param subsequent_indent: the indent string that should be placed on\n                              each consecutive line.\n    :param preserve_paragraphs: if this flag is set then the wrapping will\n                                intelligently handle paragraphs.\n    \"\"\"\n    from ._textwrap import TextWrapper\n    text = text.expandtabs()\n    wrapper = TextWrapper(width, initial_indent=initial_indent,\n                          subsequent_indent=subsequent_indent,\n                          replace_whitespace=False)\n    if not preserve_paragraphs:\n        return wrapper.fill(text)\n\n    p = []\n    buf = []\n    indent = None\n\n    def _flush_par():\n        if not buf:\n            return\n        if buf[0].strip() == '\\b':\n            p.append((indent or 0, True, '\\n'.join(buf[1:])))\n        else:\n            p.append((indent or 0, False, ' '.join(buf)))\n        del buf[:]\n\n    for line in text.splitlines():\n        if not line:\n            _flush_par()\n            indent = None\n        else:\n            if indent is None:\n                orig_len = term_len(line)\n                line = line.lstrip()\n                indent = orig_len - term_len(line)\n            buf.append(line)\n    _flush_par()\n\n    rv = []\n    for indent, raw, text in p:\n        with wrapper.extra_indent(' ' * indent):\n            if raw:\n                rv.append(wrapper.indent_only(text))\n            else:\n                rv.append(wrapper.fill(text))\n\n    return '\\n\\n'.join(rv)\n\n\nclass HelpFormatter(object):\n    \"\"\"This class helps with formatting text-based help pages.  It's\n    usually just needed for very special internal cases, but it's also\n    exposed so that developers can write their own fancy outputs.\n\n    At present, it always writes into memory.\n\n    :param indent_increment: the additional increment for each level.\n    :param width: the width for the text.  This defaults to the terminal\n                  width clamped to a maximum of 78.\n    \"\"\"\n\n    def __init__(self, indent_increment=2, width=None, max_width=None):\n        self.indent_increment = indent_increment\n        if max_width is None:\n            max_width = 80\n        if width is None:\n            width = max(min(get_terminal_size()[0], max_width) - 2, 50)\n        self.width = width\n        self.current_indent = 0\n        self.buffer = []\n\n    def write(self, string):\n        \"\"\"Writes a unicode string into the internal buffer.\"\"\"\n        self.buffer.append(string)\n\n    def indent(self):\n        \"\"\"Increases the indentation.\"\"\"\n        self.current_indent += self.indent_increment\n\n    def dedent(self):\n        \"\"\"Decreases the indentation.\"\"\"\n        self.current_indent -= self.indent_increment\n\n    def write_usage(self, prog, args='', prefix='Usage: '):\n        \"\"\"Writes a usage line into the buffer.\n\n        :param prog: the program name.\n        :param args: whitespace separated list of arguments.\n        :param prefix: the prefix for the first line.\n        \"\"\"\n        prefix = '%*s%s' % (self.current_indent, prefix, prog)\n        self.write(prefix)\n\n        text_width = max(self.width - self.current_indent - term_len(prefix), 10)\n        indent = ' ' * (term_len(prefix) + 1)\n        self.write(wrap_text(args, text_width,\n                             initial_indent=' ',\n                             subsequent_indent=indent))\n\n        self.write('\\n')\n\n    def write_heading(self, heading):\n        \"\"\"Writes a heading into the buffer.\"\"\"\n        self.write('%*s%s:\\n' % (self.current_indent, '', heading))\n\n    def write_paragraph(self):\n        \"\"\"Writes a paragraph into the buffer.\"\"\"\n        if self.buffer:\n            self.write('\\n')\n\n    def write_text(self, text):\n        \"\"\"Writes re-indented text into the buffer.  This rewraps and\n        preserves paragraphs.\n        \"\"\"\n        text_width = max(self.width - self.current_indent, 11)\n        indent = ' ' * self.current_indent\n        self.write(wrap_text(text, text_width,\n                             initial_indent=indent,\n                             subsequent_indent=indent,\n                             preserve_paragraphs=True))\n        self.write('\\n')\n\n    def write_dl(self, rows, col_max=30, col_spacing=2):\n        \"\"\"Writes a definition list into the buffer.  This is how options\n        and commands are usually formatted.\n\n        :param rows: a list of two item tuples for the terms and values.\n        :param col_max: the maximum width of the first column.\n        :param col_spacing: the number of spaces between the first and\n                            second column.\n        \"\"\"\n        rows = list(rows)\n        widths = measure_table(rows)\n        if len(widths) != 2:\n            raise TypeError('Expected two columns for definition list')\n\n        first_col = min(widths[0], col_max) + col_spacing\n\n        for first, second in iter_rows(rows, len(widths)):\n            self.write('%*s%s' % (self.current_indent, '', first))\n            if not second:\n                self.write('\\n')\n                continue\n            if term_len(first) <= first_col - col_spacing:\n                self.write(' ' * (first_col - term_len(first)))\n            else:\n                self.write('\\n')\n                self.write(' ' * (first_col + self.current_indent))\n\n            text_width = max(self.width - first_col - 2, 10)\n            lines = iter(wrap_text(second, text_width).splitlines())\n            if lines:\n                self.write(next(lines) + '\\n')\n                for line in lines:\n                    self.write('%*s%s\\n' % (\n                        first_col + self.current_indent, '', line))\n            else:\n                self.write('\\n')\n\n    @contextmanager\n    def section(self, name):\n        \"\"\"Helpful context manager that writes a paragraph, a heading,\n        and the indents.\n\n        :param name: the section name that is written as heading.\n        \"\"\"\n        self.write_paragraph()\n        self.write_heading(name)\n        self.indent()\n        try:\n            yield\n        finally:\n            self.dedent()\n\n    @contextmanager\n    def indentation(self):\n        \"\"\"A context manager that increases the indentation.\"\"\"\n        self.indent()\n        try:\n            yield\n        finally:\n            self.dedent()\n\n    def getvalue(self):\n        \"\"\"Returns the buffer contents.\"\"\"\n        return ''.join(self.buffer)\n\n\ndef join_options(options):\n    \"\"\"Given a list of option strings this joins them in the most appropriate\n    way and returns them in the form ``(formatted_string,\n    any_prefix_is_slash)`` where the second item in the tuple is a flag that\n    indicates if any of the option prefixes was a slash.\n    \"\"\"\n    rv = []\n    any_prefix_is_slash = False\n    for opt in options:\n        prefix = split_opt(opt)[0]\n        if prefix == '/':\n            any_prefix_is_slash = True\n        rv.append((len(prefix), opt))\n\n    rv.sort(key=lambda x: x[0])\n\n    rv = ', '.join(x[1] for x in rv)\n    return rv, any_prefix_is_slash\n", "path": "click/formatting.py"}], "after_files": [{"content": "from contextlib import contextmanager\nfrom .termui import get_terminal_size\nfrom .parser import split_opt\nfrom ._compat import term_len\n\n\ndef measure_table(rows):\n    widths = {}\n    for row in rows:\n        for idx, col in enumerate(row):\n            widths[idx] = max(widths.get(idx, 0), term_len(col))\n    return tuple(y for x, y in sorted(widths.items()))\n\n\ndef iter_rows(rows, col_count):\n    for row in rows:\n        row = tuple(row)\n        yield row + ('',) * (col_count - len(row))\n\n\ndef add_subsequent_indent(text, subsequent_indent):\n    lines = text.splitlines()\n    lines = [lines[0]] + [subsequent_indent + line for line in lines[1:]]\n    return '\\n'.join(lines)\n\n\ndef wrap_text(text, width=78, initial_indent='', subsequent_indent='',\n              preserve_paragraphs=False):\n    \"\"\"A helper function that intelligently wraps text.  By default, it\n    assumes that it operates on a single paragraph of text but if the\n    `preserve_paragraphs` parameter is provided it will intelligently\n    handle paragraphs (defined by two empty lines).\n\n    If paragraphs are handled, a paragraph can be prefixed with an empty\n    line containing the ``\\\\b`` character (``\\\\x08``) to indicate that\n    no rewrapping should happen in that block.\n\n    :param text: the text that should be rewrapped.\n    :param width: the maximum width for the text.\n    :param initial_indent: the initial indent that should be placed on the\n                           first line as a string.\n    :param subsequent_indent: the indent string that should be placed on\n                              each consecutive line.\n    :param preserve_paragraphs: if this flag is set then the wrapping will\n                                intelligently handle paragraphs.\n    \"\"\"\n    from ._textwrap import TextWrapper\n    text = text.expandtabs()\n    post_wrap_indent = subsequent_indent[:-1]\n    subsequent_indent = subsequent_indent[-1:]\n    wrapper = TextWrapper(width, initial_indent=initial_indent,\n                          subsequent_indent=subsequent_indent,\n                          replace_whitespace=False)\n    if not preserve_paragraphs:\n        return add_subsequent_indent(wrapper.fill(text), post_wrap_indent)\n\n    p = []\n    buf = []\n    indent = None\n\n    def _flush_par():\n        if not buf:\n            return\n        if buf[0].strip() == '\\b':\n            p.append((indent or 0, True, '\\n'.join(buf[1:])))\n        else:\n            p.append((indent or 0, False, ' '.join(buf)))\n        del buf[:]\n\n    for line in text.splitlines():\n        if not line:\n            _flush_par()\n            indent = None\n        else:\n            if indent is None:\n                orig_len = term_len(line)\n                line = line.lstrip()\n                indent = orig_len - term_len(line)\n            buf.append(line)\n    _flush_par()\n\n    rv = []\n    for indent, raw, text in p:\n        with wrapper.extra_indent(' ' * indent):\n            if raw:\n                rv.append(add_subsequent_indent(wrapper.indent_only(text),\n                                                post_wrap_indent))\n            else:\n                rv.append(add_subsequent_indent(wrapper.fill(text),\n                                                post_wrap_indent))\n\n    return '\\n\\n'.join(rv)\n\n\nclass HelpFormatter(object):\n    \"\"\"This class helps with formatting text-based help pages.  It's\n    usually just needed for very special internal cases, but it's also\n    exposed so that developers can write their own fancy outputs.\n\n    At present, it always writes into memory.\n\n    :param indent_increment: the additional increment for each level.\n    :param width: the width for the text.  This defaults to the terminal\n                  width clamped to a maximum of 78.\n    \"\"\"\n\n    def __init__(self, indent_increment=2, width=None, max_width=None):\n        self.indent_increment = indent_increment\n        if max_width is None:\n            max_width = 80\n        if width is None:\n            width = max(min(get_terminal_size()[0], max_width) - 2, 50)\n        self.width = width\n        self.current_indent = 0\n        self.buffer = []\n\n    def write(self, string):\n        \"\"\"Writes a unicode string into the internal buffer.\"\"\"\n        self.buffer.append(string)\n\n    def indent(self):\n        \"\"\"Increases the indentation.\"\"\"\n        self.current_indent += self.indent_increment\n\n    def dedent(self):\n        \"\"\"Decreases the indentation.\"\"\"\n        self.current_indent -= self.indent_increment\n\n    def write_usage(self, prog, args='', prefix='Usage: '):\n        \"\"\"Writes a usage line into the buffer.\n\n        :param prog: the program name.\n        :param args: whitespace separated list of arguments.\n        :param prefix: the prefix for the first line.\n        \"\"\"\n        prefix = '%*s%s' % (self.current_indent, prefix, prog)\n        self.write(prefix)\n\n        text_width = max(self.width - self.current_indent - term_len(prefix), 10)\n        indent = ' ' * (term_len(prefix) + 1)\n        self.write(wrap_text(args, text_width,\n                             initial_indent=' ',\n                             subsequent_indent=indent))\n\n        self.write('\\n')\n\n    def write_heading(self, heading):\n        \"\"\"Writes a heading into the buffer.\"\"\"\n        self.write('%*s%s:\\n' % (self.current_indent, '', heading))\n\n    def write_paragraph(self):\n        \"\"\"Writes a paragraph into the buffer.\"\"\"\n        if self.buffer:\n            self.write('\\n')\n\n    def write_text(self, text):\n        \"\"\"Writes re-indented text into the buffer.  This rewraps and\n        preserves paragraphs.\n        \"\"\"\n        text_width = max(self.width - self.current_indent, 11)\n        indent = ' ' * self.current_indent\n        self.write(wrap_text(text, text_width,\n                             initial_indent=indent,\n                             subsequent_indent=indent,\n                             preserve_paragraphs=True))\n        self.write('\\n')\n\n    def write_dl(self, rows, col_max=30, col_spacing=2):\n        \"\"\"Writes a definition list into the buffer.  This is how options\n        and commands are usually formatted.\n\n        :param rows: a list of two item tuples for the terms and values.\n        :param col_max: the maximum width of the first column.\n        :param col_spacing: the number of spaces between the first and\n                            second column.\n        \"\"\"\n        rows = list(rows)\n        widths = measure_table(rows)\n        if len(widths) != 2:\n            raise TypeError('Expected two columns for definition list')\n\n        first_col = min(widths[0], col_max) + col_spacing\n\n        for first, second in iter_rows(rows, len(widths)):\n            self.write('%*s%s' % (self.current_indent, '', first))\n            if not second:\n                self.write('\\n')\n                continue\n            if term_len(first) <= first_col - col_spacing:\n                self.write(' ' * (first_col - term_len(first)))\n            else:\n                self.write('\\n')\n                self.write(' ' * (first_col + self.current_indent))\n\n            text_width = max(self.width - first_col - 2, 10)\n            lines = iter(wrap_text(second, text_width).splitlines())\n            if lines:\n                self.write(next(lines) + '\\n')\n                for line in lines:\n                    self.write('%*s%s\\n' % (\n                        first_col + self.current_indent, '', line))\n            else:\n                self.write('\\n')\n\n    @contextmanager\n    def section(self, name):\n        \"\"\"Helpful context manager that writes a paragraph, a heading,\n        and the indents.\n\n        :param name: the section name that is written as heading.\n        \"\"\"\n        self.write_paragraph()\n        self.write_heading(name)\n        self.indent()\n        try:\n            yield\n        finally:\n            self.dedent()\n\n    @contextmanager\n    def indentation(self):\n        \"\"\"A context manager that increases the indentation.\"\"\"\n        self.indent()\n        try:\n            yield\n        finally:\n            self.dedent()\n\n    def getvalue(self):\n        \"\"\"Returns the buffer contents.\"\"\"\n        return ''.join(self.buffer)\n\n\ndef join_options(options):\n    \"\"\"Given a list of option strings this joins them in the most appropriate\n    way and returns them in the form ``(formatted_string,\n    any_prefix_is_slash)`` where the second item in the tuple is a flag that\n    indicates if any of the option prefixes was a slash.\n    \"\"\"\n    rv = []\n    any_prefix_is_slash = False\n    for opt in options:\n        prefix = split_opt(opt)[0]\n        if prefix == '/':\n            any_prefix_is_slash = True\n        rv.append((len(prefix), opt))\n\n    rv.sort(key=lambda x: x[0])\n\n    rv = ', '.join(x[1] for x in rv)\n    return rv, any_prefix_is_slash\n", "path": "click/formatting.py"}]}