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



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

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

#add_to!(f) ⇒ Object



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

def add_to! f; File.open(f, "a+") do |io|
    io.readlines.last.tap{|l| io.puts if l && l.end_with?($/).!}
    io.write(self)
  end
end

#bfObject



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

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

#bg(sym) ⇒ Object



220
221
222
# File 'lib/pretty_debug.rb', line 220

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



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

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

#binary_write_to!(f) ⇒ Object



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

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

#color(sym) ⇒ Object



217
218
219
# File 'lib/pretty_debug.rb', line 217

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



46
47
48
49
50
# File 'lib/pretty_debug.rb', line 46

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



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

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



35
36
37
38
39
40
# File 'lib/pretty_debug.rb', line 35

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



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

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

#parse_ltsvObject



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

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.



62
63
64
65
66
# File 'lib/pretty_debug.rb', line 62

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



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

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

#unchompObject



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

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

#unchomp!Object



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

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

#unindentObject



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

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

#write_to(f) ⇒ Object



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

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

#write_to!(f) ⇒ Object



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

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