Create common.serialize function
This commit is contained in:
parent
b0686bbe87
commit
0e263af3c6
|
@ -164,6 +164,20 @@ function common.bench(name, fn, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function common.serialize(val)
|
||||||
|
if type(val) == "string" then
|
||||||
|
return string.format("%q", val)
|
||||||
|
elseif type(val) == "table" then
|
||||||
|
local t = {}
|
||||||
|
for k, v in pairs(val) do
|
||||||
|
table.insert(t, "[" .. common.serialize(k) .. "]=" .. common.serialize(v))
|
||||||
|
end
|
||||||
|
return "{" .. table.concat(t, ",") .. "}"
|
||||||
|
end
|
||||||
|
return tostring(val)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function common.home_encode(text)
|
function common.home_encode(text)
|
||||||
if HOME then
|
if HOME then
|
||||||
local n = #HOME
|
local n = #HOME
|
||||||
|
|
|
@ -11,15 +11,6 @@ local Doc
|
||||||
|
|
||||||
local core = {}
|
local core = {}
|
||||||
|
|
||||||
local function table_serialize(t)
|
|
||||||
local ls = {"{"}
|
|
||||||
for i = 1, #t do
|
|
||||||
ls[#ls + 1] = string.format(" %q,", t[i])
|
|
||||||
end
|
|
||||||
ls[#ls + 1] = "}"
|
|
||||||
return table.concat(ls, "\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function load_projects()
|
local function load_projects()
|
||||||
local ok, t = pcall(dofile, USERDIR .. "/recent_projects.lua")
|
local ok, t = pcall(dofile, USERDIR .. "/recent_projects.lua")
|
||||||
core.recent_projects = (ok and t or {})
|
core.recent_projects = (ok and t or {})
|
||||||
|
@ -42,7 +33,7 @@ end
|
||||||
local function save_projects()
|
local function save_projects()
|
||||||
local fp = io.open(USERDIR .. "/recent_projects.lua", "w")
|
local fp = io.open(USERDIR .. "/recent_projects.lua", "w")
|
||||||
if fp then
|
if fp then
|
||||||
fp:write("return ", table_serialize(core.recent_projects), "\n")
|
fp:write("return ", common.serialize(core.recent_projects), "\n")
|
||||||
fp:close()
|
fp:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,23 +1,10 @@
|
||||||
local core = require "core"
|
local core = require "core"
|
||||||
|
local common = require "core.common"
|
||||||
local DocView = require "core.docview"
|
local DocView = require "core.docview"
|
||||||
|
|
||||||
local workspace_filename = ".lite_workspace.lua"
|
local workspace_filename = ".lite_workspace.lua"
|
||||||
|
|
||||||
|
|
||||||
local function serialize(val)
|
|
||||||
if type(val) == "string" then
|
|
||||||
return string.format("%q", val)
|
|
||||||
elseif type(val) == "table" then
|
|
||||||
local t = {}
|
|
||||||
for k, v in pairs(val) do
|
|
||||||
table.insert(t, "[" .. serialize(k) .. "]=" .. serialize(v))
|
|
||||||
end
|
|
||||||
return "{" .. table.concat(t, ",") .. "}"
|
|
||||||
end
|
|
||||||
return tostring(val)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function has_no_locked_children(node)
|
local function has_no_locked_children(node)
|
||||||
if node.locked then return false end
|
if node.locked then return false end
|
||||||
if node.type == "leaf" then return true end
|
if node.type == "leaf" then return true end
|
||||||
|
@ -164,8 +151,8 @@ local function save_workspace()
|
||||||
local root = get_unlocked_root(core.root_view.root_node)
|
local root = get_unlocked_root(core.root_view.root_node)
|
||||||
local fp = io.open(workspace_filename, "w")
|
local fp = io.open(workspace_filename, "w")
|
||||||
if fp then
|
if fp then
|
||||||
local node_text = serialize(save_node(root))
|
local node_text = common.serialize(save_node(root))
|
||||||
local dir_text = serialize(save_directories())
|
local dir_text = common.serialize(save_directories())
|
||||||
fp:write(string.format("return { documents = %s, directories = %s }\n", node_text, dir_text))
|
fp:write(string.format("return { documents = %s, directories = %s }\n", node_text, dir_text))
|
||||||
fp:close()
|
fp:close()
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue