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



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

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

#add_to!(f) ⇒ Object



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

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

#bfObject



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

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

#bg(sym) ⇒ Object



242
243
244
# File 'lib/pretty_debug.rb', line 242

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

#binary_write_to(f) ⇒ Object



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

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

#binary_write_to!(f) ⇒ Object



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

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

#color(sym) ⇒ Object



239
240
241
# File 'lib/pretty_debug.rb', line 239

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

#common_affix(other) ⇒ Object



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

def common_affix other
  i = - 1
  loop{break unless self[i] and self[i] == other[i]; i -= 1}
  self[i + 1, -(i + 1)]
end

#common_prefix(other) ⇒ Object



48
49
50
51
52
# File 'lib/pretty_debug.rb', line 48

def common_prefix other
  i = 0
  loop{break unless self[i] and self[i] == other[i]; i += 1}
  self[0, i]
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



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

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

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

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



65
66
67
68
69
# File 'lib/pretty_debug.rb', line 65

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



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

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

#unindentObject



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

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

#write_to(f) ⇒ Object



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

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

#write_to!(f) ⇒ Object



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

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