Skip to content
Browse files

split Base into CLI and Base so that Artii can be used outside of the…

… command-line script
  • Loading branch information...
1 parent 0ef093e commit 348c4beac3bb22368b3095f8e3856ce449775bb1 @muffinista muffinista committed Apr 26, 2012
Showing with 176 additions and 71 deletions.
  1. +3 −0 Gemfile
  2. +6 −0 Gemfile.lock
  3. +1 −1 bin/artii
  4. +1 −0 lib/artii.rb
  5. +20 −35 lib/artii/base.rb
  6. +51 −0 lib/artii/cli.rb
  7. +14 −35 spec/artii/base_spec.rb
  8. +80 −0 spec/artii/cli_spec.rb
View
3 Gemfile
@@ -1,5 +1,8 @@
source "http://rubygems.org"
+# Specify your gem's dependencies in gopher.gemspec
+gemspec
+
group :development do
gem "rspec", "~> 2.3.0"
gem "bundler", "~> 1.0.0"
View
6 Gemfile.lock
@@ -1,3 +1,8 @@
+PATH
+ remote: .
+ specs:
+ artii (1.0.3)
+
GEM
remote: http://rubygems.org/
specs:
@@ -22,6 +27,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ artii!
bundler (~> 1.0.0)
jeweler (~> 1.6.4)
rcov
View
2 bin/artii
@@ -3,7 +3,7 @@
require 'artii'
begin
- a = Artii::Base.new *ARGV.dup
+ a = Artii::CLI.new *ARGV.dup
puts a.output
rescue StandardError => e
puts "Something has gone horribly wrong!"
View
1 lib/artii.rb
@@ -12,6 +12,7 @@
# Copyright:: Copyright (c) 2010 Michael Tierney
# License:: Distributed under the MIT License
+require 'artii/cli'
require 'artii/base'
require 'artii/figlet'
View
55 lib/artii/base.rb
@@ -1,56 +1,41 @@
require 'find'
-require 'optparse'
require 'figlet/font'
require 'figlet/smusher'
require 'figlet/typesetter'
module Artii
class Base
- attr_accessor :output, :font, :font_name, :faces
+ attr_accessor :font, :faces
- def initialize(*args)
- @options = {}
- @output = ''
- @faces = all_fonts
-
- OptionParser.new do |opts|
- opts.banner = "Usage: artii 'your string here' [-f FONT_NAME or --font FONT_NAME] [-l or --list]"
-
- opts.on('-f', '--font FONT_NAME', 'Specify the font to be used (defaults to "big")') do |font|
- @font_name = @faces[font]
- end
-
- opts.on('-l', '--list', 'Prints the list of available fonts') do |list|
- @options[:list] = list_all_fonts
- end
-
- opts.on_tail("-h", "--help", "Show this message") do
- puts opts
- exit
- end
-
- if args.empty?
- puts opts
- exit
- end
- end.parse!(args)
-
- if @options[:list]
- @output = list_all_fonts
+ def initialize(params={})
+ if params.is_a?(String)
+ params = {
+ :text => params
+ }
end
- @font_name = @font_name.nil? ? @faces['big'] : @font_name
+ @options = {
+ :font => "big"
+ }.merge(params)
- @font = Artii::Figlet::Font.new("#{FONTPATH}/#{@font_name}")
+ @faces = all_fonts
+ @font = Artii::Figlet::Font.new font_file(@options[:font])
+ end
+
+ def font_name
+ @faces[@options[:font]]
+ end
- asciify args.first unless args.empty?
+ def font_file(name)
+ "#{FONTPATH}/#{@faces[name]}"
end
def asciify(string)
figlet = Artii::Figlet::Typesetter.new(@font)
- @output = figlet[string]
+ figlet[string]
end
+ alias :output :asciify
def list_all_fonts
font_list = "\n--------------------\nAll Available Fonts:\n--------------------\n\n"
View
51 lib/artii/cli.rb
@@ -0,0 +1,51 @@
+require 'optparse'
+
+module Artii
+ class CLI
+
+ attr_accessor :params, :action
+ def initialize(*args)
+ @params = {}
+ @action = :output
+
+ OptionParser.new do |opts|
+ opts.banner = "Usage: artii 'your string here' [-f FONT_NAME or --font FONT_NAME] [-l or --list]"
+
+ opts.on('-f', '--font FONT_NAME', 'Specify the font to be used (defaults to "big")') do |font|
+ @params[:font] = font
+ end
+
+ opts.on('-l', '--list', 'Prints the list of available fonts') do |list|
+ @action = :list_all_fonts
+ end
+
+ opts.on_tail("-h", "--help", "Show this message") do
+ puts opts
+ exit
+ end
+
+ if args.empty?
+ puts opts
+ exit
+ end
+ end.parse!(args)
+
+ @params[:text] = args.join " "
+
+ @a = Artii::Base.new(@params)
+ end
+
+ def font_name
+ @a.font_name
+ end
+
+ def output
+ if @action == :output
+ @a.send @action, @params[:text]
+ else
+ @a.send @action
+ end
+ end
+
+ end
+end
View
49 spec/artii/base_spec.rb
@@ -9,37 +9,37 @@
end
end
- it "font_name should default to 'big.flf'" do
- a = Artii::Base.new 'test'
- a.font_name.should == 'big.flf'
- end
+ it "font_name should default to 'big.flf'" do
+ a = Artii::Base.new 'test'
+ a.font_name.should == 'big.flf'
+ end
describe "font switching" do
context 'should set font if passed -f ' do
it "should set @font_name" do
- a = Artii::Base.new 'test', '-f', 'chunky'
- a.instance_variable_get(:@font_name).should == 'chunky.flf'
+ a = Artii::Base.new(:text => 'test', :font => 'chunky')
+ a.font_name.should == 'chunky.flf'
end
xit "should accept non-flf file formats" do
- a = Artii::Base.new 'test', '-f', 'frango'
+ a = Artii::Base.new(:text => 'test', :font => 'frango')
a.font_name.should == 'frango.flc'
end
it "should accept fonts in nested directories" do
- a = Artii::Base.new 'test', '-f', 'cour'
+ a = Artii::Base.new(:text => 'test', :font => 'cour')
a.font_name.should == 'bdffonts/cour.flf'
end
end
context 'should set font if passed --font' do
it "set @font_name" do
- a = Artii::Base.new 'test', '--font', 'chunky'
- a.instance_variable_get(:@font_name).should == 'chunky.flf'
+ a = Artii::Base.new(:text => 'test', :font => 'chunky')
+ a.font_name.should == 'chunky.flf'
end
it "should accept fonts in nested directories" do
- a = Artii::Base.new 'test', '--f', 'cour'
+ a = Artii::Base.new(:text => 'test', :font => 'cour')
a.font_name.should == 'bdffonts/cour.flf'
end
@@ -50,31 +50,10 @@
end
end
- # TODO - this is a pretty weak set of tests, but works for now since the list is a simple string.
- describe "available font list" do
- it "should list all fonts if passed -l" do
- a = Artii::Base.new '-l'
- a.output.should_not be_empty
- a.output.should include 'big'
- a.output.should include 'chunky'
- a.output.should include 'slant'
- a.output.should include 'helv'
- end
-
- it "should list all fonts if passed --list" do
- a = Artii::Base.new '--list'
- a.output.should_not be_empty
- a.output.should include 'big'
- a.output.should include 'chunky'
- a.output.should include 'slant'
- a.output.should include 'helv'
- end
- end
-
describe "#asciify" do
it "should produce an output" do
- a = Artii::Base.new 'hello world'
- a.output.should_not be_empty
+ a = Artii::Base.new
+ a.asciify('hello world').should_not be_empty
end
end
-end
+end
View
80 spec/artii/cli_spec.rb
@@ -0,0 +1,80 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe Artii::CLI do
+ describe '#new' do
+
+ it "should create a new instance" do
+ a = Artii::CLI.new ''
+ a.should be_an_instance_of Artii::CLI
+ end
+ end
+
+ it "font_name should default to 'big.flf'" do
+ a = Artii::CLI.new 'test'
+ a.font_name.should == 'big.flf'
+ end
+
+ describe "font switching" do
+ context 'should set font if passed -f ' do
+ it "should set @font_name" do
+ a = Artii::CLI.new 'test', '-f', 'chunky'
+ a.font_name.should == 'chunky.flf'
+ end
+
+ xit "should accept non-flf file formats" do
+ a = Artii::CLI.new 'test', '-f', 'frango'
+ a.font_name.should == 'frango.flc'
+ end
+
+ it "should accept fonts in nested directories" do
+ a = Artii::CLI.new 'test', '-f', 'cour'
+ a.font_name.should == 'bdffonts/cour.flf'
+ end
+ end
+
+ context 'should set font if passed --font' do
+ it "set @font_name" do
+ a = Artii::CLI.new 'test', '--font', 'chunky'
+ a.font_name.should == 'chunky.flf'
+ end
+
+ it "should accept fonts in nested directories" do
+ a = Artii::CLI.new 'test', '--f', 'cour'
+ a.font_name.should == 'bdffonts/cour.flf'
+ end
+
+ xit "should accept non-flf file formats" do
+ a = Artii::CLI.new 'test', '--font', 'frango'
+ a.font_name.should == 'frango.flc'
+ end
+ end
+ end
+
+ # TODO - this is a pretty weak set of tests, but works for now since the list is a simple string.
+ describe "available font list" do
+ it "should list all fonts if passed -l" do
+ a = Artii::CLI.new '-l'
+ a.output.should_not be_empty
+ a.output.should include 'big'
+ a.output.should include 'chunky'
+ a.output.should include 'slant'
+ a.output.should include 'helv'
+ end
+
+ it "should list all fonts if passed --list" do
+ a = Artii::CLI.new '--list'
+ a.output.should_not be_empty
+ a.output.should include 'big'
+ a.output.should include 'chunky'
+ a.output.should include 'slant'
+ a.output.should include 'helv'
+ end
+ end
+
+ describe "#asciify" do
+ it "should produce an output" do
+ a = Artii::CLI.new 'hello world'
+ a.output.should_not be_empty
+ end
+ end
+end

0 comments on commit 348c4be

Please sign in to comment.
Something went wrong with that request. Please try again.