From eb532d6ebd432590f40defbc1ed624d65c4a850a Mon Sep 17 00:00:00 2001 From: murphy Date: Fri, 26 Mar 2010 04:54:19 +0000 Subject: Long overdue updates for the coderay executable - yay! * *NEW* automatic TTY detection, uses @Terminal@ encoder. * *NEW* optional 3rd parameter for the filename. * *FIXED*: Warn about generated files. * *FIXED*: Ensure linebreak after the output (was problematic for LoC counter). --- Changes.textile | 7 +++++++ bin/coderay | 25 +++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Changes.textile b/Changes.textile index bceda5d..647b498 100644 --- a/Changes.textile +++ b/Changes.textile @@ -8,6 +8,13 @@ h2. Changes in 1.0 * *FIXED* some image links in the documentation * *IMPROVED* Ruby 1.9 support (_._ not in @$LOAD_PATH@) +h3. @coderay@ executable + +* *NEW* automatic TTY detection, uses @Terminal@ encoder. +* *NEW* optional 3rd parameter for the filename. +* *FIXED*: Warn about generated files. +* *FIXED*: Ensure linebreak after the output (was problematic for LoC counter). + h3. @Encoders::Terminal@ * *RENAMED* from @Encoders::Term@, added @:term@ alias. diff --git a/bin/coderay b/bin/coderay index 82d4eca..c938ecf 100644 --- a/bin/coderay +++ b/bin/coderay @@ -8,6 +8,14 @@ def err msg $stderr.puts msg end +def read + if file = ARGV[2] + File.read file + else + $stdin.read + end +end + begin require 'coderay' @@ -28,11 +36,11 @@ Example: if first if first[/-(\w+)/] == first lang = $1 - input = $stdin.read + input = read tokens = :scan elsif first == '-' lang = $1 - input = $stdin.read + input = read tokens = :scan else file = first @@ -48,11 +56,15 @@ Example: if second[/-(\w+)/] == second format = $1 else - raise 'Invalid format (must be -xxx).' + raise 'invalid format (must be -xxx)' end else - $stderr.puts 'No format given; setting to default (HTML Page)' - format = :page + if $stdout.tty? + format = :term + else + $stderr.puts 'No format given; setting to default (HTML Page).' + format = :page + end end # TODO: allow streaming @@ -69,9 +81,10 @@ Example: exit else out = File.open output_filename, 'w' + puts "Writing to #{output_filename}..." end end - out.print output + out.puts output rescue => boom err "Error: #{boom.message}\n" -- cgit v1.2.1