Introduction
cowsay
is a Unix command made by Tony Monroe, written in Perl. It outputs a picture of a cow saying given text. For example:
________________________
< Typical cowsay output! >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Challenge
Given a multiline string as input, output a cow saying the inputted string.
The output is made like so:
- Start with one space
- Append the length of the longest line of the input plus \$2\$ underscores
- Append a newline
- Append a
<
if the input is a single line, else append a/
- Append a space
- Append the first line of input
- Append the length of the longest line, minus the length of the current line, plus \$1\$, spaces
- Append a
>
if the input is a single line, else append a\
- Append a newline
- While there are still lines left in the input:
- Append a
\
if this is the last line, else append a|
- Append a space
- Append the current line of input
- Append the length of the longest line, minus the length of the current line, plus \$1\$, spaces
- Append a
/
if this is the last line, else append a|
- Append a newline
- Append a
- Append a space
- Append the length of the longest line of the input plus \$2\$ hyphens
- Append a newline
- Append the cow and speech bubble line:
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Test cases
Input
...becomes...
Output
====================
Typical cowsay output!
...becomes...
________________________
< Typical cowsay output! >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
====================
Two
Lines
...becomes...
_______
/ Two \
\ Lines /
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
====================
This is the first line of input.
This is the second line. It is the longest, so the bubble should stretch to fit it.
This is the third line.
Hello world!
(;;"''{<<['"
Empty lines shouldn't cause any problems.
...becomes...
_____________________________________________________________________________________
/ This is the first line of input. \
| This is the second line. It is the longest, so the bubble should stretch to fit it. |
| This is the third line. |
| Hello world! |
| (;;"''{<<['" |
| |
| |
| |
\ Empty lines shouldn't cause any problems. /
-------------------------------------------------------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Rules
- Standard loopholes are forbidden.
- Any amount of trailing whitespace is allowed on any line. Trailing newlines are also allowed.
- If possible, please link to an online interpreter (e.g. TIO) to run your program on.
- Please explain your answer. This is not necessary, but it makes it easier for others to understand.
- Languages newer than the question are allowed. This means you could create your own language where it would be trivial to do this, but don't expect any upvotes.
- You may assume only printable ASCII is given as input.
- Please, don't submit
cowsay
in Bash. It's boring and you'll lose rep quickly. - This is code-golf, so shortest code in bytes wins!
Reference implementation
Note
The cow is one space too close to the left, but I've kept it this way so people don't need to update their answers. But I will accept the correct version.