Class: PPP
Overview
patches pretty print
Beautify: reworking the pretty print results collapse
-
.beautify(string) ⇒ String
reworking the pretty print results.
-
.beautify_array_1dim(string) ⇒ String
Beautify for one-dimensional arrays or any object that prints like an array.
-
.beautify_array_2dim(string) ⇒ String
Beautify for two-dimensional arrays - very dirty.
-
.beautify_focus_level1(string) ⇒ String
Level 2+ : only one line left Level 3+ : clip complex objects.
-
.beautify_hash(string) ⇒ String
Beautify for Hashes.
-
.beautify_multi(string) ⇒ String
Beautify for complex objects.
-
.beautify_object(string) ⇒ String
Beautify for Objects.
-
.classify_structure(object) ⇒ Symbol
Classifies the structure of an object for output.
Pretty Print Patches collapse
- .pp(obj, out = $>, width = 79) ⇒ Object
- #comma_newline ⇒ Object
-
#pp_hash(obj) ⇒ Object
hashes with more than three keys are displayed line by line.
-
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
ensures that lists with more than three elements are displayed line by line.
Class Method Details
.beautify(string) ⇒ String
reworking the pretty print results
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/perception/ppp.rb', line 27 def self.beautify(string) begin result = string# .gsub!("`","'") result = beautify_focus_level1(result) case classify_structure(string) when :object then result = beautify_object(result) when :hash then result = beautify_hash(result) when :array_1dim then result = beautify_array_1dim(result) when :array_2dim then result = beautify_array_2dim(result) end return result # rescue # return string end end |
.beautify_array_1dim(string) ⇒ String
Beautify for one-dimensional arrays or any object that prints like an array. In front of the brackets there are identifier allowed, eg WP [1, 2, 3]
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/perception/ppp.rb', line 188 def self.beautify_array_1dim(string) return string # tabstops = result[1..-2].analyze_columns( :level_start => 0, # :level_end => 0, # :search => ',', # :stretch => 0 ) # tabstops = [25] if tabstops.empty? # maxtab = tabstops.sort[-1] # result = result.spread( :level_start => 1, # :level_end => 1, # :tabstops => (1..10).collect {|i| i * (maxtab+1)}, # :search => ',', # :position_add => 1 ) end |
.beautify_array_2dim(string) ⇒ String
Beautify for two-dimensional arrays -
very dirty.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/perception/ppp.rb', line 210 def self.beautify_array_2dim(string) result = string # Komma retten result = result.mask( :level_start => 3, :pattern => /['"({<\[]/ ) { |s| s.tr(',', Perception::PPP_KOMMA)} # Tiefere Level retten result = result.mask( :level_start => 2, :with_brackets => true, :pattern => /\[/ ) { |s| s.tr(Perception::PPP_TR_ARRAY_P, Perception::PPP_TR_ARRAY_Q)} # zeilenweise replaces = [[/,[^\n]/, ", \n "]] result = result.mask( :level_start => 1, :level_end => 1, :pattern => /\[/ ) { |s| s.mgsub(replaces)} # Tabstops tabstops = result[1..-2].analyze_columns( :level_start => 1, :level_end => 1, :search => ',', :stretch => 1 ) || [25] tabstops2 = [] tabstops.each do |t| tabstops2 << t + (tabstops2[-1]||0) end # ausrichten result = result.spread( :level_start => 2, :level_end => 2, :tabstops => tabstops2, :search => /,/ ) # Tiefere Level zurück result = result.mask( :level_start => 2, :with_brackets => true, :pattern => /\[/ ) { |s| s.tr(Perception::PPP_TR_ARRAY_Q, Perception::PPP_TR_ARRAY_R)} #Komma zurück result = result.mask( :level_start => 3, :pattern => /['"({<\[]/ ) { |s| s.gsub(Perception::PPP_KOMMA, ',')} return result end |
.beautify_focus_level1(string) ⇒ String
Level 2+ : only one line left Level 3+ : clip complex objects
73 74 75 76 77 78 79 80 |
# File 'lib/perception/ppp.rb', line 73 def self.beautify_focus_level1(string) #return string replaces = [[/\n/, ''], [/ {2,}/,' ']] result = string result = string.mask( :level_start => 2 ) { |s| s.mgsub(replaces)} result = result.mask( :level_start => 3 ) { |s| (s =~ /@.*=.*/ ? '' : s) } return result end |
.beautify_hash(string) ⇒ String
Beautify for Hashes
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/perception/ppp.rb', line 119 def self.beautify_hash(string) result = string result.gsub!(/=> *\n */m, '=>' ) # unnötigen Zeilenumbruch vermeiden # mehrzeilige Darstellung if result.include?("\n") result = string.mask( :level_start => 1, :level_end => 1, :pattern => /\{/ ) { |s| s.gsub(/=>/, ' =>')} # Pfeil retten result = result.mask( :level_start => 2, :pattern => /\{/ ) { |s| s.gsub('=>', Perception::PPP_PFEIL)} # Unterobjekte formatieren replaces = [ [/, /,', '] ] result = result.mask( :level_start => 2, :level_end => 2 ) { |s| s.mgsub(replaces)} # Tiefere Level retten result = result.mask( :level_start => 1, :with_brackets => true, :pattern => /\{/ ) { |s| s.tr(Perception::PPP_TR_ARRAY_P[0..5], Perception::PPP_TR_ARRAY_Q)} tabstops = result.analyze_columns( :level_start => 0, :level_end => 0, :search => '=' ) || [25] tabstops2 = [] tabstops2 << tabstops[0] + 1 tabstops2 << tabstops[0] + 5 # ausrichten result = result.spread( :level_start => 1, :level_end => 1, :search => /=>/, :position_add => 0, # alle '=>' übereinander :tabstops => tabstops2 ) result.gsub!('=>','=> ') # Tiefere Level zurück result = result.mask( :level_start => 1, :with_brackets => true, :pattern => /\{/ ) { |s| s.tr(Perception::PPP_TR_ARRAY_Q, Perception::PPP_TR_ARRAY_R)} # Pfeil zurück result = result.mask( :level_start => 2, :pattern => /\{/ ) { |s| s.gsub(Perception::PPP_PFEIL, '=>')} # einzeilige Darstellung else result = string.mask( :level_start => 1, :level_end => 1, :pattern => /\{/ ) { |s| s.gsub(/, /, ', ')} end result end |
.beautify_multi(string) ⇒ String
Beautify for complex objects.
Mehrere Ausgaben in einer Zeile. Die einzelnen Ausgaben liegen als Array vor. Das Ergebnis soll aber nicht wie ein Array ausssehen.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/perception/ppp.rb', line 272 def self.beautify_multi(string) result = string result = beautify_focus_level1(result) result = result.mask( :level_start => 1, :level_end => 1, :pattern => /\[/ ) {|s| s.tr(',', Perception::PPP_KOMMA)} result = result.spread_line( [25], Perception::PPP_KOMMA, 0 ) # Äußeres Array entfernen result = result.tr(Perception::PPP_TR_ALL,'') result = result.mask( :with_brackets => true, :level_start => 0, :level_end => 0, :pattern => /\[/ ) {|s| s.tr('[]','')} return result end |
.beautify_object(string) ⇒ String
Beautify for Objects
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/perception/ppp.rb', line 87 def self.beautify_object(string) result = string result.gsub!(/= *\n */m, '=' ) # unnötigen Zeilenumbruch vermeiden result = result.mask( :level_start => 1, :level_end => 1, :pattern => /</ ) { |s| s.gsub(/=/, ' = ')} replaces = [ [/ @/, ' @'], [/= #/,'=#'] ] # Unterobjekte formatieren result = result.mask( :level_start => 2, :level_end => 2 ) { |s| s.mgsub(replaces)} result = result.mask( :level_start => 4 ) { |s| ''} tabstops = result.analyze_columns( :level_start => 0, :level_end => 0, :search => '=' ) || [25] tabstops2 = [] tabstops2 << tabstops[0] tabstops2 << tabstops[0] + 5 result = result.spread( :level_start => 1, :level_end => 1, :search => /=[^>=]/, :position_add => 1, # alle '=' links :tabstops => tabstops2 ) result end |
.classify_structure(object) ⇒ Symbol
Classifies the structure of an object for output
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/perception/ppp.rb', line 47 def self.classify_structure(object) # Hash return :hash if object =~ /^\{.*=>.*\}/m # Object return :object if object =~ /^#<.*>$/m # Eindimensionales Array # oder Objekt, das sich wie ein Array ausdruckt -- vor der eckigen Klammer ist ein Identifier erlaubt, z.B. WP[ 1, 2, 3] return :array_1dim if object =~ /^[A-Z_0-9a-z]{0,30}\[[^\[].*[^\]]\]$/m # Zweidimensionales Array # Anfang prüfen: Zwei eckige Klammern auf, danach keine eckige Klammer auf # oder alternativ ein Text zwischen den beiden Klammern auf return :array_2dim if object =~ /^\[[A-Z_0-9a-z]{0,30}\[[^\[]/m #return self unless object =~ /[^\]]\]\]$/ # Ende prüfen: Zwei eckige Klammern zu, davor keine eckige Klammer zu end |
Instance Method Details
#comma_newline ⇒ Object
324 325 326 327 |
# File 'lib/perception/ppp.rb', line 324 def comma_newline text "," breakable('',9999) end |
#pp_hash(obj) ⇒ Object
hashes with more than three keys are displayed line by line
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/perception/ppp.rb', line 332 def pp_hash(obj) group(1, '{', '}') { seplist(obj, 3, :each_pair) {|k, v| group { pp k text '=>' group(1) { breakable '' pp v } } } } end |
#seplist(list, sep = nil, iter_method = :each) ⇒ Object
ensures that lists with more than three elements are displayed line by line
309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/perception/ppp.rb', line 309 def seplist(list, sep=nil, iter_method=:each) # :yield: element sep = lambda { comma_newline } if (sep.kind_of?(Integer) && list.size > sep ) sep = lambda { comma_breakable } if (sep.nil? || sep.kind_of?(Integer)) first = true list.__send__(iter_method) {|*v| if first first = false else sep.call end yield(*v) } end |