Skip to content

Commit 74b5067

Browse files
author
–Raphael Hunziker
committed
Store 20 bind phrases and scroll the history list
The history was capped at 5 entries, and the cap was on the stored data rather than the display: adding a sixth phrase silently dropped the oldest one from history.txt. Anyone managing a larger fleet loses phrases that are still in use and has to retype them. Show the history through a scrolling window of History.PAGE (5) rows so the on-screen footprint no longer depends on how much is stored, which is what forced the low cap in the first place. With that decoupled, raise the stored limit to 20. Up/Down buttons move the window a page at a time and are only shown when the history is longer than one page. A label shows the current position.
1 parent ecc2052 commit 74b5067

1 file changed

Lines changed: 72 additions & 5 deletions

File tree

src/SCRIPTS/TOOLS/elrs-bind/main.lua

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ function Defer.poll()
4242
end
4343

4444
local History = {
45-
MAX = 5,
45+
MAX = 20,
46+
PAGE = 5,
4647
FNAME = "history.txt",
4748
vals = {},
4849
}
4950

51+
-- Index of the first history entry shown; scrolls in steps of History.PAGE
52+
local histOffset = 0
53+
5054
function History.add(s)
5155
if s == nil or s == "" then
5256
return
@@ -65,6 +69,9 @@ function History.add(s)
6569
table.remove(History.vals)
6670
end
6771

72+
-- a new entry goes to the top of the list, make sure it is on screen
73+
histOffset = 0
74+
6875
History.save()
6976
end
7077

@@ -276,18 +283,45 @@ local function sendBindRx()
276283
end
277284

278285
local rebuildUi
286+
287+
-- Keep the scroll window inside the bounds of the current history
288+
local function history_clampOffset()
289+
local maxOffset = #History.vals - History.PAGE
290+
if maxOffset < 0 then
291+
maxOffset = 0
292+
end
293+
if histOffset > maxOffset then
294+
histOffset = maxOffset
295+
end
296+
if histOffset < 0 then
297+
histOffset = 0
298+
end
299+
end
300+
279301
local function history_text(id)
280-
return History.vals[id]
302+
return History.vals[id + histOffset]
281303
end
282304
local function history_visible(id)
283305
return history_text(id) ~= nil
284306
end
285307
local function history_press(id)
286-
bindPhrase = History.vals[id]
308+
bindPhrase = History.vals[id + histOffset]
287309
rebuildUi()
288310
end
289311
local function history_remove(id)
290-
History.remove(id)
312+
History.remove(id + histOffset)
313+
history_clampOffset()
314+
end
315+
local function history_scroll(delta)
316+
histOffset = histOffset + delta
317+
history_clampOffset()
318+
end
319+
local function history_position()
320+
local last = histOffset + History.PAGE
321+
if last > #History.vals then
322+
last = #History.vals
323+
end
324+
return string.format("%d-%d / %d", histOffset + 1, last, #History.vals)
291325
end
292326

293327
rebuildUi = function()
@@ -425,7 +459,7 @@ rebuildUi = function()
425459
w = lvgl.PERCENT_SIZE + 100,
426460
align = CENTER,
427461
})
428-
for i = 1, History.MAX do
462+
for i = 1, History.PAGE do
429463
local row = histSection:box({
430464
w = lvgl.PERCENT_SIZE + 100,
431465
flexFlow = lvgl.FLOW_ROW,
@@ -453,6 +487,39 @@ rebuildUi = function()
453487
end,
454488
})
455489
end
490+
491+
-- Scroll controls, only shown when the history is longer than one page
492+
local navRow = histSection:box({
493+
w = lvgl.PERCENT_SIZE + 100,
494+
flexFlow = lvgl.FLOW_ROW,
495+
flexPad = lvgl.PAD_SMALL,
496+
visible = function()
497+
return #History.vals > History.PAGE
498+
end,
499+
})
500+
navRow:button({
501+
text = "Up",
502+
press = function()
503+
return history_scroll(-History.PAGE)
504+
end,
505+
active = function()
506+
return histOffset > 0
507+
end,
508+
})
509+
navRow:button({
510+
text = "Down",
511+
press = function()
512+
return history_scroll(History.PAGE)
513+
end,
514+
active = function()
515+
return histOffset + History.PAGE < #History.vals
516+
end,
517+
})
518+
navRow:label({
519+
text = function()
520+
return history_position()
521+
end,
522+
})
456523
end
457524

458525
local function init()

0 commit comments

Comments
 (0)