Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:1565178
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
module RCat
class LinePrinter
def print_line(line)
print line
end
end
 
class PrinterDecorator
def initialize(printer)
@printer = printer
end
 
def print_line(line)
@printer.print_line(line)
end
 
def blank_line?(line)
line.chomp.empty?
end
end
class ExtraNewlineSqueezer < PrinterDecorator
def print_line(line)
super unless @prev_line_blank && blank_line?(line)
@prev_line_blank = blank_line?(line)
end
end
 
class LineNumberer < PrinterDecorator
def initialize(printer, options = {})
super(printer)
@mode = options[:mode]
@line_number = 1
end
 
def only_significant?
@mode == :significant_lines
end
 
def print_line(line)
formatted_line = line
unless blank_line?(line) && only_significant?
formatted_line = "#{@line_number.to_s.rjust(6)}\t#{formatted_line}"
@line_number += 1
end
 
super(formatted_line)
end
end
class Display
def initialize(params)
@line_numbering_style = params[:line_numbering_style]
@squeeze_extra_newlines = params[:squeeze_extra_newlines]
end
 
def create_printer
printer = LinePrinter.new
printer = LineNumberer.new(printer, :mode => @line_numbering_style) if @line_numbering_style
printer = ExtraNewlineSqueezer.new(printer) if @squeeze_extra_newlines
printer
end
 
def render(data)
printer = create_printer
data.lines.each do |line|
printer.print_line(line)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.