aboutsummaryrefslogtreecommitdiffstats
path: root/bin/clipd
blob: 22d250249f075c6b5ddc92ee007d1a64772a1598 (plain) (blame)
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
90
91
92
93
94
95
#!/usr/bin/env lua5.3

-- needs xsel, clipnotify
-- luarocks-5.3 install --local luaposix
-- luarocks-5.3 install --local argparse
-- cat .clip_history | dmenu -l 10 | xsel -ib
local function default_luarocks_modules()
    local luarocks_handle = io.popen("luarocks-5.3 path --bin")
    local path_b = false
    local cpath_b = false
    for line in luarocks_handle:lines() do
        local path = string.match(line, "LUA_PATH%s*=%s*('.+')")
        local cpath = string.match(line, "LUA_CPATH%s*=%s*('.+')")
        if path ~= nil then
            package.path = package.path .. ";" .. string.sub(path, 2, -2)
        end
        if cpath ~= nil then
            package.cpath = package.cpath .. ";" .. string.sub(cpath, 2, -2)
        end
    end

    if path_b then os.exit(1) end
    if cpath_b then os.exit(1) end
end
default_luarocks_modules()

local string = require("string")
local signal = require("posix.signal")
local argparse = require("argparse")

signal.signal(signal.SIGINT, function(signum) os.exit(128 + signum) end)

local function sleep(n) os.execute("sleep " .. tonumber(n)) end
local function file_exists(path)
    local f = io.popen(path, "r")
    return f ~= nil and io.close(f)
end
local function trim(s) return s:gsub("^%s+", ""):gsub("%s+$", "") end

local clip_hist_size = 200
local clip_hist = "/home/devi/.clip_history"

local parser = argparse("", "")
parser:option("-s --hist_size", "history file size", 200)
parser:option("-f --hist_file", "history file location",
              "/home/devi/.clip_history")

local function loop()
    local clips_table = {}
    local hist_current_count = 0

    local hist_file = io.open(clip_hist, "r")
    for line in hist_file:lines() do
        if line ~= "\n" and line ~= "" and line ~= "\r\n" and line ~= " " then
            clips_table[line] = true
            hist_current_count = hist_current_count + 1
        end
    end
    hist_file:close()

    while true do
        local wait_for_event = io.popen("clipnotify")
        local handle = io.popen("xsel -ob")
        local last_clip_entry = handle:read("*a")

        if last_clip_entry[-1] == "\n" then
            clips_table[string.sub(last_clip_entry, 0,
                                   string.len(last_clip_entry))] = true
        else
            clips_table[last_clip_entry] = true;
        end
        hist_current_count = hist_current_count + 1

        if hist_current_count >= clip_hist_size then
            table.remove(clips_table, 1)
            hist_current_count = hist_current_count - 1
        end

        hist_file = io.open(clip_hist, "w")
        for k, _ in pairs(clips_table) do
            if clips_table[k] then hist_file:write(trim(k) .. "\n") end
        end
        hist_file:close()

        wait_for_event:close()
        handle:close()
        sleep(.2)
    end
end

-- if file_exists(clip_hist) ~= true then
--     local dummy_handle = io.popen(clip_hist, "a")
--     if dummy_handle ~= nil then dummy_handle:close() end
-- end
loop()