class Betterp::Output
Constants
- COLORS
- EFFECTS
Public Class Methods
new(raw, source, options = {})
click to toggle source
# File lib/betterp/output.rb, line 8 def initialize(raw, source, options = {}) @raw = raw @source = source @color = color @effect = effect @pretty = options.fetch(:pretty, false) end
Public Instance Methods
format(args)
click to toggle source
# File lib/betterp/output.rb, line 16 def format(args) (@pretty ? args : args.map(&:inspect)).map do |arg| style = %i[yellow] header + colorize(prefix) + caller_code + Paint[pretty(arg), *style] end end
Private Instance Methods
caller_code()
click to toggle source
# File lib/betterp/output.rb, line 35 def caller_code return '' unless @raw.include?(':') path, line, *_rest = @raw.split(':') return '' unless Pathname.new(path).readable? && line.to_i.positive? Paint % [ +'%{open}%{code}%{close}', :default, open: ['{ ', :white, :default], code: [find_caller(line.to_i, path).to_s.strip, :cyan], close: [' } ', :white, :default] ] end
color()
click to toggle source
# File lib/betterp/output.rb, line 92 def color COLORS[hash(@raw).hex % COLORS.size] end
colorize(string)
click to toggle source
# File lib/betterp/output.rb, line 76 def colorize(string) Paint % [string, :default, mapping] end
effect()
click to toggle source
# File lib/betterp/output.rb, line 96 def effect EFFECTS[hash(hash(@raw)).hex % EFFECTS.size] end
find_caller(line_number, path)
click to toggle source
# File lib/betterp/output.rb, line 50 def find_caller(line_number, path) lines = File.readlines(path) token = @pretty ? 'pp' : 'p' lines[0...line_number].reverse.find { |line| line.match(/\b#{token}\b/) } end
hash(input)
click to toggle source
# File lib/betterp/output.rb, line 100 def hash(input) Digest::MD5.hexdigest(input) end
header()
click to toggle source
# File lib/betterp/output.rb, line 56 def header Paint % [ +"%{standard}%{relevant}", :default, standard: [' ', :default], relevant: ['•••• ', @color, @effect] ] end
mapping()
click to toggle source
# File lib/betterp/output.rb, line 80 def mapping { path: [@source.path], line_no: [@source.line_no], method_name: [@source.method_name], method_pointer: [' => ', :reset, :bright], separator: [':', :reset], terminator: [' :: ', :reset] } end
prefix()
click to toggle source
# File lib/betterp/output.rb, line 65 def prefix [ '%{path}', '%{separator}', '%{line_no}', '%{method_pointer}', '%{method_name}', '%{terminator}' ].join end
pretty(arg)
click to toggle source
# File lib/betterp/output.rb, line 25 def pretty(arg) return arg unless @pretty io = StringIO.new PP.pp(arg, io) return io.string unless io.string.include?("\n") "\n" + io.string.split("\n").map { |line| " #{line}" }.join("\n") end