Last edit: @kyork in #marvin-dev 2 years ago - Slack Archive

{lua} local name = argv[1]
if name == nil then return "usage: listfactoid {name} [op] [arg] | op can be: read, add {value}, remove {partial}" end
local g = fmap.lists
local op = argv[2]
if op == nil or op == "" then op = "read" end
function help()
 return "to add: _add {string}_ to remove: _remove {partial match}_"
end
function read()
 local t = g[name]
 return table.concat(t, " | ")
end
function add()
 local t = g[name]
 local v = table.concat(argv, " ", 3)
 if v == "" then return "add needs a string to add" end
 table.insert(t, v)
 g[name] = t
 return "added `" .. v .. "`"
end
function remove()
 local t = g[name]
 local query = table.concat(argv, " ", 3)
 if query == "" then return "remove needs a string to match" end
 local pos = nil
 for i, v in ipairs(t) do
  if string.match(v, query) ~= nil then
   pos = i
  end
 end
 if pos == nil then return "remove: no matches for `" .. query .. "`" end
 table.remove(t, pos)
 g[name] = t
 return "removed #" .. pos
end
function clear()
 local t = json.load("[]")
 g[name] = t
 return "reset table " .. name
end
local funcs = {help=help, read=read, add=add, remove=remove, clear=clear, reset=clear}
local func = funcs[op]
if func == nil then return help() end
return func()

History

Revision: 1624 by @kyork in #marvin-dev 2 years agoSlack Archive

{lua} local name = argv[1]
if name == nil then return "usage: listfactoid {name} [op] [arg] | op can be: read, add {value}, remove {partial}" end
local g = fmap.lists
local op = argv[2]
if op == nil or op == "" then op = "read" end
function help()
 return "to add: _add {string}_ to remove: _remove {partial match}_"
end
function read()
 local t = g[name]
 return table.concat(t, " | ")
end
function add()
 local t = g[name]
 local v = table.concat(argv, " ", 3)
 if v == "" then return "add needs a string to add" end
 table.insert(t, v)
 g[name] = t
 return "added `" .. v .. "`"
end
function remove()
 local t = g[name]
 local query = table.concat(argv, " ", 3)
 if query == "" then return "remove needs a string to match" end
 local pos = nil
 for i, v in ipairs(t) do
  if string.match(v, query) ~= nil then
   pos = i
  end
 end
 if pos == nil then return "remove: no matches for `" .. query .. "`" end
 table.remove(t, pos)
 g[name] = t
 return "removed #" .. pos
end
function clear()
 local t = json.load("[]")
 g[name] = t
 return "reset table " .. name
end
local funcs = {help=help, read=read, add=add, remove=remove, clear=clear, reset=clear}
local func = funcs[op]
if func == nil then return help() end
return func()

Revision: 1623 by @kyork in #marvin-dev 2 years agoSlack Archive

{lua} local name = argv[1]
if name == nil then return "usage: listfactoid {name} [op] [arg] | op can be: read, add {value}, remove {partial}" end
local g = fmap.lists
local op = argv[2]
if op == nil or op == "" then op = "read" end
function help()
 return "to add: _add {string}_ to remove: _remove {partial match}_"
end
function read()
 local t = g[name]
 return table.concat(t, " | ")
end
function add()
 local t = g[name]
 local v = table.concat(argv, " ", 3)
 if v == "" then return "add needs a string to add" end
 table.insert(t, v)
 g[name] = t
 return "added `" .. v .. "`"
end
function remove()
 local t = g[name]
 local query = table.concat(argv, " ", 3)
 if query == "" then return "remove needs a string to match" end
 local pos = nil
 for i, v in ipairs(t) do
  if string.match(v, query) ~= nil then
   pos = i
  end
 end
 if pos == nil then return "remove: no matches for `" .. query .. "`" end
 table.remove(t, pos)
 g[name] = t
 return "removed #" .. pos
end
function clear()
 local t = json.load("[]")
 g[name] = t
 return "reset table " .. name
end
local funcs = {help=help, read=read, add=add, remove=remove}
local func = funcs[op]
if func == nil then return help() end
return func()

Revision: 1620 by @kyork in #marvin-dev 2 years agoSlack Archive

{lua} local name = argv[1]
if name == nil then return "usage: listfactoid {name} [op] [arg] | op can be: read, add {value}, remove {partial}" end
local g = fmap.lists
local op = argv[2]
if op == nil or op == "" then op = "read" end
function help()
 return "to add: _add {string}_ to remove: _remove {partial match}_"
end
function read()
 local t = g[name]
 return table.concat(t, " | ")
end
function add()
 local t = g[name]
 local v = table.concat(argv, " ", 3)
 if v == "" then return "add needs a string to add" end
 table.insert(t, v)
 g[name] = t
 return "added `" .. v .. "`"
end
function remove()
 local t = g[name]
 local query = table.concat(argv, " ", 3)
 if query == "" then return "remove needs a string to match" end
 local pos = nil
 for i, v in ipairs(t) do
  if string.match(v, query) ~= nil then
   pos = i
  end
 end
 if pos == nil then return "remove: no matches for `" .. query .. "`" end
 table.remove(t, pos)
 g[name] = t
 return "removed #" .. pos
end
local funcs = {help=help, read=read, add=add, remove=remove}
local func = funcs[op]
if func == nil then return help() end
return func()