Sha256: 112c5553af1b4b0cbdd4890b91b91db14ac580759dea625184e2806b2d5a9fa0

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

#!/usr/bin/env ruby
require 'optparse'
require 'ruby-beautify'

include RubyBeautify

Options = OptionParser.new do |opts|
	opts.on("-V", "--version", "Print version") { |version| puts RBeautify::VERSION;exit 0}
	opts.on("-c", "--indent_count [COUNT]", Integer, "Count of characters to use for indenting. (default: 1)") { |count| @indent_count = count}
	opts.on("-t", "--tabs", "Use tabs for the indent character (default)") { @indent_token = "\t" }
	opts.on("-s", "--spaces", "Use spaces for the indent character") { @indent_token = " " }
	opts.on("--overwrite", "Overwrite files as you go (won't touch files that faile a syntax check).") { @overwrite = true }
	opts.banner = "Usage: print ruby into a pretty format, or break trying."
end
Options.parse!

@indent_token = "\t" unless @indent_token
@indent_count = 1 unless @indent_count

def print_or_die(content)
	if content
		if syntax_ok? content
			puts pretty_string content, indent_token: @indent_token, indent_count: @indent_count
		else
			puts content
			exit 127
		end
	end
end

# no argument, assume we want to open STDIN.
if ARGV.empty?
	content = $stdin.read
	print_or_die content
else
	ARGV.each do |file|
		if File.exist? file
			fh = open(file)
			content = fh.read
			fh.sync
			fh.close
		else
			puts "No such file: #{file}"
			exit
		end

		if @overwrite
			if syntax_ok? content
				fh = open(file, 'w')
				fh.write pretty_string content
				fh.sync
				fh.close
			else
				next
			end
		else
			print_or_die content
		end
	end
end

Version data entries

2 entries across 1 versions & 1 rubygems

Version Path
ruby-beautify-0.97.0 bin/rbeautify
ruby-beautify-0.97.0 bin/ruby-beautify