formatador-0.2.1/0000755000175000017500000000000011713747534012642 5ustar bigonbigonformatador-0.2.1/metadata.yml0000644000175000017500000000364611713747534015156 0ustar bigonbigon--- !ruby/object:Gem::Specification name: formatador version: !ruby/object:Gem::Version hash: 21 prerelease: segments: - 0 - 2 - 1 version: 0.2.1 platform: ruby authors: - geemus (Wesley Beary) autorequire: bindir: bin cert_chain: [] date: 2011-09-01 00:00:00 Z dependencies: - !ruby/object:Gem::Dependency name: rake prerelease: false requirement: &id001 !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version hash: 3 segments: - 0 version: "0" type: :development version_requirements: *id001 - !ruby/object:Gem::Dependency name: shindo prerelease: false requirement: &id002 !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version hash: 3 segments: - 0 version: "0" type: :development version_requirements: *id002 description: STDOUT text formatting email: geemus@gmail.com executables: [] extensions: [] extra_rdoc_files: - README.rdoc files: - Gemfile - README.rdoc - Rakefile - formatador.gemspec - lib/formatador.rb - lib/formatador/progressbar.rb - lib/formatador/table.rb - tests/basic_tests.rb - tests/table_tests.rb - tests/tests_helper.rb homepage: http://github.com/geemus/NAME licenses: [] post_install_message: rdoc_options: - --charset=UTF-8 require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version hash: 3 segments: - 0 version: "0" required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version hash: 3 segments: - 0 version: "0" requirements: [] rubyforge_project: formatador rubygems_version: 1.8.5 signing_key: specification_version: 2 summary: Ruby STDOUT text formatting test_files: [] formatador-0.2.1/tests/0000755000175000017500000000000011713747534014004 5ustar bigonbigonformatador-0.2.1/tests/tests_helper.rb0000644000175000017500000000047411713747534017037 0ustar bigonbigon$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'formatador' require 'rubygems' require 'shindo' def capture_stdout old_stdout = $stdout new_stdout = StringIO.new $stdout = new_stdout yield $stdout = old_stdout new_stdout.string end formatador-0.2.1/tests/table_tests.rb0000644000175000017500000000146011713747534016643 0ustar bigonbigonShindo.tests("Formatador") do output = <<-OUTPUT +---+ | \e[1ma\e[0m | +---+ | 1 | +---+ | 2 | +---+ OUTPUT tests("#display_table([{:a => 1}, {:a => 2}])").returns(output) do capture_stdout do Formatador.display_table([{:a => 1}, {:a => 2}]) end end output = <<-OUTPUT +--------+ | \e[1mheader\e[0m | +--------+ +--------+ OUTPUT tests("#display_table([], [:header])").returns(output) do capture_stdout do Formatador.display_table([], [:header]) end end output = <<-OUTPUT +--------+ | \e[1mheader\e[0m | +--------+ | | +--------+ OUTPUT tests("#display_table([{:a => 1}], [:header])").returns(output) do capture_stdout do Formatador.display_table([{:a => 1}], [:header]) end end end formatador-0.2.1/tests/basic_tests.rb0000644000175000017500000000107611713747534016640 0ustar bigonbigonShindo.tests("Formatador") do tests("#display_line(Formatador)").returns(" Formatador\n") do capture_stdout do Formatador.display_line('Formatador') end end output = <<-OUTPUT one two OUTPUT tests("#display_lines(['one', 'two']").returns(output) do capture_stdout do Formatador.display_lines(['one', 'two']) end end tests("#indent { display_line('Formatador') }").returns(" Formatador\n") do capture_stdout do Formatador.indent do Formatador.display_line('Formatador') end end end end formatador-0.2.1/lib/0000755000175000017500000000000011713747534013410 5ustar bigonbigonformatador-0.2.1/lib/formatador/0000755000175000017500000000000011713747534015546 5ustar bigonbigonformatador-0.2.1/lib/formatador/table.rb0000644000175000017500000000347511713747534017173 0ustar bigonbigonclass Formatador def display_table(hashes, keys = nil, &block) new_hashes = hashes.inject([]) do |accum,item| accum << :split unless accum.empty? accum << item end display_compact_table(new_hashes, keys, &block) end def display_compact_table(hashes, keys = nil, &block) headers = keys || [] widths = {} if hashes.empty? && keys for key in keys widths[key] = key.to_s.length end else for hash in hashes next unless hash.respond_to?(:keys) for key in hash.keys unless keys headers << key end widths[key] = [ length(key), widths[key] || 0, hash[key] && length(hash[key]) || 0].max end headers = headers.uniq end end if block_given? headers = headers.sort(&block) elsif !keys headers = headers.sort {|x,y| x.to_s <=> y.to_s} end split = "+" if headers.empty? split << '--+' else for header in headers widths[header] ||= length(header) split << ('-' * (widths[header] + 2)) << '+' end end display_line(split) columns = [] for header in headers columns << "[bold]#{header}[/]#{' ' * (widths[header] - header.to_s.length)}" end display_line("| #{columns.join(' | ')} |") display_line(split) for hash in hashes if hash.respond_to? :keys columns = [] for header in headers datum = hash[header] || '' columns << "#{datum}#{' ' * (widths[header] - length(datum))}" end display_line("| #{columns.join(' | ')} |") else if hash == :split display_line(split) end end nil end display_line(split) end private def length(value) value.to_s.gsub(PARSE_REGEX, '').length end end formatador-0.2.1/lib/formatador/progressbar.rb0000644000175000017500000000322311713747534020424 0ustar bigonbigonclass Formatador def redisplay_progressbar(current, total, options = {}) options = { :color => 'white', :width => 50, :new_line => true }.merge!(options) data = progressbar(current, total, options) if current < total redisplay(data) else redisplay("#{data}") if options[:new_line] new_line end @progressbar_started_at = nil end end private def progressbar(current, total, options) color = options[:color] started_at = options[:started_at] width = options[:width] output = [] if options[:label] output << options[:label] end # width # we are going to write a string that looks like " current/total" # It would be nice if it were left padded with spaces in such a way that # it puts the progress bar in a constant place on the page. This witdh # calculation allows for the "current" string to be up to two characters # longer than the "total" string without problems. eg- current = # 9.99, total = 10 padding = total.to_s.size * 2 + 3 output << "[#{color}]%#{padding}s[/]" % "#{current}/#{total}" percent = current.to_f / total.to_f percent = 0 if percent < 0 percent = 1 if percent > 1 done = '*' * (percent * width).ceil remaining = ' ' * (width - done.length) output << "[_white_]|[/][#{color}][_#{color}_]#{done}[/]#{remaining}[_white_]|[/]" if started_at elapsed = Time.now - started_at minutes = (elapsed / 60).truncate.to_s seconds = (elapsed % 60).truncate.to_s output << "#{minutes}:#{'0' if seconds.size < 2}#{seconds}" end output << '' output.join(' ') end end formatador-0.2.1/lib/formatador.rb0000644000175000017500000000575111713747534016103 0ustar bigonbigonrequire File.join(File.dirname(__FILE__), 'formatador', 'table') require File.join(File.dirname(__FILE__), 'formatador', 'progressbar') class Formatador VERSION = '0.2.1' STYLES = { :"\/" => "0", :reset => "0", :bold => "1", :underline => "4", :blink_slow => "5", :blink_fast => "6", :negative => "7", # invert color/color :normal => "22", :underline_none => "24", :blink_off => "25", :positive => "27", # revert color/color :black => "30", :red => "31", :green => "32", :yellow => "33", :blue => "34", :magenta => "35", :purple => "35", :cyan => "36", :white => "37", :_black_ => "40", :_red_ => "41", :_green_ => "42", :_yellow_ => "43", :_blue_ => "44", :_magenta_ => "45", :_purple_ => "45", :_cyan_ => "46", :_white_ => "47", :light_black => "90", :light_red => "91", :light_green => "92", :light_yellow => "93", :light_blue => "94", :light_magenta => "95", :light_purple => "95", :light_cyan => "96", :_light_black_ => "100", :_light_red_ => "101", :_light_green_ => "102", :_light_yellow_ => "103", :_light_blue_ => "104", :_light_magenta_ => "105", :_light_purple_ => "105", :_light_cyan_ => "106", } PARSE_REGEX = /\[(#{ STYLES.keys.join('|') })\]/ix INDENT_REGEX = /\[indent\]/ix def initialize @indent = 1 end def display(string = '') print(parse("[indent]#{string}")) STDOUT.flush nil end def display_line(string = '') display(string) new_line nil end def display_lines(lines = []) for line in [*lines] display_line(line) end nil end def parse(string) if STDOUT.tty? string.gsub(PARSE_REGEX) { "\e[#{STYLES[$1.to_sym]}m" }.gsub(INDENT_REGEX) { indentation } else strip(string) end end def indent(&block) @indent += 1 yield @indent -= 1 end def indentation ' ' * @indent end def redisplay(string = '', width = 120) print("\r#{' ' * width}\r") display("#{string}") nil end def redisplay_line(string = '', width = 120) redisplay(string, width) new_line nil end def new_line print("\n") nil end def strip(string) string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation } end %w{display display_line display_lines display_table display_compact_table indent parse redisplay redisplay_line new_line redisplay_progressbar}.each do |method| eval <<-DEF def self.#{method}(*args, &block) Thread.current[:formatador] ||= new Thread.current[:formatador].#{method}(*args, &block) end DEF end end formatador-0.2.1/formatador.gemspec0000644000175000017500000000627211713747534016354 0ustar bigonbigon## This is the rakegem gemspec template. Make sure you read and understand ## all of the comments. Some sections require modification, and others can ## be deleted if you don't need them. Once you understand the contents of ## this file, feel free to delete any comments that begin with two hash marks. ## You can find comprehensive Gem::Specification documentation, at ## http://docs.rubygems.org/read/chapter/20 Gem::Specification.new do |s| s.specification_version = 2 if s.respond_to? :specification_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.rubygems_version = '1.3.5' ## Leave these as is they will be modified for you by the rake gemspec task. ## If your rubyforge_project name is different, then edit it and comment out ## the sub! line in the Rakefile s.name = 'formatador' s.version = '0.2.1' s.date = '2011-09-01' s.rubyforge_project = 'formatador' ## Make sure your summary is short. The description may be as long ## as you like. s.summary = "Ruby STDOUT text formatting" s.description = "STDOUT text formatting" ## List the primary authors. If there are a bunch of authors, it's probably ## better to set the email to an email list or something. If you don't have ## a custom homepage, consider using your GitHub URL or the like. s.authors = ["geemus (Wesley Beary)"] s.email = 'geemus@gmail.com' s.homepage = 'http://github.com/geemus/NAME' ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb' s.require_paths = %w[lib] ## This sections is only necessary if you have C extensions. # s.require_paths << 'ext' # s.extensions = %w[ext/extconf.rb] ## If your gem includes any executables, list them here. # s.executables = ["name"] # s.default_executable = 'name' ## Specify any RDoc options here. You'll want to add your README and ## LICENSE files to the extra_rdoc_files list. s.rdoc_options = ["--charset=UTF-8"] s.extra_rdoc_files = %w[README.rdoc] ## List your runtime dependencies here. Runtime dependencies are those ## that are needed for an end user to actually USE your code. # s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"]) ## List your development dependencies here. Development dependencies are ## those that are only needed during development s.add_development_dependency('rake') s.add_development_dependency('shindo') ## Leave this section as-is. It will be automatically generated from the ## contents of your Git repository via the gemspec task. DO NOT REMOVE ## THE MANIFEST COMMENTS, they are used as delimiters by the task. # = MANIFEST = s.files = %w[ Gemfile README.rdoc Rakefile formatador.gemspec lib/formatador.rb lib/formatador/progressbar.rb lib/formatador/table.rb tests/basic_tests.rb tests/table_tests.rb tests/tests_helper.rb ] # = MANIFEST = ## Test files will be grabbed from the file list. Make sure the path glob ## matches what you actually use. s.test_files = s.files.select { |path| path =~ /^[spec|tests]\/.*_[spec|tests]\.rb/ } end formatador-0.2.1/Rakefile0000644000175000017500000000670111713747534014313 0ustar bigonbigonrequire 'rubygems' require 'rake' require 'date' ############################################################################# # # Helper functions # ############################################################################# def name @name ||= Dir['*.gemspec'].first.split('.').first end def version line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/] line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1] end def date Date.today.to_s end def rubyforge_project name end def gemspec_file "#{name}.gemspec" end def gem_file "#{name}-#{version}.gem" end def replace_header(head, header_name) head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"} end ############################################################################# # # Standard tasks # ############################################################################# require 'shindo/rake' Shindo::Rake.new task :default => :tests desc "Generate RCov test coverage and open in your browser" task :coverage do require 'rcov' sh "rm -fr coverage" sh "rcov test/test_*.rb" sh "open coverage/index.html" end require 'rake/rdoctask' Rake::RDocTask.new do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = "#{name} #{version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') end desc "Open an irb session preloaded with this library" task :console do sh "irb -rubygems -r ./lib/#{name}.rb" end ############################################################################# # # Custom tasks (add your own tasks here) # ############################################################################# ############################################################################# # # Packaging tasks # ############################################################################# task :release => :build do unless `git branch` =~ /^\* master$/ puts "You must be on the master branch to release!" exit! end sh "sudo gem install pkg/#{name}-#{version}.gem" sh "git commit --allow-empty -a -m 'Release #{version}'" sh "git tag v#{version}" sh "git push origin master" sh "git push origin v#{version}" sh "gem push pkg/#{name}-#{version}.gem" end task :build => :gemspec do sh "mkdir -p pkg" sh "gem build #{gemspec_file}" sh "mv #{gem_file} pkg" end task :gemspec => :validate do # read spec file and split out manifest section spec = File.read(gemspec_file) head, manifest, tail = spec.split(" # = MANIFEST =\n") # replace name version and date replace_header(head, :name) replace_header(head, :version) replace_header(head, :date) #comment this out if your rubyforge_project has a different name replace_header(head, :rubyforge_project) # determine file list from git ls-files files = `git ls-files`. split("\n"). sort. reject { |file| file =~ /^\./ }. reject { |file| file =~ /^(rdoc|pkg)/ }. map { |file| " #{file}" }. join("\n") # piece file back together and write manifest = " s.files = %w[\n#{files}\n ]\n" spec = [head, manifest, tail].join(" # = MANIFEST =\n") File.open(gemspec_file, 'w') { |io| io.write(spec) } puts "Updated #{gemspec_file}" end task :validate do libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"] unless libfiles.empty? puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir." exit! end unless Dir['VERSION*'].empty? puts "A `VERSION` file at root level violates Gem best practices." exit! end end formatador-0.2.1/README.rdoc0000644000175000017500000000534211713747534014454 0ustar bigonbigon= formatador STDOUT text formatting == Quick and dirty You can call class methods to print out single lines like this: Formatador.display_line('Hello World') You use tags, similar to html, to set formatting options: Formatador.display_line('[green]Hello World[/]') [/] resets everything to normal, colors are supported and [_color_] sets the background color. == Standard options * format - and adds color codes if STDOUT.tty? is true * display - calls format on the input and prints it * display_line - calls display, but adds on a newline (\n) * redisplay - Displays text, prepended with \r which will overwrite the last existing line == Extensions * display_table: takes an array of hashes. Each hash is a row, with the keys being the headers and values being the data. An optional second argument can specify which headers/columns to include and in what order they should appear. * display_compact_table: Same as display_table, execpt that split lines are not drawn by default in the body of the table. If you need a split line, put a :split constant in the body array. * redisplay_progressbar: takes the current and total values as its first two arguments and redisplays a progressbar (until current = total and then it display_lines). An optional third argument represents the start time and will add an elapsed time counter. == Indentation By initializing a formatador object you can keep track of indentation: formatador = Formatador.new formatador.display_line('one level of indentation') formatador.indent { formatador.display_line('two levels of indentation') } formatador.display_line('one level of indentation') == Copyright (The MIT License) Copyright (c) 2009 {geemus (Wesley Beary)}[http://github.com/geemus] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. formatador-0.2.1/Gemfile0000644000175000017500000000004611713747534014135 0ustar bigonbigonsource "http://rubygems.org" gemspec