From 7e27d18f1da9c38e053de707a54f4710bb8ab5af Mon Sep 17 00:00:00 2001 From: technomancy Date: Tue, 9 Oct 2007 04:31:09 +0000 Subject: [PATCH] outputs coloring for dummy backend git-svn-id: svn+ssh://rubyforge.org/var/svn/augment@2 433cabc8-d307-4f6b-84b0-56fd43cf9f23 --- Rakefile | 4 +++ html/index.html | 52 ++++++++++++++++++++++++++++++++++++ lib/augment.rb | 15 ++++++++++- lib/backends/backend.rb | 16 +++++++++++ lib/backends/coloring_backend.rb | 24 +++++++++++++++++ lib/frontends/ansi_color_frontend.rb | 31 +++++++++++++++++++++ spec/augment_spec.rb | 33 +++++++++++++++-------- spec/fixtures/drinks/lib/drink.rb | 15 +++++++++++ 8 files changed, 178 insertions(+), 12 deletions(-) create mode 100644 html/index.html create mode 100644 lib/backends/backend.rb create mode 100644 lib/backends/coloring_backend.rb create mode 100644 lib/frontends/ansi_color_frontend.rb create mode 100644 spec/fixtures/drinks/lib/drink.rb diff --git a/Rakefile b/Rakefile index a39e64a..0c2271a 100644 --- a/Rakefile +++ b/Rakefile @@ -14,4 +14,8 @@ Hoe.new('augment', Augment::VERSION) do |p| p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n") end +task :publish_html do + system("scp -r html/* technomancy@rubyforge.org:/var/www/gforge-projects/augment/") +end + # vim: syntax=Ruby diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..fe0d340 --- /dev/null +++ b/html/index.html @@ -0,0 +1,52 @@ + + + + + Augment + + + + + +
+ + + sample + +

Augment is a system for gathering metadata from code and + displaying it. This metadata would include test failures, test + coverage levels, complexity metrics, and others. Display + frontends will be pluggable so as to interface with many + editors.

+ + +
+ + diff --git a/lib/augment.rb b/lib/augment.rb index 1a57099..2d1ef3e 100644 --- a/lib/augment.rb +++ b/lib/augment.rb @@ -1,3 +1,16 @@ +$LOAD_PATH << File.dirname(__FILE__) +$LOAD_PATH << File.dirname(__FILE__) + '/backends' +$LOAD_PATH << File.dirname(__FILE__) + '/frontends' + +require 'rubygems' +require 'json' + +require 'backend' +require 'layer' + +Dir.glob(File.dirname(__FILE__) + '/backends/*rb').each { |b| require b.gsub('.rb', '') } + class Augment VERSION = '1.0.0' -end \ No newline at end of file + +end diff --git a/lib/backends/backend.rb b/lib/backends/backend.rb new file mode 100644 index 0000000..48b6793 --- /dev/null +++ b/lib/backends/backend.rb @@ -0,0 +1,16 @@ +class Backend + class << self + def write_layers + FileUtils.mkpath('.augment') + @layers.each do |file, layers| + File.open(augment_file(file), 'w') do |f| + f.puts "[#{layers.map{ |l| l.to_json }.join(", \n")}]" + end + end + end + + def augment_file(file) + '.augment/' + file.gsub(/[^a-zA-Z]/, '_') + end + end +end diff --git a/lib/backends/coloring_backend.rb b/lib/backends/coloring_backend.rb new file mode 100644 index 0000000..3adec6f --- /dev/null +++ b/lib/backends/coloring_backend.rb @@ -0,0 +1,24 @@ +class ColoringBackend < Backend + COLORS = ['white', 'red', 'green', 'blue', 'brown'] + + class << self + attr_reader :layers + + def run(file) + @layers = {} + text = File.read(file) + + COLORS.each do |color| + offset = 0 + while occurance = text.index(color, offset) do + @layers[file] ||= [] + @layers[file] << Layer.new((occurance .. occurance + color.length), + color, "Found a #{color}") + offset += (occurance + 1) + end + end + + write_layers + end + end +end diff --git a/lib/frontends/ansi_color_frontend.rb b/lib/frontends/ansi_color_frontend.rb new file mode 100644 index 0000000..04117cd --- /dev/null +++ b/lib/frontends/ansi_color_frontend.rb @@ -0,0 +1,31 @@ +class String + COLOR_LOOKUP = {'black' => 30, + 'red' => 31, + 'green' => 32, + 'yellow' => 33, + 'blue' => 34, + 'magenta' => 35, + 'cyan' => 36, + 'white' => 37, + 'default' => 38 } + + def colorize(color_code) + "\e[#{String.lookup_color_code(color_code)}m#{self}\e[0m" + end + + def colorize_range(range, color) + "#{self[0 .. range.begin]}#{self[range].colorize(color)}#{self[range.end .. -1]}" + end + + def self.lookup_color_code(color, foreground = true) + return color if color.is_a? Fixnum + COLOR_LOOKUP[color.downcase] + end +end + +class AnsiColorFrontend + class << self + def run(file) + end + end +end diff --git a/spec/augment_spec.rb b/spec/augment_spec.rb index 39ca10f..9b4e3c8 100644 --- a/spec/augment_spec.rb +++ b/spec/augment_spec.rb @@ -1,26 +1,37 @@ $LOAD_PATH << File.dirname(__FILE__) + '/../lib' require 'augment' -require 'coloring_augmentor' -require 'spec_helper' -describe AugmentBackend, " when augmenting by color" do +PROJECT_ROOT = File.expand_path(File.dirname(__FILE__) + '/fixtures/drinks/') + +describe Backend, " when augmenting by color" do before do - FileUtils.cd(File.dirname(__FILE__) + '/fixtures/drinks') + FileUtils.cd(PROJECT_ROOT) FileUtils.rm_r('.augment') rescue nil - ColoringAugmentor.run('lib/drink.rb') - ColoringAugmentor.run('lib/white_russian.rb') + ColoringBackend.run('lib/drink.rb') end - it "should create .augment directory" do + it "should create .augment directory and files" do File.should exist('.augment') + File.should exist('.augment/lib_drink_rb') end - it "should create .augment files for each code file" - it "should color red things red" + it "should color the colors and ranges" do + output = JSON.parse(File.read('.augment/lib_drink_rb')) + output.size.should == 4 + output[0]['color'].should == 'red' + output[1]['color'].should == 'green' + output[2]['color'].should == 'brown' + output[3]['color'].should == 'brown' + + output[0]['range'].should == (371 .. 374).to_s + output[1]['range'].should == (456 .. 461).to_s + output[2]['range'].should == (291 .. 296).to_s + output[3]['range'].should == (528 .. 533).to_s + end end -describe AugmentBackend, " when augmenting test results" do +describe Backend, " when augmenting test results" do it "should color passing tests green" it "should color failing tests red" @@ -29,7 +40,7 @@ describe AugmentBackend, " when augmenting test results" do it "should highlight specific line" end -describe AugmentBackend, " when augmenting flog results" do +describe Backend, " when augmenting flog results" do it "should color a complex method" it "should color a simple method" diff --git a/spec/fixtures/drinks/lib/drink.rb b/spec/fixtures/drinks/lib/drink.rb new file mode 100644 index 0000000..078c189 --- /dev/null +++ b/spec/fixtures/drinks/lib/drink.rb @@ -0,0 +1,15 @@ +class Drink + attr_accessor :name, :proof, :color + + def initialize args + @name = args[:name] + @proof = args[:proof] + @color = args[:color] + end +end + +Vodka = Drink.new :name => 'Vodka', :proof => 80, :color => 'clear' +Kahlua = Drink.new :name => 'Kahlua', :proof => 40, :color => 'brown' +TomatoJuice = Drink.new :name => 'Tomato Juice', :proof => 0, :color => 'red' +MikesHardLime = Drink.new :name => 'Mike\'s Hard Lime', :proof => 8, :color => 'green' +Whisky = Drink.new :name => 'Jim Beam', :proof => 80, :color => 'brown' -- 2.11.4.GIT