summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/print.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/print.lua')
-rw-r--r--apps/plugins/lua/include_lua/print.lua80
1 files changed, 36 insertions, 44 deletions
diff --git a/apps/plugins/lua/include_lua/print.lua b/apps/plugins/lua/include_lua/print.lua
index 9b21dafb9d..3a11e7c3dc 100644
--- a/apps/plugins/lua/include_lua/print.lua
+++ b/apps/plugins/lua/include_lua/print.lua
@@ -49,13 +49,12 @@ local _print = {} do
49 49
50 local _NIL = nil -- _NIL placeholder 50 local _NIL = nil -- _NIL placeholder
51 local _LCD = rb.lcd_framebuffer() 51 local _LCD = rb.lcd_framebuffer()
52 local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
53 local WHITE = _clr.set(-1, 255, 255, 255) 52 local WHITE = _clr.set(-1, 255, 255, 255)
54 local BLACK = _clr.set(0, 0, 0, 0) 53 local BLACK = _clr.set(0, 0, 0, 0)
55 local DRMODE_SOLID = 3 54 local DRMODE_SOLID = 3
56 local col_buf, s_lines = {}, {} 55 local col_buf, s_lines = {}, {}
57 local _p_opts = _NIL 56 local _p_opts = _NIL
58 57 local tabstring = string.rep(" ", 2)
59-- print internal helper functions 58-- print internal helper functions
60-------------------------------------------------------------------------------- 59--------------------------------------------------------------------------------
61 -- clamps value to >= min and <= max 60 -- clamps value to >= min and <= max
@@ -69,46 +68,20 @@ local _print = {} do
69 return max 68 return max
70 end 69 end
71 70
72 -- updates screen in specified rectangle
73 local function update_rect(x, y, w, h)
74 rb.lcd_update_rect(x - 1, y - 1,
75 clamp(x + w, 1, LCD_W) - 1,
76 clamp(y + h, 1, LCD_H) - 1)
77 end
78
79 -- Gets size of text 71 -- Gets size of text
80 local function text_extent(msg, font) 72 local function text_extent(msg, font)
81 font = font or rb.FONT_UI
82 -- res, w, h 73 -- res, w, h
83 return rb.font_getstringsize(msg, font) 74 return rb.font_getstringsize(msg, font or rb.FONT_UI)
84 end
85
86 -- Sets viewport size
87 local function set_viewport(vp)
88 if not vp then rb.set_viewport() return end
89
90 if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
91 --vp.drawmode = bit.bxor(vp.drawmode, 4)
92 vp.fg_pattern = 3 - vp.fg_pattern
93 vp.bg_pattern = 3 - vp.bg_pattern
94 end
95 rb.set_viewport(vp)
96 end
97
98 -- shallow copy of table
99 function table_clone(t)
100 local copy = {}
101 for k, v in pairs(t) do
102 copy[k] = v
103 end
104 return copy
105 end 75 end
106 76
107 -- Updates a single line on the screen 77 -- Updates a single line on the screen
108 local function update_line(enabled, opts, line, h) 78 local function update_line(enabled, opts, line, h)
109 if enabled ~= true then return end 79 if enabled ~= true then return end
110 local o = opts or _p_opts 80 local o = opts or _p_opts
111 update_rect(o.x, o.y + line * h + 1, o.width, h) 81 -- updates screen in specified rectangle
82 rb.lcd_update_rect(o.x - 1, o.y + line * h,
83 clamp(o.x + o.width, 1, rb.LCD_WIDTH) - 1,
84 clamp(o.y + line * h + 1 + h, 1, rb.LCD_HEIGHT) - 1)
112 end 85 end
113 86
114 -- Clears a single line on the screen 87 -- Clears a single line on the screen
@@ -129,22 +102,23 @@ local _print = {} do
129 local function col_buf_insert(msg, line, _p_opts) 102 local function col_buf_insert(msg, line, _p_opts)
130 --if _p_opts.line <= 1 then col_buf = {} end 103 --if _p_opts.line <= 1 then col_buf = {} end
131 if not col_buf[line] then 104 if not col_buf[line] then
132 table.insert(col_buf, line, msg) end 105 table.insert(col_buf, line, msg)
106 end
133 end 107 end
134 108
135 --replaces / strips escape characters 109 --replaces / strips tab characters
136 local function check_escapes(o, msg) 110 local function check_escapes(o, msg)
111--[[ --for replacing a variety of escapes
137 local tabsz = 2 112 local tabsz = 2
138 local tabstr = string.rep(" ", tabsz) 113 local tabstr = string.rep(" ", tabsz)
139
140 local function repl(esc) 114 local function repl(esc)
141 local ret = "" 115 local ret = ""
142 if esc:sub(1,1) == "\t" then ret = string.rep(tabstr, esc:len()) end 116 if esc:sub(1,1) == "\t" then ret = string.rep(tabstr, esc:len()) end
143 return ret 117 return ret
144 end 118 end
145
146 msg = msg:gsub("(%c+)", repl) 119 msg = msg:gsub("(%c+)", repl)
147 120]]
121 msg = msg:gsub("\t", tabstring)
148 local res, w, h = text_extent(msg, o.font) 122 local res, w, h = text_extent(msg, o.font)
149 return w, h, msg 123 return w, h, msg
150 end 124 end
@@ -154,8 +128,8 @@ local _print = {} do
154 local function set_defaults() 128 local function set_defaults()
155 _p_opts = { x = 1, 129 _p_opts = { x = 1,
156 y = 1, 130 y = 1,
157 width = LCD_W - 1, 131 width = rb.LCD_WIDTH - 1,
158 height = LCD_H - 1, 132 height = rb.LCD_HEIGHT - 1,
159 font = rb.FONT_UI, 133 font = rb.FONT_UI,
160 drawmode = DRMODE_SOLID, 134 drawmode = DRMODE_SOLID,
161 fg_pattern = WHITE, 135 fg_pattern = WHITE,
@@ -178,7 +152,15 @@ local _print = {} do
178 -- if bByRef is _NIL or false then a copy is returned 152 -- if bByRef is _NIL or false then a copy is returned
179 local function get_settings(bByRef) 153 local function get_settings(bByRef)
180 _p_opts = _p_opts or set_defaults() 154 _p_opts = _p_opts or set_defaults()
181 if not bByRef then return table_clone(_p_opts) end 155 if not bByRef then
156 -- shallow copy of table
157 local copy = {}
158 for k, v in pairs(_p_opts) do
159 copy[k] = v
160 end
161 return copy
162 end
163
182 return _p_opts 164 return _p_opts
183 end 165 end
184 166
@@ -216,7 +198,16 @@ local _print = {} do
216 -- alternative selection method 198 -- alternative selection method
217 --msg = "> " .. msg 199 --msg = "> " .. msg
218 end 200 end
219 set_viewport(o) 201
202 if not o then rb.set_viewport() return end
203
204 if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
205 o.fg_pattern = 3 - o.fg_pattern
206 o.bg_pattern = 3 - o.bg_pattern
207 end
208
209 rb.set_viewport(o)
210
220 o = _NIL 211 o = _NIL
221 end 212 end
222 213
@@ -251,8 +242,8 @@ local _print = {} do
251 -- sets print area 242 -- sets print area
252 local function set_area(x, y, w, h) 243 local function set_area(x, y, w, h)
253 local o = get_settings(true) 244 local o = get_settings(true)
254 o.x, o.y = clamp(x, 1, LCD_W), clamp(y, 1, LCD_H) 245 o.x, o.y = clamp(x, 1, rb.LCD_WIDTH), clamp(y, 1, rb.LCD_HEIGHT)
255 o.width, o.height = clamp(w, 1, LCD_W - o.x), clamp(h, 1, LCD_H - o.y) 246 o.width, o.height = clamp(w, 1, rb.LCD_WIDTH - o.x), clamp(h, 1, rb.LCD_HEIGHT - o.y)
256 o.max_line = max_lines(_p_opts) 247 o.max_line = max_lines(_p_opts)
257 248
258 clear() 249 clear()
@@ -312,6 +303,7 @@ local _print = {} do
312 local o = get_settings(true) 303 local o = get_settings(true)
313 local w, h, msg 304 local w, h, msg
314 local line = o.line - 1 -- rb is 0-based lua is 1-based 305 local line = o.line - 1 -- rb is 0-based lua is 1-based
306
315 if not (...) or (...) == "\n" then -- handles blank line / single '\n' 307 if not (...) or (...) == "\n" then -- handles blank line / single '\n'
316 local res, w, h = text_extent(" ", o.font) 308 local res, w, h = text_extent(" ", o.font)
317 309