BUG: Fix MI repr with long names
State: closed

Closes #21180 

```python
In [4]:         try:
   ...:             from unittest import mock
   ...:         except ImportError:
   ...:             mock = pytest.importorskip("mock")
   ...:
   ...:         terminal_size = os.terminal_size((118, 96))
   ...:         p1 = mock.patch('pandas.io.formats.console.get_terminal_size',
   ...:                         return_value=terminal_size)
   ...:         p2 = mock.patch('pandas.io.formats.format.get_terminal_size',
   ...:                         return_value=terminal_size)
   ...:
   ...:         index = range(5)
   ...:         columns = pd.MultiIndex.from_tuples([
   ...:             ('This is a long title with > 37 chars.', 'cat'),
   ...:             ('This is a loooooonger title with > 43 chars.', 'dog'),
   ...:         ])
   ...:         df = pd.DataFrame(1, index=index, columns=columns)
   ...:
   ...:

In [5]: with p1, p2:
   ...:     print('-' * 80)
   ...:     print(repr(df))
   ...:     print('-' * 80)
   ...:
```

output:

```
--------------------------------------------------------------------------------
  ...
  ...
0 ...
1 ...
2 ...
3 ...
4 ...

[5 rows x 2 columns]
--------------------------------------------------------------------------------
```

This matches the repr for non-hierarchical

```python
In [6]: s = pd.DataFrame({"A" * 41: [1, 2], 'B' * 41: [1, 2]})

In [7]: with p1, p2:
   ...:     print('-' * 80)
   ...:     print(repr(s))
   ...:     print('-' * 80)
   ...:
```

output:

```
--------------------------------------------------------------------------------
  ...
0 ...
1 ...

[2 rows x 2 columns]
--------------------------------------------------------------------------------
```

These can certainly be improved, though I'm not sure we'll (I'll) get to it for 0.23.2.

Labels:
name: Output-Formatting
description: __repr__ of pandas objects, to_string

Comments:
from @jorisvandenbossche 

> Overflowing the line is what is happening in my console if I make it smaller

Yeah, agreed that would be best, but I can't tell whose doing the wrapping, if that makes sense.
Well, I guess it has to be the terminal... So we should just always print two columns? And let the terminal wrap if needed?
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21655?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@1cc5471`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/21655/graphs/tree.svg?token=eZ4WkYLtcO&width=650&src=pr&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/21655?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master   #21655   +/-   ##
=========================================
  Coverage          ?    91.9%           
=========================================
  Files             ?      154           
  Lines             ?    49657           
  Branches          ?        0           
=========================================
  Hits              ?    45638           
  Misses            ?     4019           
  Partials          ?        0
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.28% <100%> (?)` | |
| #single | `42.05% <75%> (?)` | |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21655?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/21655/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `98.25% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21655?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21655?src=pr&el=footer). Last update [1cc5471...4391c24](https://codecov.io/gh/pandas-dev/pandas/pull/21655?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Running

```python
import pandas as pd
import numpy as np

s = pd.DataFrame({"A" * 41: 1, "B" * 41: 1}, index=[0, 1, 2])
print("Regular Columns:")
print(repr(s))
print('\n\n')

index = range(5)
columns = pd.MultiIndex.from_tuples([
    ('This is a long title with > 37 chars.', 'cat'),
    ('This is a loooooonger title with > 43 chars.', 'dog'),
])
df = pd.DataFrame(1, index=index, columns=columns)

print("MultiIndex")
print(repr(df))
```

output

```
Regular Columns:
   AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA  BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
0                                          1                                          1
1                                          1                                          1
2                                          1                                          1



MultiIndex
  This is a long title with > 37 chars. This is a loooooonger title with > 43 chars.
                                    cat                                          dog
0                                     1                                            1
1                                     1                                            1
2                                     1                                            1
3                                     1                                            1
4                                     1                                            1
```
The output in the top-post is not longer correct with the latest changes?
Correct, https://github.com/pandas-dev/pandas/pull/21655#issuecomment-400685035 has the current output.
I can confirm that this fixes https://github.com/pandas-dev/pandas/issues/21327 (the one that I could reproduce in my local console without any hacks)