diff options
author | Solderpunk <solderpunk@sdf.org> | 2019-11-05 19:51:44 +0000 |
---|---|---|
committer | Solderpunk <solderpunk@sdf.org> | 2019-11-05 19:51:44 +0000 |
commit | d299f91246448a2de0a36c4b4fe66ec50e679df1 (patch) | |
tree | 37792803100d0005255dfc6ff3a6329b62ad4963 /telem.lua | |
parent | Make finger invocation work on GNU/Linux. (diff) |
Initial implementation of search function.
Diffstat (limited to '')
-rwxr-xr-x | telem.lua | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -523,6 +523,55 @@ function do_scan() end end +function do_search() + -- Get search term + io.write("Search term? ") + local searchterm = io.read() + if string.len(searchterm) == 0 then + print("Search cancelled.") + return + end + local hits = {} + local N = 10 + for i, board in ipairs(boards) do + local threads = get_threads(board) + for _, thread in ipairs(threads) do + local x = {} + x.board = board + x.thread = thread + if string.find(string.lower(x.thread.subject), string.lower(searchterm)) == nil then + goto continue + end + if #hits < N then + table.insert(hits, x) + table.sort(hits, function(x,y) return x.thread.updated > y.thread.updated end) + elseif thread.updated > hits[N].thread.updated then + table.remove(hits, N) + table.insert(hits, x) + table.sort(hits, function(x,y) return x.thread.updated > y.thread.updated end) + end + ::continue:: + end + end + if #hits == 0 then + print("No hits found.") + return + end + print("Most recently active matching threads:") + print(colourise("magenta", string.format("%s %s %s", + stringx.ljust("Board name", 20), + stringx.ljust("Thread topic" ,48), + "Latest post")) + ) + print(string.rep("-",79)) + for i, update in ipairs(hits) do + io.write(colourise("cyan", stringx.ljust(update.board.name,20)) .. " " .. + stringx.ljust(update.thread.subject, 48) .. " " .. + os.date("%B %d, %Y", update.thread.updated) .. "\n") + end + print(string.rep("-",79)) +end + -- The "type" command is implemented using multiple functions -- do_type is the entry point, which runs a command dispatch loop that -- runs various do_type_xxx functions. @@ -723,6 +772,7 @@ dispatch["r"] = do_reply dispatch["R"] = do_recent dispatch["b"] = do_board dispatch["c"] = do_colour +dispatch["/"] = do_search -- Infinite loop of command dispatch function prompt() |