"Cowsay" in short

12

Simulate cowsay in the default mode.

$ cowsay <<< Hello
 _______
< Hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

$ perl -e 'print "Long text "x20' | cowsay
 __________________________________________
/ Long text Long text Long text Long text  \
| Long text Long text Long text Long text  |
| Long text Long text Long text Long text  |
| Long text Long text Long text Long text  |
\ Long text Long text Long text Long text  /
 ------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

How short can be the code? The cow and the balloon can be slightly different, but all elements should be present. Dynamic/multiline balloon is a plus.

Explicit list of the required elements:

  • Text;
  • Balloon;
  • Line from the cow to the balloon;
  • The cow (eye[s], month, ears, body, legs (minimum two), udder, tail).

/usr/share/cowsay/cows/ may be accessed, but obviously not /usr/games/cowsay itself.

Note: here is the /usr/share/cowsay/cows/default.cow file:

$the_cow = <<"EOC";
        $thoughts   ^__^
         $thoughts  ($eyes)\\_______
            (__)\\       )\\/\\
             $tongue ||----w |
                ||     ||
EOC

Usage of the file is optional. When used, it must be accessed by full path. (Windows users may copy it to something like C:\usr\share\cowsay\co\default.cow, for fairness).

Vi.

Posted 2013-04-11T21:34:28.287

Reputation: 2 644

Question was closed 2016-08-26T15:47:35.157

I'm voting to close this as off-topic because the specification for the format is extremely non-descriptive, which makes it impossible to determine validity of outputs. – Mego – 2016-08-26T07:24:42.817

Answers

26

Mathematica: Work in progress

I'll pin down the balloon once I can get the cow to settle:

ExampleData[{"Geometry3D", "Cow"}]

enter image description here

Dr. belisarius

Posted 2013-04-11T21:34:28.287

Reputation: 5 345

12this made my day. – acolyte – 2013-04-12T14:06:45.650

bahahahhahahahhaha – jamylak – 2013-04-13T09:24:27.960

7...is it done yet? – noɥʇʎԀʎzɐɹƆ – 2016-08-18T19:08:00.310

Any further development on that balloon? – Taylor Scott – 2017-05-16T23:16:23.367

16

Perl, 84 characters

print$a='-'x52,<>=~s/.{1,50}\b/sprintf"
|%-50s|",$&/rge,"$a
\\   
]:p
| )=
| P= 
 ¬"

Output:

----------------------------------------------------
|Long text Long text Long text Long text Long text |
|Long text Long text Long text Long text Long text |
|Long text Long text Long text Long text Long text |
|Long text Long text Long text Long text Long text |
----------------------------------------------------
\   
]:p
| )=
| P= 
 ¬

Admittedly, I golfed the cow as much as I golfed the code. But the Mathematica cow is going to win anyway :)

Note: Requires Perl 5.16 for the /r non-destructive substitution flag.

user7486

Posted 2013-04-11T21:34:28.287

Reputation:

Can you tell us please what is that r after the s///? Can not find such flag in the documentation. Thank you. – manatwork – 2013-04-12T15:53:55.107

3Upvoted. Not everyone is brave enough to call that "a cow" – Dr. belisarius – 2013-04-12T17:06:39.070

1Bow your head left to see the cow. – Vi. – 2013-04-12T17:37:47.820

@manatwork the r flag returns the result, instead of modifying the operand in place (<> is of course read-only, and can't be modified). – primo – 2013-04-12T18:04:53.903

Thank you for the explanation, Vi. @belisarius, it gets a dachshund look here because the line-height: 1.4em set for the code blocks, but in terminal it looks better.

– manatwork – 2013-04-12T18:15:08.523

@manatwork I've got it! :D – Dr. belisarius – 2013-04-13T01:32:11.963

5

Ruby: 152 150 149 146 143 characters

load'/usr/share/cowsay/cows/default.cow'
p=%w{U~ o* o o}
puts l=?-*44,gets.gsub(/(.{1,40})\b\s*/){"( %-41s)\n"%$1}+l,$the_cow.gsub(/\$\w+/){p.pop}

The cow art is read from the default.cow file and decorated in after-12-rounds-vs-Mike-Tyson manner, similar with a cowthink -e 'o*' -T 'U~' invocation.

The text is read from standard input and wrapped at most 40 characters. Line breaks in the input text are not supported.

Sample run:

bash-4.2$ perl -e 'print "Long text "x15' | ruby cow.rb 
--------------------------------------------
( Long text Long text Long text Long text  )
( Long text Long text Long text Long text  )
( Long text Long text Long text Long text  )
( Long text Long text Long text            )
--------------------------------------------
        o   ^__^
         o  (o*)\_______
            (__)\       )\/\
             U~ ||----w |
                ||     ||

With art bending: 138 135 136 characters

load'/usr/share/cowsay/cows/default.cow'
puts l=?-*44,gets.gsub(/(.{1,40})\b\s*/){"( %-41s)\n"%$1}+l,$the_cow.gsub(/\$.+?([use]+)\b/,'\1')

As the elements have to be just present, we can use what we have in place instead of defining realistic ones.

Sample run:

bash-4.2$ ruby cow.rb <<< 'Hello poor little cow'
--------------------------------------------
( Hello poor little cow                    )
--------------------------------------------
        s   ^__^
         s  (es)\_______
            (__)\       )\/\
             ue ||----w |
                ||     ||

manatwork

Posted 2013-04-11T21:34:28.287

Reputation: 17 865

2

APL 133 140 138 122

This one liner takes screen input in the form of a character vector or array enclosed in quotes via: ←⍎⍞ The appearance of the output is improved by including leading and following spaces in the input.

('∘'⍪'∘',((¯2↑1,⍴t)⍴t←⍎⍞),'∘')⍪'∘'⋄5 19⍴(,⍉(19⍴2)⊤323584 196592 63503 4072 3096)\'∘^__^∘(oo)\_______(__)\)\/\||----w|||||'

The cow character locations on each row of the array are specified as 19 bit boolean vectors.

To use input the one liner followed by the bubble text in quotes:

' Hello '

°°°°°°°°°
° Hello °
°°°°°°°°°
°  ^__^
 ° (oo)\_______
   (__)\       )\/\
       ||----w |
       ||     ||

Multi-line text is input as an array.

3 36⍴' Multi-line text input as an array. '

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
° Multi-line text input as an array. °
° Multi-line text input as an array. °
° Multi-line text input as an array. °
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
°  ^__^
 ° (oo)\_______
   (__)\       )\/\
       ||----w |
       ||     ||

This version can be tested via the free APL interpreter at www.nars2000.org

Graham

Posted 2013-04-11T21:34:28.287

Reputation: 3 184

How to properly count the characters? I counted 171 UTF-8 bytes and 141 Unicode codepoints here (newlines included). /* Also haven't found what I need to apt-get install to run APL... */ – Vi. – 2013-04-12T11:07:48.873

1

@Vi. The APL+WIN v5 character set is based on single byte characters. I have to convert them to UTF-8 to get them to render properly on this site. Therefore I believe I am justified in counting characters including newline characters as single bytes. I am not sure how you can run APL. I normally recommend the free version at http://www.nars2000.org but it does not appear to support relative assign.

– Graham – 2013-04-12T11:18:16.097

2

K, 178

{-1'g,({"+ ",x,((&/(c-4;60-#x))#" ")," +"}'l),(g:,(&/(64;4+c::#*l:(60*!-_-(#x)%60)_x))#"+"),("+  ^__^";" + (oo)\\_______";"   (__)\\       )\\/\\";"       ||----w |";"       ||     ||");}

.

k){-1'g,({"+ ",x,((c-#x)#" ")," +"}'l),(g:,(&/(64;4+c::#*l:(60*!-_-(#x)%60)_x))#"+"),("+  ^__^";" + (oo)\\_______";"   (__)\\       )\\/\\";"       ||----w |";"       ||     ||");}"Hello codegolf.stackexchange.com"
++++++++++++++++++++++++++++++++++++
+ Hello codegolf.stackexchange.com +
++++++++++++++++++++++++++++++++++++
+  ^__^
 + (oo)\_______
   (__)\       )\/\
       ||----w |
       ||     ||

tmartin

Posted 2013-04-11T21:34:28.287

Reputation: 3 917