12
\$\begingroup\$

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).

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – user45941
    Commented Aug 26, 2016 at 7:24

5 Answers 5

29
\$\begingroup\$

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

\$\endgroup\$
2
  • 11
    \$\begingroup\$ ...is it done yet? \$\endgroup\$ Commented Aug 18, 2016 at 19:08
  • 1
    \$\begingroup\$ Any further development on that balloon? \$\endgroup\$ Commented May 16, 2017 at 23:16
16
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ Can you tell us please what is that r after the s///? Can not find such flag in the documentation. Thank you. \$\endgroup\$
    – manatwork
    Commented Apr 12, 2013 at 15:53
  • 3
    \$\begingroup\$ Upvoted. Not everyone is brave enough to call that "a cow" \$\endgroup\$ Commented Apr 12, 2013 at 17:06
  • 1
    \$\begingroup\$ Bow your head left to see the cow. \$\endgroup\$
    – Vi.
    Commented Apr 12, 2013 at 17:37
  • \$\begingroup\$ @manatwork the r flag returns the result, instead of modifying the operand in place (<> is of course read-only, and can't be modified). \$\endgroup\$
    – primo
    Commented Apr 12, 2013 at 18:04
  • \$\begingroup\$ 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. \$\endgroup\$
    – manatwork
    Commented Apr 12, 2013 at 18:15
5
\$\begingroup\$

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 |
                ||     ||
\$\endgroup\$
0
2
\$\begingroup\$

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 |
       ||     ||
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 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... */ \$\endgroup\$
    – Vi.
    Commented Apr 12, 2013 at 11:07
  • 1
    \$\begingroup\$ @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 nars2000.org but it does not appear to support relative assign. \$\endgroup\$
    – Graham
    Commented Apr 12, 2013 at 11:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.