diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/print.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/print.lua | 165 |
1 files changed, 127 insertions, 38 deletions
diff --git a/apps/plugins/lua/include_lua/print.lua b/apps/plugins/lua/include_lua/print.lua index 3fb19bef1f..3b4c389848 100644 --- a/apps/plugins/lua/include_lua/print.lua +++ b/apps/plugins/lua/include_lua/print.lua | |||
@@ -123,6 +123,49 @@ local _print = {} do | |||
123 | return w, h, msg | 123 | return w, h, msg |
124 | end | 124 | end |
125 | -------------------------------------------------------------------------------- | 125 | -------------------------------------------------------------------------------- |
126 | local function set_linedesc(t_linedesc, opts) | ||
127 | local o = opts or _print.opt.get(true) | ||
128 | --local out = function() local t = {} for k, v in pairs(o) do t[#t + 1] = tostring(k) t[#t + 1] = tostring(v) end return table.concat(t, "\n") end | ||
129 | --rb.splash_scroller(1000, out()) | ||
130 | local linedesc ={ | ||
131 | --These are the defaults - changes will be made below if you supplied t_linedesc | ||
132 | indent = 0, -- internal indent text | ||
133 | line = 0, -- line index within group | ||
134 | nlines = 1, -- selection grouping | ||
135 | offset = 0, -- internal item offset | ||
136 | scroll = true, | ||
137 | selected = false, --internal | ||
138 | separator_height = 0, | ||
139 | line_separator = false, | ||
140 | show_cursor = false, | ||
141 | show_icons = false, | ||
142 | icon = -1, | ||
143 | icon_fn = function() return -1 end, | ||
144 | style = rb.STYLE_COLORBAR, | ||
145 | text_color = o.fg_pattern or WHITE, | ||
146 | line_color = o.bg_pattern or BLACK, | ||
147 | line_end_color= o.bg_pattern or BLACK, | ||
148 | } | ||
149 | if type(t_linedesc) == "table" then | ||
150 | |||
151 | if not o.linedesc then | ||
152 | o.linedesc = {} | ||
153 | for k, v in pairs(linedesc) do | ||
154 | o.linedesc[k] = v | ||
155 | end | ||
156 | end | ||
157 | |||
158 | for k, v in pairs(t_linedesc) do | ||
159 | o.linedesc[k] = v | ||
160 | end | ||
161 | if o.linedesc.separator_height > 0 then | ||
162 | o.linedesc.line_separator = true | ||
163 | end | ||
164 | return | ||
165 | end | ||
166 | o.linedesc = linedesc | ||
167 | return o.linedesc | ||
168 | end | ||
126 | 169 | ||
127 | -- set defaults for print view | 170 | -- set defaults for print view |
128 | local function set_defaults() | 171 | local function set_defaults() |
@@ -138,11 +181,13 @@ local _print = {} do | |||
138 | line = 1, | 181 | line = 1, |
139 | max_line = _NIL, | 182 | max_line = _NIL, |
140 | col = 0, | 183 | col = 0, |
141 | ovfl = "auto", | 184 | header = false, --Internal use - treats first entry as header |
142 | justify = "left", | 185 | ovfl = "auto", -- auto, manual, none |
143 | autoupdate = true, | 186 | justify = "left", --left, center, right |
144 | drawsep = false, | 187 | autoupdate = true, --updates screen as items are added |
188 | drawsep = false, -- separator between items | ||
145 | } | 189 | } |
190 | set_linedesc(nil, _p_opts) -- default line display | ||
146 | _p_opts.max_line = max_lines(_p_opts) | 191 | _p_opts.max_line = max_lines(_p_opts) |
147 | 192 | ||
148 | s_lines, col_buf = {}, {} | 193 | s_lines, col_buf = {}, {} |
@@ -187,29 +232,16 @@ local _print = {} do | |||
187 | 232 | ||
188 | -- helper function sets up colors/marker for selected items | 233 | -- helper function sets up colors/marker for selected items |
189 | local function show_selected(iLine, msg) | 234 | local function show_selected(iLine, msg) |
190 | local o = get_settings() -- using a copy of opts so changes revert | ||
191 | |||
192 | if s_lines[iLine] == true then | ||
193 | if not rb.lcd_set_background then | ||
194 | o.drawmode = bit.bxor(o.drawmode, 4) | ||
195 | else | ||
196 | o.fg_pattern = o.bg_pattern | ||
197 | o.bg_pattern = o.sel_pattern | ||
198 | end | ||
199 | -- alternative selection method | ||
200 | --msg = "> " .. msg | ||
201 | end | ||
202 | |||
203 | if not o then rb.set_viewport() return end | ||
204 | |||
205 | if rb.LCD_DEPTH == 2 then -- invert 2-bit screens | 235 | if rb.LCD_DEPTH == 2 then -- invert 2-bit screens |
236 | local o = get_settings() -- using a copy of opts so changes revert | ||
237 | if not o then rb.set_viewport() return end | ||
206 | o.fg_pattern = 3 - o.fg_pattern | 238 | o.fg_pattern = 3 - o.fg_pattern |
207 | o.bg_pattern = 3 - o.bg_pattern | 239 | o.bg_pattern = 3 - o.bg_pattern |
240 | rb.set_viewport(o) | ||
241 | o = _NIL | ||
242 | else | ||
243 | show_selected = function() end -- no need to check again | ||
208 | end | 244 | end |
209 | |||
210 | rb.set_viewport(o) | ||
211 | |||
212 | o = _NIL | ||
213 | end | 245 | end |
214 | 246 | ||
215 | -- sets line explicitly or increments line if line is _NIL | 247 | -- sets line explicitly or increments line if line is _NIL |
@@ -274,31 +306,82 @@ local _print = {} do | |||
274 | s_lines[iLine] = true | 306 | s_lines[iLine] = true |
275 | end | 307 | end |
276 | 308 | ||
277 | -- Internal print function | 309 | -- Internal print function |
278 | local function print_internal(t_opts, x, w, h, msg) | 310 | local function print_internal(t_opts, x, w, h, msg) |
279 | 311 | ||
312 | local linedesc | ||
313 | local line_separator = false | ||
280 | local o = t_opts | 314 | local o = t_opts |
281 | if o.justify == "center" then | 315 | local ld = o.linedesc or set_linedesc() |
282 | x = x + (o.width - w) / 2 | 316 | local show_cursor = ld.show_cursor or 0 |
283 | elseif o.justify == "right" then | 317 | local line_indent = 0 |
284 | x = x + (o.width - w) | 318 | |
319 | local linestyle = ld.style or rb.STYLE_COLORBAR | ||
320 | |||
321 | if o.justify ~= "left" then | ||
322 | line_indent = (o.width - w) --"right" | ||
323 | if o.justify == "center" then | ||
324 | line_indent = line_indent / 2 | ||
325 | end | ||
285 | end | 326 | end |
286 | 327 | ||
287 | local line = o.line - 1 -- rb is 0-based lua is 1-based | 328 | local line = o.line - 1 -- rb is 0-based lua is 1-based |
288 | if(o.ovfl == "auto" and w >= o.width) then -- -o.x | 329 | |
289 | rb.lcd_puts_scroll(0, line, msg) | 330 | if o.ovfl == "manual" then --save msg for later side scroll |
331 | col_buf_insert(msg, o.line, o) | ||
332 | end | ||
333 | |||
334 | -- bit of a pain to set the fields this way but its much more efficient than iterating a table to set them | ||
335 | local function set_desc(tld, scroll, separator_height, selected, style, indent, text_color, line_color, line_end_color) | ||
336 | tld.scroll = scroll | ||
337 | tld.separator_height = separator_height | ||
338 | tld.selected = selected | ||
339 | tld.style = style | ||
340 | tld.indent = indent | ||
341 | tld.text_color = text_color | ||
342 | tld.line_color = line_color | ||
343 | tld.line_end_color = line_end_color | ||
344 | end | ||
345 | |||
346 | line_separator = ld.line_separator | ||
347 | |||
348 | if o.line == 1 and o.header then | ||
349 | --rb scroller doesn't like negative offset! | ||
350 | local indent = line_indent < 0 and 0 or line_indent | ||
351 | set_desc(ld, true, 1, false, rb.STYLE_DEFAULT, | ||
352 | indent, o.fg_pattern, o.bg_pattern, o.bg_pattern) | ||
353 | ld.show_cursor = false | ||
354 | elseif s_lines[o.line] then | ||
355 | --/* Display line selector */ | ||
356 | local style = show_cursor == true and rb.STYLE_DEFAULT or linestyle | ||
357 | |||
358 | local indent = line_indent < 0 and 0 or line_indent | ||
359 | --rb scroller doesn't like negative offset! | ||
360 | local ovfl = (o.ovfl == "auto" and w >= o.width and x == 0) | ||
361 | set_desc(ld, ovfl, 0, true, style, indent, | ||
362 | o.bg_pattern, o.sel_pattern, o.sel_pattern) | ||
290 | else | 363 | else |
291 | rb.lcd_putsxy(x, line * h, msg) | 364 | set_desc(ld, false, 0, false, rb.STYLE_DEFAULT,line_indent, |
292 | if o.ovfl == "manual" then --save msg for later side scroll | 365 | o.fg_pattern, o.bg_pattern, o.bg_pattern) |
293 | col_buf_insert(msg, o.line, o) | ||
294 | end | ||
295 | end | 366 | end |
296 | if o.drawsep == true then | 367 | |
297 | if s_lines[o.line] == true then | 368 | if ld.show_icons then |
298 | rb.set_viewport(o) --nned to revert drawmode if selected | 369 | ld.icon = ld.icon_fn(line, ld.icon or -1) |
370 | end | ||
371 | |||
372 | rb.lcd_put_line(x, line *h, msg, ld) | ||
373 | |||
374 | ld.show_cursor = show_cursor | ||
375 | ld.style = linestyle | ||
376 | if line_separator then | ||
377 | if ld.selected == true then | ||
378 | rb.set_viewport(o) -- revert drawmode if selected | ||
299 | end | 379 | end |
300 | rb.lcd_drawline(0, line * h, o.width, line * h) | 380 | rb.lcd_drawline(0, line * h, o.width, line * h) |
381 | rb.lcd_drawline(0, line * h + h, o.width, line * h + h) --only to add the last line | ||
382 | -- but we don't have an idea which line is the last line here so every line is the last line! | ||
301 | end | 383 | end |
384 | |||
302 | --only update the line we changed | 385 | --only update the line we changed |
303 | update_line(o.autoupdate, o, line, h) | 386 | update_line(o.autoupdate, o, line, h) |
304 | 387 | ||
@@ -308,7 +391,7 @@ local _print = {} do | |||
308 | -- Helper function that acts mostly like a normal printf() would | 391 | -- Helper function that acts mostly like a normal printf() would |
309 | local function printf(fmt, v1, ...) | 392 | local function printf(fmt, v1, ...) |
310 | local o = get_settings(true) | 393 | local o = get_settings(true) |
311 | local w, h, msg | 394 | local w, h, msg, rep |
312 | local line = o.line - 1 -- rb is 0-based lua is 1-based | 395 | local line = o.line - 1 -- rb is 0-based lua is 1-based |
313 | 396 | ||
314 | if not (fmt) or (fmt) == "\n" then -- handles blank line / single '\n' | 397 | if not (fmt) or (fmt) == "\n" then -- handles blank line / single '\n' |
@@ -322,6 +405,9 @@ local _print = {} do | |||
322 | return o.line, o.max_line, o.width, h | 405 | return o.line, o.max_line, o.width, h |
323 | end | 406 | end |
324 | 407 | ||
408 | fmt, rep = fmt.gsub(fmt or "", "%%h", "%%s") | ||
409 | o.header = (rep == 1) | ||
410 | |||
325 | msg = string.format(fmt, v1, ...) | 411 | msg = string.format(fmt, v1, ...) |
326 | 412 | ||
327 | show_selected(o.line, msg) | 413 | show_selected(o.line, msg) |
@@ -337,6 +423,8 @@ local _print = {} do | |||
337 | local function set_column(x) | 423 | local function set_column(x) |
338 | local o = get_settings() | 424 | local o = get_settings() |
339 | if o.ovfl ~= "manual" then return end -- no buffer stored to scroll | 425 | if o.ovfl ~= "manual" then return end -- no buffer stored to scroll |
426 | rb.lcd_scroll_stop() | ||
427 | |||
340 | local res, w, h, str, line | 428 | local res, w, h, str, line |
341 | 429 | ||
342 | for key, value in pairs(col_buf) do | 430 | for key, value in pairs(col_buf) do |
@@ -367,6 +455,7 @@ local _print = {} do | |||
367 | _print.opt.justify = set_justify | 455 | _print.opt.justify = set_justify |
368 | _print.opt.sel_line = select_line | 456 | _print.opt.sel_line = select_line |
369 | _print.opt.line = set_line | 457 | _print.opt.line = set_line |
458 | _print.opt.linedesc = set_linedesc | ||
370 | _print.opt.autoupdate = set_update | 459 | _print.opt.autoupdate = set_update |
371 | _print.clear = clear | 460 | _print.clear = clear |
372 | _print.f = printf | 461 | _print.f = printf |