Class: String
- Defined in:
- lib/pretty_debug.rb,
lib/pretty_debug.rb,
lib/pretty_debug.rb
Overview
String
Constant Summary collapse
- @@indent =
" "
Instance Method Summary collapse
- #add_to(f) ⇒ Object
- #add_to!(f) ⇒ Object
- #bf ⇒ Object
- #bg(sym) ⇒ Object
- #binary_write_to(f) ⇒ Object
- #binary_write_to!(f) ⇒ Object
- #color(sym) ⇒ Object
- #common_affix(other) ⇒ Object
- #common_prefix(other) ⇒ Object
- #ellipsis(n) ⇒ Object
- #indent(n = 1) ⇒ Object
- #parse_ltsv ⇒ Object
-
#safely_write_to(dest, tmp = "#{Dir.tmpdir}/safe-save") ⇒ Object
Raises an error if either save, read, or round-trip-matching fails.
- #terminal_escape ⇒ Object
- #unchomp ⇒ Object
- #unchomp! ⇒ Object
- #unindent ⇒ Object
- #write_to(f) ⇒ Object
- #write_to!(f) ⇒ Object
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 |
#bf ⇒ Object
229 |
# File 'lib/pretty_debug.rb', line 229 def bf; "#{vt100("01")}#{self}#{vt100}" end |
#bg(sym) ⇒ Object
226 227 228 |
# File 'lib/pretty_debug.rb', line 226 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
223 224 225 |
# File 'lib/pretty_debug.rb', line 223 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_ltsv ⇒ Object
243 |
# File 'lib/pretty_debug.rb', line 243 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_escape ⇒ Object
222 |
# File 'lib/pretty_debug.rb', line 222 def terminal_escape; "\"#{self}\"" end |
#unchomp ⇒ Object
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 |
#unindent ⇒ Object
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 |