Skip to content

Instantly share code, notes, and snippets.

@roktas
Created April 17, 2014 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roktas/10995974 to your computer and use it in GitHub Desktop.
Save roktas/10995974 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# Ascii logo for the forthcoming Dotunderscore
require 'erb'
require 'ostruct'
require 'term/ansicolor'
# Object core extensions from ActiveSupport
class Object #:nodoc:
def try(method, *args, &block)
send(method, *args, &block)
end
remove_method :try
alias_method :try, :__send__
end
# NilClass core extensions from ActiveSupport
class NilClass #:nodoc:
def try(*args)
nil
end
end
# String core extensions from ActiveSupport
class String #:nodoc:
def strip_heredoc
indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
gsub(/^[ \t]{#{indent}}/, '')
end
end
# Process logo
class Logo < ERB
include Term::ANSIColor
LOGO_FILE = 'logo'
# rubocop:disable LineLength, MethodLength
def self.template
<<-'TEMPLATE'.strip_heredoc
<%= @dot.front %>____<%= @none %>
<%= @dot.front %>/\<%= @none %> <%= @dot.back %>/<%= @dot.front %>\<%= @none %>
<%= @dot.front %>/<%= @none %><%= @dot.back %>_<%= @none %> <%= @dot.front %>\___\<%= @none %>
<%= @dot.front %>\<%= @none %> <%= @dot.front %>/<%= @none %> <%= @dot.front %>/<%= @none %>
<%= @dot.front %>\/__<%= @none %><%= @dot.back %>\<%= @none %><%= @dot.front %>/<%= @none %> <%= @underscore.front %>______________<%= @none %>
<%= @underscore.front %>/\<%= @none %> <%= @underscore.back %>/<%= @none %><%= @underscore.front %>\<%= @none %>
<%= @underscore.front %>/<%= @none %><%= @underscore.back %>_<%= @none %> <%= @underscore.front %>\_____________\<%= @none %>
<%= @underscore.front %>\<%= @none %> <%= @underscore.front %>/<%= @none %> <%= @underscore.front %>/<%= @none %>
<%= @underscore.front %>\/____________<%= @none %><%= @underscore.back %>\<%= @none %><%= @underscore.front %>/<%= @none %>
TEMPLATE
end
def self.default
new(dot: :cyan, underscore: :yellow)
end
def self.plain
new
end
def self.method_missing(meth, *args)
colors = meth.to_s.split('_').map(&:to_sym)
if colors.length == 2 && colors.all? { |c| method_defined? c }
return new(dot: colors[0], underscore: colors[1])
end
super
end
attr_accessor :dot, :underscore, :none
def initialize(*args)
setup(*args)
super(self.class.template)
end
def save(outfile = LOGO_FILE)
content = result
File.open(outfile, 'w') do |f|
f.write content
end
content
end
def result
super(binding)
end
def to_s
result
end
private
def setup(arg = {})
[:dot, :underscore].each do |attr|
front = back = main = arg[attr] ? send(arg[attr]) : ''
unless main.empty?
front += bold
back += dark
end
instance_variable_set :"@#{attr}",
OpenStruct.new(front: front, back: back)
end
@none = arg.empty? ? '' : reset
end
end
if __FILE__ == $PROGRAM_NAME
logo =
if ARGV.empty?
Logo.default
else
Logo.send ARGV[0].to_sym
end
puts logo
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment