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



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

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

#add_to!(f) ⇒ Object



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

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

#bfObject



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

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

#bg(sym) ⇒ Object



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

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



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

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

#binary_write_to!(f) ⇒ Object



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

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

#color(sym) ⇒ Object



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

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



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

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



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

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

#parse_ltsvObject



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

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.



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

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



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

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

#unchompObject



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

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

#unchomp!Object



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

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

#underlineObject



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

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

#unindentObject



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

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

#verbatimObject



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

def verbatim; bg(:magenta) end

#write_to(f) ⇒ Object



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

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

#write_to!(f) ⇒ Object



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

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