Commit 1185265
authored
fix: calculates remaining space for options to avoid line breaking (#64)
partially addresses elastic/kibana#257200
## Summary
This PR changes how the remaining space on the line of a command is
calculated to decide when to break options into a new line or not.
## Details
**Before the fix:** The decision to break command options (e.g. STATS’s
BY ip) to a new line used this condition: `options.length > opts.wrap -
inp.remaining - cmd.length - 1 - args.txt.length`. For every command,
inp.remaining was set to the same value as opts.wrap (e.g. 500), so
`opts.wrap - inp.remaining` was always 0. The “remaining width” for
options became negative (e.g. 0 − 6 − 1 − 85 = −92), and so
`options.length > remaining` was always true (e.g. 5 > −92). As a
result, options were always broken to a new line, regardless of wrap.
Example: with `{ wrap: 120 and multiline: true }`, a STATS line like `|
STATS count = COUNT(*), avg = AVG(bytes), p95 = PERCENTILE(bytes, 95),
ext = VALUES(tags.keyword) BY ip` would still be split so that `BY ip`
appeared on the next line, even though it would have fit on the same
line.
**After the fix:** The code now computes how much of the line is already
used by the prefix (indent + pipe tab + "| " + command name + space +
args) and treats the rest as the width available for options. So
`linePrefixLength = indent.length + pipeTab.length + 2 + cmd.length + 1
+ args.txt.length`, and `remainingForOptions = opts.wrap -
linePrefixLength`. Options are broken only when `options.length >
remainingForOptions`. Example: with `{ wrap: 120 }`, a prefix of length
96 leaves 24 characters for options; "BY ip" has length 5, so 5 > 24 is
false and BY stays on the same line as STATS. With `{ wrap: 80 }`, the
same prefix might leave no room (or negative room), so 5 >
remainingForOptions becomes true and BY is correctly broken to a new
line. So the new structure represents “space left on the current line
for options” and only breaks when the options text would not fit in that
space.
### Checklist
- [x] Unit tests have been added or updated.
- [x] The PR title has the correct [conventional
commit](https://www.conventionalcommits.org/en/v1.0.0/) label in the
title, this is crucial for the correct versioning of this package. Check
the following cheat shit.
| Title Label | Release |
|---|---|
| `breaking: true (!)` | `major` |
| `feat` | `minor` |
| `fix` | `patch` |
| `refactor` | `patch` |
| `perf` | `patch` |
| `build` | `patch` |
| `docs` | `patch` |
| `chore` | `patch` |
| `revert` | `patch` |1 parent d6b90c7 commit 1185265
3 files changed
Lines changed: 46 additions & 20 deletions
File tree
- src/pretty_print
- __tests__
Lines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1024 | 1024 | | |
1025 | 1025 | | |
1026 | 1026 | | |
1027 | | - | |
1028 | | - | |
| 1027 | + | |
1029 | 1028 | | |
1030 | 1029 | | |
1031 | 1030 | | |
1032 | 1031 | | |
1033 | 1032 | | |
1034 | | - | |
1035 | | - | |
| 1033 | + | |
1036 | 1034 | | |
1037 | 1035 | | |
1038 | 1036 | | |
| |||
Lines changed: 31 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
230 | 230 | | |
231 | 231 | | |
232 | 232 | | |
233 | | - | |
234 | | - | |
| 233 | + | |
235 | 234 | | |
236 | 235 | | |
237 | 236 | | |
| |||
329 | 328 | | |
330 | 329 | | |
331 | 330 | | |
332 | | - | |
333 | | - | |
| 331 | + | |
334 | 332 | | |
335 | 333 | | |
336 | 334 | | |
| |||
782 | 780 | | |
783 | 781 | | |
784 | 782 | | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
785 | 806 | | |
786 | 807 | | |
787 | 808 | | |
| |||
940 | 961 | | |
941 | 962 | | |
942 | 963 | | |
943 | | - | |
| 964 | + | |
944 | 965 | | |
945 | 966 | | |
946 | 967 | | |
| |||
956 | 977 | | |
957 | 978 | | |
958 | 979 | | |
959 | | - | |
| 980 | + | |
960 | 981 | | |
961 | 982 | | |
962 | 983 | | |
| |||
1503 | 1524 | | |
1504 | 1525 | | |
1505 | 1526 | | |
1506 | | - | |
1507 | | - | |
| 1527 | + | |
1508 | 1528 | | |
1509 | 1529 | | |
1510 | 1530 | | |
1511 | 1531 | | |
1512 | 1532 | | |
1513 | | - | |
1514 | | - | |
| 1533 | + | |
1515 | 1534 | | |
1516 | 1535 | | |
1517 | 1536 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
915 | 915 | | |
916 | 916 | | |
917 | 917 | | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
918 | 930 | | |
919 | | - | |
920 | | - | |
921 | | - | |
922 | | - | |
| 931 | + | |
923 | 932 | | |
924 | 933 | | |
925 | 934 | | |
| |||
0 commit comments