32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
113
114
115
116
117
118
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
|
# File 'lib/console_hash_array.rb', line 32
def self.console( arr, rindex, cindex, key, mode, options={} )
def self.options_insert( p, arr, rindex, cindex, key, mode, options )
if !options .keys.include?( :print__left )
options[:print__left] = p[:print][:left]
end
errors = []
options.keys.each do | key |
allow = [
:right__boxes_total,
:style__spaces,
:style__steps
]
denied = [
:print__left
]
if allow.include?( key )
kk = key.to_s.split( '__' ).map { | a | a.to_sym }
p[ kk[ 0 ] ][ kk[ 1 ] ] = options[ key ]
else
if !denied.include?( key )
error = " \":#{key.to_s}\" - Key not found."
errors.push( error )
end
end
end
return [ p, errors ]
end
def self. options_print_left( p, arr, rindex, cindex, key, mode, options )
errors = []
if options.keys.include?( :print__left )
vars = options[:print__left]
.scan( /\{{[a-z,_,:]+\}}/ )
.map { | a | a.gsub( /[{:}]/, '' ) }
str = options[:print__left]
vars.each do | var |
var = "{{#{var}}}"
if !var.index( '__' ).nil?
keys = var.split( '__' )
.map { | a | a.gsub( /[{}]/, '' ) }
.map { | a | a.to_sym }
str = str.gsub( var, p[ keys[ 0 ] ][ keys[ 1 ] ] )
else
case var
when '{{rindex}}'
m = ( arr.keys.length-1 ).to_s.length
mm = m - rindex.to_s.length
p[:left][:spaces] << mm.times.map { | a | p[:style][:spaces] }.join( '' )
str = str.gsub( var, rindex.to_s )
when '{{cindex}}'
str = str.gsub( var, cindex.to_s )
when '{{key}}'
str = str.gsub( var, key.to_s )
when '{{mode}}'
str = str.gsub( var, mode.to_s )
else
error = " \"#{var}\" - Variable not found."
errors.push( error )
end
end
end
end
p[:print][:left] = str
return [ p, errors ]
end
p = {}
case mode
when :left
p = Marshal.load( Marshal.dump( @TEMPLATE ) )
errors = []
messages = []
p, errors = self.options_insert( p, arr, rindex, cindex, key, mode, options )
messages.concat( errors )
p[:left][:max] = arr.keys.map { | aa | aa.length }.max
p[:left][:spaces] = ( p[:left][:max] - key.length ).times.map { | a | p[:style][:spaces] }.join( '' )
p[:right][:box_current] = 0
p[:right][:mark] = 0
p[:right][:length_max] = arr[ key ].length
p[:right][:box] = p[:right][:length_max] / p[:right][:boxes_total]
p[:right][:boxes] = ( 1..p[:right][:boxes_total] ).map { | a | p[:right][:box] * a }
p, errors = self.options_print_left( p, arr, rindex, cindex, key, mode, options )
messages.concat( errors )
if messages.length != 0 and rindex == 0
puts 'Following errors occured:'
messages.each do | message |
puts message
end
puts
end
print( p[:print][:left] )
if arr[ key ].length == 0
puts
end
when :right
p = @p
if !p[:right][:boxes].find_index( cindex ).nil? and p[:right][:boxes].find_index( cindex ) != p[:right][:box_current]
box_new = p[:right][:boxes].find_index( cindex )
( box_new - p[:right][:box_current] ).times.each do | a |
if p[:right][:mark] < p[:right][:boxes_total]
print( p[:style][:steps] )
end
p[:right][:mark] = p[:right][:mark] + 1
end
p[:right][:box_current] = box_new
end
if cindex == arr[ key ].length-1
if p[:right][:mark] != arr[ key ].length-1
c = p[:right][:boxes_total] - p[:right][:mark]
c > ( arr[ key ].length-1 ) ? c = arr[ key ].length-1 : ''
c.times.each do | a |
print( p[:style][:steps] )
end
end
puts
end
else
end
@p = p
return p
end
|