├── Rakefile ├── lib ├── cowsay │ ├── version.rb │ ├── character.rb │ └── character │ │ ├── bunny.rb │ │ ├── koala.rb │ │ ├── cow.rb │ │ ├── tux.rb │ │ ├── moose.rb │ │ ├── sheep.rb │ │ ├── ren.rb │ │ ├── elephant.rb │ │ ├── kitty.rb │ │ ├── frogs.rb │ │ ├── stimpy.rb │ │ ├── cheese.rb │ │ ├── beavis.rb │ │ ├── daemon.rb │ │ ├── stegosaurus.rb │ │ ├── dragon.rb │ │ ├── ghostbusters.rb │ │ ├── turtle.rb │ │ ├── turkey.rb │ │ └── base.rb └── cowsay.rb ├── Gemfile ├── .gitignore ├── README.md ├── bin └── cowsay ├── cowsay.gemspec └── LICENSE.txt /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | -------------------------------------------------------------------------------- /lib/cowsay/version.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | VERSION = '0.3.0' 3 | end 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in zerobuf.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /lib/cowsay/character.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | autoload :Base, 'cowsay/character/base' 4 | end 5 | end 6 | 7 | Dir[File.expand_path('character/*.rb', File.dirname(__FILE__))].each do |character| 8 | require character 9 | end 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | *.swp 4 | .DS_Store 5 | .bundle 6 | .config 7 | .vagrant 8 | .yardoc 9 | Gemfile.lock 10 | InstalledFiles 11 | _yardoc 12 | coverage 13 | doc/ 14 | lib/bundler/man 15 | pkg 16 | rdoc 17 | spec/reports 18 | test/tmp 19 | test/version_tmp 20 | tmp 21 | -------------------------------------------------------------------------------- /lib/cowsay/character/bunny.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Bunny < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} \\ 9 | \\ /\\ 10 | ( ) 11 | .( o ). 12 | TEMPLATE 13 | end 14 | end 15 | 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /lib/cowsay/character/koala.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Koala < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | ___ 10 | {~._.~} 11 | ( Y ) 12 | ()~*~() 13 | (_)-(_) 14 | TEMPLATE 15 | end 16 | end 17 | 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/cowsay/character/cow.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Cow < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} ^__^ 8 | #{@thoughts} (oo)\\_______ 9 | (__)\\ )\\/\\ 10 | ||----w | 11 | || || 12 | TEMPLATE 13 | end 14 | end 15 | 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /lib/cowsay/character/tux.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Tux < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | .--. 10 | |o_o | 11 | |:_/ | 12 | // \\ \\ 13 | (| | ) 14 | /'\\_ _/`\\ 15 | \\___)=(___/ 16 | TEMPLATE 17 | end 18 | end 19 | 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /lib/cowsay/character/moose.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Moose < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} \\_\\_ _/_/ 9 | #{@thoughts} \\__/ 10 | (oo)\\_______ 11 | (__)\\ )\\/\\ 12 | ||----w | 13 | || || 14 | TEMPLATE 15 | end 16 | end 17 | 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/cowsay/character/sheep.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Sheep < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | __ 10 | UooU\\.'\@\@\@\@\@\@`. 11 | \\__/(\@\@\@\@\@\@\@\@\@\@) 12 | (\@\@\@\@\@\@\@\@) 13 | `YY~~~~YY' 14 | || || 15 | TEMPLATE 16 | end 17 | end 18 | 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/cowsay/character/ren.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Ren < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | ____ 10 | /# /_\\_ 11 | | |/o\\o\\ 12 | | \\\\_/_/ 13 | / |_ | 14 | | ||\\_ ~| 15 | | ||| \\/ 16 | | |||_ 17 | \\// | 18 | || | 19 | ||_ \\ 20 | \\_| o| 21 | /\\___/ 22 | / ||||__ 23 | (___)_) 24 | TEMPLATE 25 | end 26 | end 27 | 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/cowsay/character/elephant.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Elephant < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} /\\ ___ /\\ 8 | #{@thoughts} // \\/ \\/ \\\\ 9 | (( O O )) 10 | \\\\ / \\ // 11 | \\/ | | \\/ 12 | | | | | 13 | | | | | 14 | | o | 15 | | | | | 16 | |m| |m| 17 | TEMPLATE 18 | end 19 | end 20 | 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/cowsay/character/kitty.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Kitty < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | ("`-' '-/") .___..--' ' "`-._ 10 | ` *_ * ) `-. ( ) .`-.__. `) 11 | (_Y_.) ' ._ ) `._` ; `` -. .-' 12 | _.. `--'_..-_/ /--' _ .' ,4 13 | ( i l ),-'' ( l i),' ( ( ! .-' 14 | TEMPLATE 15 | end 16 | end 17 | 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/cowsay/character/frogs.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Frogs < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | oO)-. .-(Oo 10 | /__ _\\ /_ __\\ 11 | \\ \\( | ()~() | )/ / 12 | \\__|\\ | (-___-) | /|__/ 13 | ' '--' ==`-'== '--' ' 14 | TEMPLATE 15 | end 16 | end 17 | 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cowsay 2 | 3 | ASCII art avatars emote your messages 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | gem install cowsay 10 | 11 | And then execute: 12 | 13 | $ cowsay 'Hello world!' 14 | 15 | ## Contributing 16 | 17 | 1. Fork it 18 | 2. Create your feature branch (`git checkout -b my-new-feature`) 19 | 3. Commit your changes (`git commit -am 'Add some feature'`) 20 | 4. Push to the branch (`git push origin my-new-feature`) 21 | 5. Create new Pull Request 22 | -------------------------------------------------------------------------------- /lib/cowsay/character/stimpy.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Stimpy < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} . _ . 8 | #{@thoughts} |\\_|/__/| 9 | / / \\/ \\ \\ 10 | /__|O||O|__ \\ 11 | |/_ \\_/\\_/ _\\ | 12 | | | (____) | || 13 | \\/\\___/\\__/ // 14 | (_/ || 15 | | || 16 | | ||\\ 17 | \\ //_/ 18 | \\______// 19 | __ || __|| 20 | (____(____) 21 | TEMPLATE 22 | end 23 | end 24 | 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/cowsay/character/cheese.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Cheese < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | _____ _________ 10 | / \\_/ | 11 | | || 12 | | || 13 | | ###\\ /### | | 14 | | 0 \\/ 0 | | 15 | /| | | 16 | / | < |\\ \\ 17 | | /| | | | 18 | | | \\_______/ | | | 19 | | | | / / 20 | /|| /||| 21 | ----------------| 22 | | | | | 23 | *** *** 24 | /___\\ /___\\ 25 | TEMPLATE 26 | end 27 | end 28 | 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /bin/cowsay: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'cowsay' 4 | require 'optparse' 5 | 6 | options = {} 7 | OptionParser.new do |opts| 8 | opts.banner = "Usage: cowsay [-h] [-f cowfile] [-l] [message]" 9 | opts.on("-l", "List available cow files") do |cowfile| 10 | options['list'] = true 11 | end 12 | opts.on("-f COWFILE", "Specify a cow file") do |cowfile| 13 | options['cowfile'] = cowfile 14 | end 15 | end.parse! 16 | 17 | if options['list'] 18 | puts "Cow files:" 19 | puts Cowsay.character_classes.join(' ') 20 | else 21 | if ARGV.any? 22 | message = ARGV.join(' ') 23 | else 24 | #retrieve any piped input, otherwise use the empty string. 25 | message = STDIN.tty? ? '' : ARGF.read.chomp 26 | end 27 | puts Cowsay.say(message, options['cowfile']) 28 | end 29 | -------------------------------------------------------------------------------- /lib/cowsay/character/beavis.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Beavis < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} __------~~-, 8 | #{@thoughts} ,' , 9 | / \\ 10 | / : 11 | | ' 12 | | | 13 | | | 14 | | _-- | 15 | _| =-. .-. || 16 | o|/o/ _. | 17 | / ~ \\ | 18 | (____\@) ___~ | 19 | |_===~~~.` | 20 | _______.--~ | 21 | \\________ | 22 | \\ | 23 | __/-___-- -__ 24 | / _ \\ 25 | 26 | TEMPLATE 27 | end 28 | end 29 | 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/cowsay/character/daemon.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Daemon < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} , , 8 | #{@thoughts} /( )` 9 | #{@thoughts} \\ \\___ / | 10 | /- _ `-/ ' 11 | (/\\/ \\ \\ /\\ 12 | / / | ` \\ 13 | O O ) / | 14 | `-^--'`< ' 15 | (_.) _ ) / 16 | `.___/` / 17 | `-----' / 18 | <----. __ / __ \\ 19 | <----|====O)))==) \\) /==== 20 | <----' `--' `.__,' \\ 21 | | | 22 | \\ / 23 | ______( (_ / \\______ 24 | ,' ,-----' | \\ 25 | `--{__________) \\/ 26 | TEMPLATE 27 | end 28 | end 29 | 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /cowsay.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'cowsay/version' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = 'cowsay' 8 | gem.version = Cowsay::VERSION 9 | gem.authors = ['JohnnyT'] 10 | gem.email = ['johnnyt@moneydesktop.com'] 11 | gem.description = %q{ASCII art avatars emote your messages} 12 | gem.summary = gem.description 13 | gem.homepage = 'https://github.com/moneydesktop/cowsay' 14 | 15 | gem.files = `git ls-files`.split($/) 16 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 17 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 18 | gem.require_paths = ['lib'] 19 | 20 | gem.add_development_dependency 'rake' 21 | # gem.add_development_dependency 'rspec-pride' 22 | end 23 | -------------------------------------------------------------------------------- /lib/cowsay.rb: -------------------------------------------------------------------------------- 1 | require 'cowsay/version' 2 | require 'cowsay/character' 3 | 4 | module ::Cowsay 5 | module_function # all instance methods are available on the module (class) level 6 | 7 | def random_character 8 | random_class = Character.const_get(character_classes[rand(character_classes.length)]) 9 | random_class.new 10 | end 11 | 12 | def character_classes 13 | @character_classes ||= Character.constants.map { |c| c.to_sym } - [:Base, :Template] 14 | end 15 | 16 | def say(message, character) 17 | character ||= 'cow' 18 | if character == 'random' 19 | random_character.say(message) 20 | else 21 | if character_classes.include? character.capitalize.to_sym 22 | Character.const_get(character.capitalize).say(message) 23 | else 24 | puts "No cow file found for #{character}. Use the -l flag to see a list of available cow files." 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/cowsay/character/stegosaurus.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Stegosaurus < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} . . 8 | #{@thoughts} / `. .' " 9 | #{@thoughts} .---. < > < > .---. 10 | #{@thoughts} | \\ \\ - ~ ~ - / / | 11 | _____ ..-~ ~-..-~ 12 | | | \\~~~\\.' `./~~~/ 13 | --------- \\__/ \\__/ 14 | .' O \\ / / \\ " 15 | (_____, `._.' | } \\/~~~/ 16 | `----. / } | / \\__/ 17 | `-. | / | / `. ,~~| 18 | ~-.__| /_ - ~ ^| /- _ `..-' 19 | | / | / ~-. `-. _ _ _ 20 | |_____| |_____| ~ - . _ _ _ _ _> 21 | TEMPLATE 22 | end 23 | end 24 | 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/cowsay/character/dragon.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Dragon < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} / \\ //\\ 8 | #{@thoughts} |\\___/| / \\// \\\\ 9 | /0 0 \\__ / // | \\ \\ 10 | / / \\/_/ // | \\ \\ 11 | \@_^_\@'/ \\/_ // | \\ \\ 12 | //_^_/ \\/_ // | \\ \\ 13 | ( //) | \\/// | \\ \\ 14 | ( / /) _|_ / ) // | \\ _\\ 15 | ( // /) '/,_ _ _/ ( ; -. | _ _\\.-~ .-~~~^-. 16 | (( / / )) ,-{ _ `-.|.-~-. .~ `. 17 | (( // / )) '/\\ / ~-. _ .-~ .-~^-. \\ 18 | (( /// )) `. { } / \\ \\ 19 | (( / )) .----~-.\\ \\-' .~ \\ `. \\^-. 20 | ///.----..> \\ _ -~ `. ^-` ^-_ 21 | ///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~ 22 | TEMPLATE 23 | end 24 | end 25 | 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2012 MoneyDesktop Inc. 2 | Copyright Cowsay contributors https://github.com/moneydesktop/cowsay/contributors 3 | 4 | MIT License 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /lib/cowsay/character/ghostbusters.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Ghostbusters < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} 8 | #{@thoughts} 9 | #{@thoughts} __---__ 10 | _- /--______ 11 | __--( / \\ )XXXXXXXXXXX\\v. 12 | .-XXX( O O )XXXXXXXXXXXXXXX- 13 | /XXX( U ) XXXXXXX\\ 14 | /XXXXX( )--_ XXXXXXXXXXX\\ 15 | /XXXXX/ ( O ) XXXXXX \\XXXXX\\ 16 | XXXXX/ / XXXXXX \\__ \\XXXXX 17 | XXXXXX__/ XXXXXX \\__----> 18 | ---___ XXX__/ XXXXXX \\__ / 19 | \\- --__/ ___/\\ XXXXXX / ___--/= 20 | \\-\\ ___/ XXXXXX '--- XXXXXX 21 | \\-\\/XXX\\ XXXXXX /XXXXX 22 | \\XXXXXXXXX \\ /XXXXX/ 23 | \\XXXXXX > _/XXXXX/ 24 | \\XXXXX--__/ __-- XXXX/ 25 | -XXXXXXXX--------------- XXXXXX- 26 | \\XXXXXXXXXXXXXXXXXXXXXXXXXX/ 27 | ""VXXXXXXXXXXXXXXXXXXV"" 28 | TEMPLATE 29 | end 30 | end 31 | 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/cowsay/character/turtle.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Turtle < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} ___-------___ 8 | #{@thoughts} _-~~ ~~-_ 9 | #{@thoughts} _-~ /~-_ 10 | /^\\__/^\\ /~ \\ / \\ 11 | /| O|| O| / \\_______________/ \\ 12 | | |___||__| / / \\ \\ 13 | | \\ / / \\ \\ 14 | | (_______) /______/ \\_________ \\ 15 | | / / \\ / \\ 16 | \\ \\^\\\\ \\ / \\ / 17 | \\ || \\______________/ _-_ //\\__// 18 | \\ ||------_-~~-_ ------------- \\ --/~ ~\\ || __/ 19 | ~-----||====/~ |==================| |/~~~~~ 20 | (_(__/ ./ / \\_\\ \\. 21 | (_(___/ \\_____)_) 22 | TEMPLATE 23 | end 24 | end 25 | 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/cowsay/character/turkey.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Turkey < Base 5 | def template 6 | <<-TEMPLATE 7 | #{@thoughts} ,+*^^*+___+++_ 8 | #{@thoughts} ,*^^^^ ) 9 | #{@thoughts} _+* ^**+_ 10 | #{@thoughts} +^ _ _++*+_+++_, ) 11 | _+^^*+_ ( ,+*^ ^ \\+_ ) 12 | { ) ( ,( ,_+--+--, ^) ^\\ 13 | { (\@) } f ,( ,+-^ __*_*_ ^^\\_ ^\\ ) 14 | {:;-/ (_+*-+^^^^^+*+*<_ _++_)_ ) ) / 15 | ( / ( ( ,___ ^*+_+* ) < < \\ 16 | U _/ ) *--< ) ^\\-----++__) ) ) ) 17 | ( ) _(^)^^)) ) )\\^^^^^))^*+/ / / 18 | ( / (_))_^)) ) ) ))^^^^^))^^^)__/ +^^ 19 | ( ,/ (^))^)) ) ) ))^^^^^^^))^^) _) 20 | *+__+* (_))^) ) ) ))^^^^^^))^^^^^)____*^ 21 | \\ \\_)^)_)) ))^^^^^^^^^^))^^^^) 22 | (_ ^\\__^^^^^^^^^^^^))^^^^^^^) 23 | ^\\___ ^\\__^^^^^^))^^^^^^^^)\\\\ 24 | ^^^^^\\uuu/^^\\uuu/^^^^\\^\\^\\^\\^\\^\\^\\^\\ 25 | ___) >____) >___ ^\\_\\_\\_\\_\\_\\_\\) 26 | ^^^//\\\\_^^//\\\\_^ ^(\\_\\_\\_\\) 27 | ^^^ ^^ ^^^ ^ 28 | TEMPLATE 29 | end 30 | end 31 | 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/cowsay/character/base.rb: -------------------------------------------------------------------------------- 1 | module Cowsay 2 | module Character 3 | 4 | class Base 5 | MAX_LINE_LENGTH = 36 unless defined?(MAX_LINE_LENGTH) 6 | 7 | def self.say(message) 8 | new.say(message) 9 | end 10 | 11 | def initialize 12 | @thoughts = '\\' 13 | end 14 | 15 | def say(message) 16 | render_balloon(message) + render_character 17 | end 18 | 19 | def template 20 | raise '#template should be subclassed' 21 | end 22 | 23 | private 24 | 25 | def render_character 26 | template 27 | end 28 | 29 | def render_balloon(message) 30 | message_lines = format_message(message) 31 | line_length = message_lines.max{ |a,b| a.length <=> b.length }.length 32 | 33 | output_lines = [] 34 | 35 | output_lines << " #{'_' * (line_length + 2)} " 36 | 37 | message_lines.each do |line| 38 | # 'Here is your message: %s' % 'hello world' 39 | # is the same as 40 | # printf('Here is your message: %s', 'hello world') 41 | output_lines << "| %-#{line_length}s |" % line 42 | end 43 | 44 | output_lines << " #{'-' * (line_length + 2)} " 45 | output_lines << '' 46 | 47 | output_lines.join("\n") 48 | end 49 | 50 | def format_message(message) 51 | return [message] if message.length <= MAX_LINE_LENGTH 52 | 53 | lines = [] 54 | words = message.split(/\s/).reject{ |word| word.length.zero? } 55 | new_line = '' 56 | 57 | words.each do |word| 58 | new_line << "#{word} " 59 | 60 | if new_line.length > MAX_LINE_LENGTH 61 | lines << new_line.chomp 62 | new_line = '' 63 | end 64 | end 65 | 66 | lines << new_line.chomp unless new_line.length.zero? 67 | 68 | lines 69 | end 70 | end 71 | 72 | end 73 | end 74 | --------------------------------------------------------------------------------