Skip to content

Commit

Permalink
#403 guide searching
Browse files Browse the repository at this point in the history
MikaylaFischler committed Jun 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 09c44a6 commit 356caf7
Showing 3 changed files with 58 additions and 11 deletions.
61 changes: 54 additions & 7 deletions pocket/ui/apps/guide.lua
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@
-- System Guide
--

-- local util = require("scada-common.util")
local util = require("scada-common.util")
-- local log = require("scada-common.log")

local iocontrol = require("pocket.iocontrol")
local TextField = require("graphics.elements.form.text_field")
local TextField = require("graphics.elements.form.text_field")

local docs = require("pocket.ui.docs")
local style = require("pocket.ui.style")
@@ -73,10 +73,10 @@ local function new_view(root)
local panes = { home, search, use, uis, fps, gls }

local doc_map = {}
local search_map = {}
local search_db = {}

---@class _guide_section_constructor_data
local sect_construct_data = { app, page_div, panes, doc_map, search_map, btn_fg_bg, btn_active }
local sect_construct_data = { app, page_div, panes, doc_map, search_db, btn_fg_bg, btn_active }

TextBox{parent=home,y=1,text="cc-mek-scada Guide",height=1,alignment=ALIGN.CENTER}

@@ -88,10 +88,57 @@ local function new_view(root)

TextBox{parent=search,y=1,text="Search",height=1,alignment=ALIGN.CENTER}

TextField{parent=search,x=1,y=3,width=18,fg_bg=cpair(colors.white,colors.gray)}
PushButton{parent=search,x=20,y=3,text="GO",fg_bg=btn_fg_bg,active_fg_bg=btn_active,callback=function()end}
local query_field = TextField{parent=search,x=1,y=3,width=18,fg_bg=cpair(colors.white,colors.gray)}

local search_results = ListBox{parent=search,x=1,y=5,scroll_height=100,nav_fg_bg=cpair(colors.lightGray,colors.gray),nav_active=cpair(colors.white,colors.gray)}
local func_ref = {}

PushButton{parent=search,x=20,y=3,text="GO",fg_bg=btn_fg_bg,active_fg_bg=btn_active,callback=function()func_ref.run_search()end}

local search_results = ListBox{parent=search,x=1,y=5,scroll_height=200,nav_fg_bg=cpair(colors.lightGray,colors.gray),nav_active=cpair(colors.white,colors.gray)}

function func_ref.run_search()
local query = string.lower(query_field.get_value())
local s_results = { {}, {}, {} }

search_results.remove_all()

if string.len(query) < 3 then
TextBox{parent=search_results,text=util.trinary(string.len(query)==0,"Click 'GO' to search...","Search requires at least 3 characters.")}
return
end

for _, entry in ipairs(search_db) do
local s_start, _ = string.find(entry[1], query, 1, true)

if s_start == nil then
elseif s_start == 1 then
-- best match, start of key
table.insert(s_results[1], entry)
elseif string.sub(query, s_start - 1, s_start) == " " then
-- start of word, good match
table.insert(s_results[2], entry)
else
-- basic match in content
table.insert(s_results[3], entry)
end
end

local empty = true

for tier = 1, 3 do
for idx = 1, #s_results[tier] do
local entry = s_results[tier][idx]
TextBox{parent=search_results,text=entry[3].." >",fg_bg=cpair(colors.gray,colors.black)}
PushButton{parent=search_results,text=entry[2],alignment=ALIGN.LEFT,fg_bg=btn_fg_bg,active_fg_bg=btn_active,callback=entry[4]}

empty = false
end
end

if empty then
TextBox{parent=search_results,text="No results found."}
end
end

TextBox{parent=search_results,text="Click 'GO' to search..."}

2 changes: 1 addition & 1 deletion pocket/ui/docs.lua
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ doc("automatic", "Auto Reactor SCRAM", "Indicates if the automatic control syste
doc("sys_fail", "System Failure", "Indicates if the RPS tripped due to the reactor not being formed.")
doc("high_dmg", "Damage Level High", "Indicates if the RPS tripped due to significant reactor damage.")
doc("ex_waste", "Excess Waste", "Indicates if the RPS tripped due to very high waste levels.")
doc("ex_hcool", "Excess Heated Coolant", "Indicates if the RPS tripped due to very high waste levels.")
doc("ex_hcool", "Excess Heated Coolant", "Indicates if the RPS tripped due to very high heated coolant levels.")
doc("high_temp", "Temperature High", "Indicates if the RPS tripped due to reaching damaging temperatures.")
doc("low_cool", "Coolant Level Low Low", "Indicates if the RPS tripped due to very low coolant levels that result in the temperature uncontrollably rising.")
doc("no_fuel", "No Fuel", "Indicates if the RPS tripped due to no fuel being available.")
6 changes: 3 additions & 3 deletions pocket/ui/pages/guide_section.lua
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ local cpair = core.cpair
---@param scroll_height integer
---@return nav_tree_page
return function (data, base_page, title, items, scroll_height)
local app, page_div, panes, doc_map, search_map, btn_fg_bg, btn_active = table.unpack(data)
local app, page_div, panes, doc_map, search_db, btn_fg_bg, btn_active = table.unpack(data)

local section_page = app.new_page(base_page, #panes + 1)
local section_div = Div{parent=page_div,x=2}
@@ -50,9 +50,9 @@ return function (data, base_page, title, items, scroll_height)
end

doc_map[item.key] = view
search_map[item.name] = view
table.insert(search_db, { string.lower(item.name), item.name, title, view })

PushButton{parent=name_list,text=item.name,fg_bg=cpair(colors.blue,colors.black),active_fg_bg=btn_active,callback=view}
PushButton{parent=name_list,text=item.name,alignment=ALIGN.LEFT,fg_bg=cpair(colors.blue,colors.black),active_fg_bg=btn_active,callback=view}
end

return section_page

0 comments on commit 356caf7

Please sign in to comment.