aboutsummaryrefslogtreecommitdiff
path: root/telem.lua
diff options
context:
space:
mode:
authorSolderpunk <solderpunk@sdf.org>2019-11-05 20:30:09 +0000
committerSolderpunk <solderpunk@sdf.org>2019-11-05 20:30:09 +0000
commitc32d8a5db030168b042ad52b2a6dd99bb34d4d87 (patch)
treea9cb88f135f8c56ce506bf55327065e796ba6e8a /telem.lua
parentInitial implementation of search function. (diff)
Factor out duplicated code between do_recent and do_search.
Diffstat (limited to '')
-rwxr-xr-xtelem.lua67
1 files changed, 27 insertions, 40 deletions
diff --git a/telem.lua b/telem.lua
index a087954..3c6ac8e 100755
--- a/telem.lua
+++ b/telem.lua
@@ -471,24 +471,7 @@ function do_new()
end
function do_recent()
- local recent = {}
- 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 #recent < N then
- table.insert(recent, x)
- table.sort(recent, function(x,y) return x.thread.updated > y.thread.updated end)
- elseif thread.updated > recent[N].thread.updated then
- table.remove(recent, N)
- table.insert(recent, x)
- table.sort(recent, function(x,y) return x.thread.updated > y.thread.updated end)
- end
- end
- end
+ local recent = filter_latest_posts(function(x) return true end, 10)
print("Most recently active threads:")
print(colourise("magenta", string.format("%s %s %s",
stringx.ljust("Board name", 20),
@@ -504,6 +487,29 @@ function do_recent()
print(string.rep("-",79))
end
+function filter_latest_posts(filter, N)
+ local matches = {}
+ 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 filter(x) == nil then
+ -- pass
+ elseif #matches < N then
+ table.insert(matches, x)
+ table.sort(matches, function(x,y) return x.thread.updated > y.thread.updated end)
+ elseif thread.updated > matches[N].thread.updated then
+ table.remove(matches, N)
+ table.insert(matches, x)
+ table.sort(matches, function(x,y) return x.thread.updated > y.thread.updated end)
+ end
+ end
+ end
+ return matches
+end
+
function do_scan()
for i, board in ipairs(boards) do
io.write(string.format("%3d ", i))
@@ -531,28 +537,9 @@ function do_search()
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
+ local hits = filter_latest_posts(function(x)
+ return string.find(string.lower(x.thread.subject), string.lower(searchterm))
+ end, 10)
if #hits == 0 then
print("No hits found.")
return