Class: String

Inherits:
Object show all
Defined in:
lib/pretty_debug.rb,
lib/pretty_debug.rb,
lib/pretty_debug.rb

Overview

String

Constant Summary collapse

@@indent =
"  "

Instance Method Summary collapse

Instance Method Details

#add_to(f) ⇒ Object



51
# File 'lib/pretty_debug.rb', line 51

def add_to f; add_to!(f) rescue nil end

#add_to!(f) ⇒ Object



52
# File 'lib/pretty_debug.rb', line 52

def add_to! f; File.add(f, self) end

#bfObject



270
# File 'lib/pretty_debug.rb', line 270

def bf; "#{vt100("01".freeze)}#{self}#{vt100}" end

#bg(sym) ⇒ Object



275
276
277
# File 'lib/pretty_debug.rb', line 275

def bg sym; "#{vt100(40+
{black: 0, red: 1, green: 2, yellow: 3, blue: 4, magenta: 5, cyan: 6, white: 7}[sym.to_sym]
)}#{self}#{vt100}" end

#binary_write_to(f) ⇒ Object



49
# File 'lib/pretty_debug.rb', line 49

def binary_write_to f; binary_write_to!(f) rescue nil end

#binary_write_to!(f) ⇒ Object



50
# File 'lib/pretty_debug.rb', line 50

def binary_write_to! f; File.write(f, self, "wb".freeze) end

#color(sym) ⇒ Object



272
273
274
# File 'lib/pretty_debug.rb', line 272

def color sym; "#{vt100(30+
{black: 0, red: 1, green: 2, yellow: 3, blue: 4, magenta: 5, cyan: 6, white: 7}[sym.to_sym]
)}#{self}#{vt100}" end

#ellipsis(n) ⇒ Object



41
42
43
44
45
46
# File 'lib/pretty_debug.rb', line 41

def ellipsis n
	if length <= n then self
	elsif n.odd? then "#{slice(0..n/2-3)}...#{slice(-n/2+2..-1)}"
	else "#{slice(0..n/2-3)}...#{slice(-n/2+1..-1)}"
	end
end

#indent(n = 1) ⇒ Object



37
# File 'lib/pretty_debug.rb', line 37

def indent n = 1; gsub(/^/, @@indent * n) end

#parse_ltsvObject



291
# File 'lib/pretty_debug.rb', line 291

def parse_ltsv; Hash[chomp.split("\t".freeze).map{|f| f.split(":".freeze, 2)}] end

#safely_write_to(dest, tmp = "#{Dir.tmpdir}/safe-save") ⇒ Object

Raises an error if either save, read, or round-trip-matching fails.



54
55
56
57
58
# File 'lib/pretty_debug.rb', line 54

def safely_write_to dest, tmp = "#{Dir.tmpdir}/safe-save"
	write_to!(tmp)
	raise "Round trip `save-read` failed on `#{dest}`." unless File.read(tmp) == self
	File.rename(tmp, dest)
end

#terminal_escapeObject



269
# File 'lib/pretty_debug.rb', line 269

def terminal_escape; "\"#{self}\"" end

#unchompObject



39
# File 'lib/pretty_debug.rb', line 39

def unchomp; sub(/#$/?\z/, $/) end

#unchomp!Object



40
# File 'lib/pretty_debug.rb', line 40

def unchomp!; sub!(/#$/?\z/, $/) end

#underlineObject



271
# File 'lib/pretty_debug.rb', line 271

def underline; "#{vt100("04".freeze)}#{self}#{vt100}" end

#unindentObject



38
# File 'lib/pretty_debug.rb', line 38

def unindent; gsub(/^#{match(/^\s+/)}/, "".freeze).chomp end

#verbatimObject



268
# File 'lib/pretty_debug.rb', line 268

def verbatim; bg(:magenta) end

#write_to(f) ⇒ Object



47
# File 'lib/pretty_debug.rb', line 47

def write_to f; write_to!(f) rescue nil end

#write_to!(f) ⇒ Object



48
# File 'lib/pretty_debug.rb', line 48

def write_to! f; File.write(f, self) end