migrate existing lua code to use new library fns

develop
myk002 2021-07-02 23:30:59 -07:00
parent f1ec222d9a
commit f62ff3ecc0
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
4 changed files with 11 additions and 50 deletions

@ -199,7 +199,7 @@ function parse_label_text(obj)
for _,v in ipairs(text) do for _,v in ipairs(text) do
local vv local vv
if type(v) == 'string' then if type(v) == 'string' then
vv = utils.split_string(v, NEWLINE) vv = v:split(NEWLINE)
else else
vv = { v } vv = { v }
end end
@ -803,7 +803,7 @@ function FilteredList:setFilter(filter, pos)
self.edit.text = filter self.edit.text = filter
if filter ~= '' then if filter ~= '' then
local tokens = utils.split_string(filter, ' ') local tokens = filter:split()
local ipos = pos local ipos = pos
choices = {} choices = {}

@ -482,16 +482,7 @@ end
-- Split the string by the given delimiter -- Split the string by the given delimiter
function split_string(self, delimiter) function split_string(self, delimiter)
local result = { } return self:split(delimiter)
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end end
-- Ask a yes-no question -- Ask a yes-no question
@ -545,9 +536,9 @@ end
-- Normalize directory separator slashes across platforms to '/' and collapse -- Normalize directory separator slashes across platforms to '/' and collapse
-- adjacent slashes into a single slash. -- adjacent slashes into a single slash.
local platformSlash = package.config:sub(1,1) local PLATFORM_SLASH = package.config:sub(1,1)
function normalizePath(path) function normalizePath(path)
return path:gsub(platformSlash, '/'):gsub('/+', '/') return path:gsub(PLATFORM_SLASH, '/'):gsub('/+', '/')
end end
function invert(tab) function invert(tab)
@ -618,7 +609,7 @@ function df_expr_to_ref(expr)
expr = expr:gsub('%["(.-)"%]', function(field) return '.' .. field end) expr = expr:gsub('%["(.-)"%]', function(field) return '.' .. field end)
:gsub('%[\'(.-)\'%]', function(field) return '.' .. field end) :gsub('%[\'(.-)\'%]', function(field) return '.' .. field end)
:gsub('%[(%d+)]', function(field) return '.' .. field end) :gsub('%[(%d+)]', function(field) return '.' .. field end)
local parts = split_string(expr, '%.') local parts = expr:split('.', true)
local obj = df_env[parts[1]] local obj = df_env[parts[1]]
for i = 2, #parts do for i = 2, #parts do
local key = tonumber(parts[i]) or parts[i] local key = tonumber(parts[i]) or parts[i]

@ -1,5 +1,6 @@
local _ENV = mkmodule('plugins.blueprint') local _ENV = mkmodule('plugins.blueprint')
local argparse = require('argparse')
local utils = require('utils') local utils = require('utils')
-- the info here is very basic and minimal, so hopefully we won't need to change -- the info here is very basic and minimal, so hopefully we won't need to change
@ -43,17 +44,11 @@ local valid_phase_list = {
valid_phases = utils.invert(valid_phase_list) valid_phases = utils.invert(valid_phase_list)
local function parse_cursor(opts, arg) local function parse_cursor(opts, arg)
local _, _, x, y, z = arg:find('^(%d+),(%d+),(%d+)$') local cursor = argparse.coords(arg)
if not x then
qerror(('invalid argument for --cursor option: "%s"; expected format' ..
' is "<x>,<y>,<z>", for example: "30,60,150"'):format(arg))
end
-- be careful not to replace struct members when called from C++, but also -- be careful not to replace struct members when called from C++, but also
-- create the table as needed when called from lua -- create the table as needed when called from lua
if not opts.start then opts.start = {} end if not opts.start then opts.start = {} end
opts.start.x = tonumber(x) utils.assign(opts.start, cursor)
opts.start.y = tonumber(y)
opts.start.z = tonumber(z)
end end
local function parse_positionals(opts, args, start_argidx) local function parse_positionals(opts, args, start_argidx)
@ -93,7 +88,7 @@ local function process_args(opts, args)
return return
end end
return utils.processArgsGetopt(args, { return argparse.processArgsGetopt(args, {
{'c', 'cursor', hasArg=true, {'c', 'cursor', hasArg=true,
handler=function(optarg) parse_cursor(opts, optarg) end}, handler=function(optarg) parse_cursor(opts, optarg) end},
{'h', 'help', handler=function() opts.help = true end}, {'h', 'help', handler=function() opts.help = true end},

@ -922,31 +922,6 @@ function matchall(haystack, needles)
return true return true
end end
function splitstring(full, pattern)
local last = string.len(full)
local result = {}
local n = 1
while n <= last do
local start, stop = string.find(full, pattern, n)
if not start then
result[#result+1] = string.sub(full, n)
break
elseif start > n then
result[#result+1] = string.sub(full, n, start - 1)
end
if stop < n then
-- The pattern matches an empty string.
-- Avoid an infinite loop.
break
end
n = stop + 1
end
return result
end
function screen:refilter() function screen:refilter()
-- Determine which rows to show, and in which colors. -- Determine which rows to show, and in which colors.
-- Todo: The official one now has three categories of search results: -- Todo: The official one now has three categories of search results:
@ -956,7 +931,7 @@ function screen:refilter()
self.page_size = self.frame_rect.height - ExtraLines self.page_size = self.frame_rect.height - ExtraLines
local filtered = {} local filtered = {}
local needles = splitstring(self.search_string, " ") local needles = self.search_string:split()
for key, value in ipairs(reaction_list) do for key, value in ipairs(reaction_list) do
if matchall(string.upper(value.name), needles) then if matchall(string.upper(value.name), needles) then
filtered[#filtered+1] = { filtered[#filtered+1] = {