File: session.lua

package info (click to toggle)
luakit 2012.09.13-r1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,160 kB
  • ctags: 1,276
  • sloc: ansic: 6,086; makefile: 153; ruby: 79; sh: 38
file content (89 lines) | stat: -rw-r--r-- 2,645 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
------------------------------------------------------
-- Session saving / loading functions               --
-- © 2010 Mason Larobina <mason.larobina@gmail.com> --
------------------------------------------------------

local function rm(file)
    luakit.spawn(string.format("rm %q", file))
end

-- Session functions
session = {
    -- The file which we'll use for session info, $XDG_DATA_HOME/luakit/session
    file = luakit.data_dir .. "/session",

    -- Save all given windows uris to file.
    save = function (wins)
        local lines = {}
        -- Save tabs from all the given windows
        for wi, w in pairs(wins) do
            local current = w.tabs:current()
            for ti, tab in ipairs(w.tabs.children) do
                table.insert(lines, string.format("%d\t%d\t%s\t%s", wi, ti,
                    tostring(current == ti), tab.uri))
            end
        end

        if #lines > 0 then
            local fh = io.open(session.file, "w")
            fh:write(table.concat(lines, "\n"))
            io.close(fh)
        else
            rm(session.file)
        end
    end,

    -- Load window and tab state from file
    load = function (delete)
        if not os.exists(session.file) then return end
        local ret = {}

        -- Read file
        local lines = {}
        local fh = io.open(session.file, "r")
        for line in fh:lines() do table.insert(lines, line) end
        io.close(fh)
        -- Delete file
        if delete ~= false then rm(session.file) end

        -- Parse session file
        local split = lousy.util.string.split
        for _, line in ipairs(lines) do
            local wi, ti, current, uri = unpack(split(line, "\t"))
            wi = tonumber(wi)
            current = (current == "true")
            if not ret[wi] then ret[wi] = {} end
            table.insert(ret[wi], {uri = uri, current = current})
        end

        return (#ret > 0 and ret) or nil
    end,

    -- Spawn windows from saved session and return the last window
    restore = function (delete)
        wins = session.load(delete)
        if not wins or #wins == 0 then return end

        -- Spawn windows
        local w
        for _, win in ipairs(wins) do
            w = nil
            for _, item in ipairs(win) do
                if not w then
                    w = window.new({item.uri})
                else
                    w:new_tab(item.uri, item.current)
                end
            end
        end

        return w
    end,
}

-- Save current window session helper
window.methods.save_session = function (w)
    session.save({w,})
end

-- vim: et:sw=4:ts=8:sts=4:tw=80