aniview 1.1.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,286 +1,305 @@
1
- require_relative 'emote'
1
+ require_relative 'color'
2
2
  require_relative '../util/alogger'
3
3
  require_relative '../util/format'
4
4
 
5
5
  class Menu
6
6
 
7
- def initialize(
8
- refresh_function:,
9
- interface:,
10
- name:,
11
- pref:,
12
- format:,
13
- term:,
14
- children: true
15
- )
16
- @term = term
17
- @format = format
18
- @attributes = {
19
- "t" => name,
20
- }
21
- @pref = pref
22
- @refresh_func = refresh_function
23
- @interface = interface
24
- @expanded = -1
25
- @selected = 0
26
- @scrollup = false
27
- @children = children
28
- @items_formatted = {}
29
- @items_hash = {}
30
- @hashmap = {}
31
- @lu = 0
32
-
33
- refresh
34
- @lastcount = getLen
35
-
36
- end
37
-
38
- def setRfunc(name)
39
- @refresh_func = name
40
- @items_formatted = {}
41
- @items_hash = {}
42
- @lu = 0
43
- refresh
44
- refreshFormats
45
- end
46
-
47
- def change_screen_size
48
- #@term.rows = lines
49
- #@term.cols = cols
50
- @items_formatted = {}
51
- @items_hash = {}
52
- @lu = 0
53
- refresh
54
- draw
55
- end
56
-
57
- def setName(name)
58
- @attributes["t"] = name
59
- end
60
-
61
- def setFormat(format_)
62
- @format = format_
63
- end
64
-
65
- def refresh
66
- @items = @interface.makeHash @interface.send(@refresh_func)
67
- @expanded = -1 if @items.values[@expanded] == nil
68
- fixCursor
69
- end
70
-
71
- def expand(i)
72
- return if i > @items.length
73
- if @expanded == i
74
- @selected = @expanded
75
- @expanded = -1
76
- else
77
- @selected = i
78
- @expanded = i
79
- end
80
- end
81
-
82
- def resolveSelected(resolve_me=@selected)
83
- in_expanded=false
84
- inside = 0
85
- outside = resolve_me
86
- if @expanded > -1
87
- if resolve_me > @expanded and resolve_me <= (@items.values[@expanded].length + @expanded)
88
- outside = @expanded
89
- inside = resolve_me - @expanded - 1
90
- in_expanded=true
91
- elsif resolve_me > @expanded
92
- outside = resolve_me - @items.values[@expanded].length
93
- end
94
- end
95
-
96
- return Hash["out" => outside, "in" => inside, "in_expanded" => in_expanded]
97
- end
98
-
99
- def moveCursor(dir)
100
- @scrollup = false if dir == "down"
101
- @scrollup = true if dir == "up"
102
- @selected -= 1 if dir == "up"
103
- @selected += 1 if dir == "down"
104
- fixCursor
105
- end
106
-
107
- def fixCursor
108
- #wrap selector
109
- #@selected = getLen - 1 if @selected < 0
110
- #@selected = 0 if @selected >= getLen
111
- #
112
- #nowrap
113
- @selected = 0 if @selected < 0
114
- @selected = getLen - 1 if @selected >= getLen
115
- end
116
-
117
- def getLen
118
- len = @items.length
119
- len += @items.values[@expanded].length if @expanded >= 0
120
- return len
121
- end
122
-
123
- def color(index)
124
- if index == @selected
125
- return $clr[@pref.get("clr")["selected"]]
126
- else
127
- return $clr[@pref.get("clr")["secondary"]]
128
- end
129
- end
130
-
131
- def refreshFormats
132
- now = Integer(DateTime.now.strftime('%Q'))
133
- return if now - @lu < 100
134
- @lu = now
135
-
136
- @cl = {
137
- "s" => $clr[@pref.get("clr")["selected"]],
138
- "1" => $clr[@pref.get("clr")["primary"]],
139
- "2" => $clr[@pref.get("clr")["secondary"]],
140
- "m" => $clr[@pref.get("clr")["main"]],
141
- }
142
-
143
- #@items_formatted = {}
144
- new_items_formatted = {}
145
- new_items_hash = {}
146
- new_hashmap = {}
147
- @items.each_with_index { |item, i|
148
-
149
- #@l.log @items.keys[i]
150
- #
151
- babble = item[0].cereal #Digest::SHA256.bubblebabble String(item[0])
152
-
153
- if @items_hash.key? babble
154
- val = @items_hash[babble]
155
- new_items_hash.merge!(babble => val)
156
- new_items_formatted.merge!(@hashmap[babble] => @items_formatted[@hashmap[babble]])
157
- new_hashmap.merge!(babble => @hashmap[babble])
158
- next
159
- end
160
-
161
- parent = Format.parse_format(
162
- @pref.get(@format)["parent"],
163
- item[0].attributes,
164
- @term.cols
165
- )
166
-
167
- children = []
168
- children_hash = []
169
-
170
- if @children
171
- item[1].each { |subitem|
172
-
173
- child_babble = subitem.cereal
174
-
175
- if @items_hash.key? child_babble
176
- val = @items_hash[babble]
177
- new_items_hash.merge!(child_babble => val)
178
- children<<@hashmap[babble]
179
- new_hashmap.merge!(babble => @hashmap[babble])
180
- next
181
- end
182
-
183
- children << Format.parse_format(
184
- @pref.get(@format)["child"],
185
- subitem.attributes,
186
- @term.cols
187
- )
188
- children_hash << subitem.hash
189
-
190
- }
191
- end
192
-
193
- new_items_hash.merge!(babble => children_hash)
194
- new_items_formatted.merge!(parent => children)
195
- new_hashmap.merge!(babble => parent)
196
- }
197
-
198
- @items_formatted = new_items_formatted
199
- @items_hash = new_items_hash
200
- @hashmap = new_hashmap
201
-
202
- @title_formatted = Format.parse_format(
203
- @pref.get(@format)["title"],
204
- @attributes,
205
- @term.cols
206
- )
207
-
208
- end
209
-
210
- def draw
211
- #the number of lines to preview
212
- buffer = 3
213
- refreshFormats
214
-
215
- #@term.rows-2 is used because 2 lines are reserved for
216
- #the command line (the bottom line)
217
- #and the title line (the top line)
218
-
219
- if getLen > @term.rows - 2
220
- finish = @selected + buffer #if not @scrollup
221
- finish = @term.rows - 2 if finish < @term.rows - 2
222
- finish = getLen if finish > getLen
223
- start = finish - ( @term.rows-2 )
224
- else
225
- start = 0
226
- finish = @term.rows - 2
227
- end
228
-
229
- drawFrom(start, finish)
230
- end
231
-
232
- def drawFrom(line_on, lines_to_draw)
233
- #print menu header
234
- lo="\e[1;1H"
235
- print lo + @cl["m"] + @title_formatted + "\e[K"
236
-
237
- resolved_start_position = resolveSelected line_on
238
-
239
- outer_index = resolved_start_position["out"]
240
- inner_index = resolved_start_position["in"]
241
-
242
- if resolved_start_position["in_expanded"]
243
- outer_index+=1
244
- end
245
-
246
- offset=line_on-1
247
-
248
- while line_on < lines_to_draw
249
-
250
- in_outer_region = ! (line_on >= getLen)
251
- in_expanded_region = resolveSelected(line_on)["in_expanded"]
252
- line = line_on-offset+1
253
- selected = line_on == @selected
254
-
255
- cl = ""
256
- cl = @cl["s"] if selected
257
- lo = "\e[#{line};1H" + cl
258
-
259
- if in_expanded_region and @children
260
- print @cl["2"] + lo + String(@items_formatted.values[@expanded][inner_index]) + "\e[K"
261
- inner_index+=1
262
- elsif in_outer_region
263
- print @cl["1"] + lo + String(@items_formatted.keys[outer_index]) + "\e[K"
264
- outer_index+=1
265
- else
266
- print "\e[#{line};1H\e[2K\n"
267
- end
268
-
269
- line_on+=1
270
- end
271
- end
272
-
273
- def control(key)
274
- sel = resolveSelected
275
- if key == @pref.get("keybindings")["menu_nav_up"]
276
- moveCursor("up")
277
- elsif key == @pref.get("keybindings")["menu_nav_down"]
278
- moveCursor("down")
279
- else
280
- customControl(key, sel)
281
- end
282
- end
283
-
284
- def customControl(key, sel); end
285
-
286
- end
7
+ def initialize(refresh_function:,interface:,name:,pref:,format:,term:,children: true)
8
+ @term = term
9
+ @format = format
10
+ @attributes = {
11
+ "t" => name,
12
+ }
13
+ @pref = pref
14
+ @refresh_func = refresh_function
15
+ @interface = interface
16
+ @expanded = -1
17
+ @selected = 0
18
+ @children = children
19
+ @items_formatted = {}
20
+ @items_hash = {}
21
+ @hashmap = {}
22
+ @lu = 0
23
+
24
+ @oldRows = @term.rows
25
+ @logger = ALogger.new("/dev/null", true)
26
+
27
+ refresh
28
+ @lastcount = getLen
29
+
30
+ @view = [0, @term.rows - 3]
31
+
32
+ end
33
+
34
+ def setRfunc(name)
35
+ @refresh_func = name
36
+ @items_formatted = {}
37
+ @items_hash = {}
38
+ @lu = 0
39
+ refresh
40
+ refreshFormats
41
+ end
42
+
43
+ def change_screen_size
44
+ #@term.rows = lines
45
+ #@term.cols = cols
46
+ @items_formatted = {}
47
+ @items_hash = {}
48
+ @lu = 0
49
+ refresh
50
+
51
+ #if @term.rows > @oldRows
52
+
53
+ diff = @term.rows - @oldRows
54
+ #@view[0] += diff
55
+ @view[1] += diff
56
+
57
+ @oldRows = @term.rows
58
+ adjustView
59
+ draw
60
+ end
61
+
62
+ def setName(name)
63
+ @attributes["t"] = name
64
+ end
65
+
66
+ def setFormat(format_)
67
+ @format = format_
68
+ end
69
+
70
+ def refresh
71
+ @items = @interface.makeHash @interface.send(@refresh_func)
72
+ @expanded = -1 if @items.values[@expanded] == nil
73
+ fixCursor
74
+ end
75
+
76
+ def expand(i)
77
+ return if i > @items.length
78
+ if @expanded == i
79
+ @selected = @expanded
80
+ @expanded = -1
81
+ else
82
+ @selected = i
83
+ @expanded = i
84
+ end
85
+ fixCursor
86
+ adjustView
87
+ end
88
+
89
+ def resolveSelected(resolve_me=@selected)
90
+ in_expanded=false
91
+ inside = 0
92
+ outside = resolve_me
93
+ if @expanded > -1
94
+ if resolve_me > @expanded and resolve_me <= (@items.values[@expanded].length + @expanded)
95
+ outside = @expanded
96
+ inside = resolve_me - @expanded - 1
97
+ in_expanded=true
98
+ elsif resolve_me > @expanded
99
+ outside = resolve_me - @items.values[@expanded].length
100
+ end
101
+ end
102
+
103
+ return Hash["out" => outside, "in" => inside, "in_expanded" => in_expanded]
104
+ end
105
+
106
+ def moveCursor(dir)
107
+ @selected -= 1 if dir == "up"
108
+ @selected += 1 if dir == "down"
109
+ adjustView
110
+ fixCursor
111
+ end
112
+
113
+ def fixCursor
114
+ #wrap selector
115
+ #@selected = getLen - 1 if @selected < 0
116
+ #@selected = 0 if @selected >= getLen
117
+ #
118
+ #nowrap
119
+ @selected = 0 if @selected < 0
120
+ @selected = getLen - 1 if @selected >= getLen
121
+ end
122
+
123
+ def getLen
124
+ len = @items.length
125
+ len += @items.values[@expanded].length if @expanded >= 0
126
+ return len
127
+ end
128
+
129
+ def color(index)
130
+ if index == @selected
131
+ return Color.public_send(@pref.get("clr")["selected"])
132
+ else
133
+ return Color.public_send(@pref.get("clr")["secondary"])
134
+ end
135
+ end
136
+
137
+ def refreshFormats
138
+ now = Integer(DateTime.now.strftime('%Q'))
139
+ return if now - @lu < 100
140
+ @lu = now
141
+
142
+ @cl = {
143
+ "s" => Color.public_send(@pref.get("clr")["selected"]),
144
+ "1" => Color.public_send(@pref.get("clr")["primary"]),
145
+ "2" => Color.public_send(@pref.get("clr")["secondary"]),
146
+ "m" => Color.public_send(@pref.get("clr")["main"]),
147
+ }
148
+
149
+ #@items_formatted = {}
150
+ new_items_formatted = {}
151
+ new_items_hash = {}
152
+ new_hashmap = {}
153
+ @items.each_with_index { |item, i|
154
+
155
+ #@l.log @items.keys[i]
156
+ #
157
+ babble = item[0].cereal #Digest::SHA256.bubblebabble String(item[0])
158
+
159
+ if @items_hash.key? babble
160
+ val = @items_hash[babble]
161
+ new_items_hash.merge!(babble => val)
162
+ new_items_formatted.merge!(@hashmap[babble] => @items_formatted[@hashmap[babble]])
163
+ new_hashmap.merge!(babble => @hashmap[babble])
164
+ next
165
+ end
166
+
167
+ parent = Format.parse_format(
168
+ @pref.get(@format)["parent"],
169
+ item[0].attributes,
170
+ @term.cols
171
+ )
172
+
173
+ children = []
174
+ children_hash = []
175
+
176
+ if @children
177
+ item[1].each { |subitem|
178
+
179
+ child_babble = subitem.cereal
180
+
181
+ if @items_hash.key? child_babble
182
+ val = @items_hash[babble]
183
+ new_items_hash.merge!(child_babble => val)
184
+ children<<@hashmap[babble]
185
+ new_hashmap.merge!(babble => @hashmap[babble])
186
+ next
187
+ end
188
+
189
+ children << Format.parse_format(
190
+ @pref.get(@format)["child"],
191
+ subitem.attributes,
192
+ @term.cols
193
+ )
194
+ children_hash << subitem.hash
195
+
196
+ }
197
+ end
198
+
199
+ new_items_hash.merge!(babble => children_hash)
200
+ new_items_formatted.merge!(parent => children)
201
+ new_hashmap.merge!(babble => parent)
202
+ }
203
+
204
+ @items_formatted = new_items_formatted
205
+ @items_hash = new_items_hash
206
+ @hashmap = new_hashmap
207
+
208
+ @title_formatted = Format.parse_format(
209
+ @pref.get(@format)["title"],
210
+ @attributes,
211
+ @term.cols
212
+ )
213
+ end
214
+
215
+ def adjustView
216
+ buffer = 2
217
+ bottom = @term.rows-3
218
+
219
+ diff = 0
220
+ #@logger.log("pre first loop: #{@selected}, #{buffer}, #{@view}")
221
+ if @selected < @view[0] + buffer #and @selected > buffer
222
+ while @selected < @view[0] + buffer
223
+ @view[0]-=1
224
+ @view[1]-=1
225
+ @logger.log("first loop: #{@selected}, #{@view}")
226
+ end
227
+ elsif @selected > @view[1] - (buffer+1) and @selected < getLen - buffer
228
+ @logger.log("")
229
+ while @selected > @view[1] - (buffer+1) and @selected < getLen - buffer
230
+ @logger.log("second loop: #{@selected}, #{@view}")
231
+ @view[0]+=1
232
+ @view[1]+=1
233
+ @logger.log("-----------: #{@selected}, #{@view}")
234
+ end
235
+ end
236
+
237
+ @logger.log("#{@selected}, #{@view}")
238
+
239
+ diff = 0 - @view[0] if @view[0] < 0
240
+ diff = bottom - @view[1] if @view[1] < bottom
241
+
242
+ @view[0] += diff
243
+ @view[1] += diff
244
+ end
245
+
246
+ def draw
247
+ refreshFormats
248
+ drawFrom(@view[0], @view[1])
249
+ end
250
+
251
+ def drawFrom(line_on, lines_to_draw)
252
+ #print menu header
253
+ lo="\e[1;1H"
254
+ print lo + @cl["m"] + @title_formatted + "\e[K"
255
+
256
+ resolved_start_position = resolveSelected line_on
257
+
258
+ outer_index = resolved_start_position["out"]
259
+ inner_index = resolved_start_position["in"]
260
+
261
+ if resolved_start_position["in_expanded"]
262
+ outer_index+=1
263
+ end
264
+
265
+ offset=line_on-1
266
+
267
+ while line_on < lines_to_draw
268
+
269
+ in_outer_region = ! (line_on >= getLen)
270
+ in_expanded_region = resolveSelected(line_on)["in_expanded"]
271
+ line = line_on-offset+1
272
+ selected = line_on == @selected
273
+
274
+ cl = ""
275
+ cl = @cl["s"] if selected
276
+ lo = "\e[#{line};1H" + cl
277
+
278
+ if in_expanded_region and @children
279
+ print @cl["2"] + lo + String(@items_formatted.values[@expanded][inner_index]) + "\e[K" unless @items_formatted.values[@expanded] == nil
280
+ inner_index+=1
281
+ elsif in_outer_region
282
+ print @cl["1"] + lo + String(@items_formatted.keys[outer_index]) + "\e[K"
283
+ outer_index+=1
284
+ else
285
+ print "\e[#{line};1H\e[2K\n"
286
+ end
287
+
288
+ line_on+=1
289
+ end
290
+ end
291
+
292
+ def control(key)
293
+ sel = resolveSelected
294
+ if key == @pref.get("keybindings")["menu_nav_up"]
295
+ moveCursor("up")
296
+ elsif key == @pref.get("keybindings")["menu_nav_down"]
297
+ moveCursor("down")
298
+ else
299
+ customControl(key, sel)
300
+ end
301
+ end
302
+
303
+ def customControl(key, sel); end
304
+
305
+ end