Introduction
Most code-golfers here add explanations to their submissions, so it's easier to understand what's going on. Usually the codelines go on the left and the corresponding explanation to the right with some kind of separator. To make it look pretty, the separators are all on the same column. Also long explanation text is usually wrapped to the next line, so the readers don't have to scroll horizontally to read everything.
However, when you want to edit this explanation because you made some crazy golfs, you often end up spending time to make your explanation pretty again. Since this is a very repetitive task, you want to write a program for this.
The Challenge
Given several lines of code with explanation and a separator, output the nicely formatted code with explanation.
Example
Input
shM-crz1dc4."ANDBYOROF # z = input rz1 # convert input to uppercase c d # split input on spaces c4."ANDBYOROF # create a list of the words from a packed string which shall be ignored - # filter those words out hM # only take the first letter of all words s # join them into one string
Output
shM-crz1dc4."ANDBYOROF # z = input rz1 # convert input to uppercase c d # split input on spaces c4."ANDBYOROF # create a list of the words from a packed string which shall be # ignored - # filter those words out hM # only take the first letter of all words s # join them into one string
One cookie for the first one who can find out what this code does.
The formatting algorithm
- Find the longest code-line (excluding the explanation and the spaces between code and separator).
- Add 5 spaces after this code-line and append the corresponding separator with explanation. This is now the reference line.
- Adjust every other line to this reference line, so that the seperators are all in the same column.
- Wrap all lines that are longer than 93 characters to a new line in the following way:
- Find the last word which end is at column 93 or lower.
- Take all words after this one and wrap them to a new line with the leading separator and the correct spacing. The space between those two words has to be deleted, so the first line ends with a word character and the second line starts with one after the separator.
- If the resulting line is still longer than 93 characters do the same again until every line is below 94 characters.
Notes
- A word consists of non-whitespace characters. Words are separated by a single space.
- The word wrapping is always possible. This means that no word is so long that it would make the wrapping impossible.
- The input will only contain printable ASCII and won't have any trailing whitespaces
- The separator will only appear once per line.
- While the explanation can have unlimited length, the separator and the code can only have a combined maximum length of
93 - 5 = 87
chars. The 5 chars are the spaces between code and separator. Code and separator will always be at least one character long. - The input may contain empty lines. Those will never contain any characters (except a newline if you take input as multiline string). Those empty lines have to be present in the output as well.
- Every line will have some code, a separator and an explanation. Exceptions are empty lines.
- You may take the input in any reasonable format, as long as it is not pre-processed. Make it clear in your answer which one you use.
- Output can be a multiline string or a list of strings.
Rules
- Function or full program allowed.
- Default rules for input/output.
- Standard loopholes apply.
- This is code-golf, so lowest byte-count wins. Tiebreaker is earlier submission.
Test cases
Input format here is a list of string representing the lines and a single string for the separator. Both are separated by a comma. Output is a list of strings.
['shM-crz1dc4."ANDBYOROF # z = input', '', ' rz1 # convert input to uppercase', ' c d # split input on spaces', ' c4."ANDBYOROF # create a list of the words from a packed string which shall be ignored', ' - # filter those words out', ' hM # only take the first letter of all words', 's # join them into one string'], "# " -> ['shM-crz1dc4."ANDBYOROF # z = input', '', ' rz1 # convert input to uppercase', ' c d # split input on spaces', ' c4."ANDBYOROF # create a list of the words from a packed string which shall be', ' # ignored', ' - # filter those words out', ' hM # only take the first letter of all words', 's # join them into one string'] ['codecodecode e#Explanation', 'sdf dsf sdf e#A Very very very very very very very very very long long long long long long long long long long long explanation and it keeps getting longer and longer', '', 'some more codee# and some more explanation'], "e#" -> ['codecodecode e#Explanation', 'sdf dsf sdf e#A Very very very very very very very very very long long long long long ', ' e#long long long long long long explanation and it keeps getting longer', ' e#and longer', '', 'some more code e# and some more explanation']
Happy Coding!
length of the longest code-line + 5
. This also applied to lines which only contain an explanation, because they were wrapped. \$\endgroup\$