135 lines
3.2 KiB
Nim
135 lines
3.2 KiB
Nim
import base
|
|
import std/[strutils,os,db_sqlite,osproc]
|
|
|
|
const CLIP_DB = WM_TOOLS_DIR & ".clipurr_cache.sqlite"
|
|
const KEEP_ITEMS = 20
|
|
|
|
proc addClip(str: var string)
|
|
|
|
proc killOldRunningProcesses() =
|
|
let x = execCmdEx("killall wl-paste clipnotify")
|
|
echo x
|
|
|
|
|
|
proc runDaemon() =
|
|
echo "Starting Daemon..."
|
|
if wayland:
|
|
echo "Using Wl-paste"
|
|
let cwd = currentSourcePath().parentDir()
|
|
let outp = execProcess("wl-paste -n -w " & cwd & "/clipurr set")
|
|
else:
|
|
var run = true
|
|
while run:
|
|
echo "Using Clipnotify"
|
|
let outp = execCmdEx("clipnotify")
|
|
if outp.exitcode == 0:
|
|
var content = getCurrentClipboardContent()
|
|
addClip(content)
|
|
|
|
echo "Exiting Daemon..."
|
|
|
|
proc openDBConn(): DBConn =
|
|
let db: DBconn = open(CLIP_DB,"","","")
|
|
try:
|
|
db.exec(sql"""create table if not exists
|
|
clip_items (
|
|
timestamp DATETIME NOT NULL,
|
|
clip NVARCHAR(500) NOT NULL
|
|
)
|
|
""")
|
|
except:
|
|
echo getCurrentExceptionMsg()
|
|
return db
|
|
|
|
proc clearHistory() =
|
|
let db = openDBConn()
|
|
defer: db.close()
|
|
try:
|
|
db.exec(sql"drop table if exists clip_items")
|
|
except:
|
|
echo getCurrentExceptionMsg()
|
|
|
|
proc maintainDB() =
|
|
let db = openDBConn()
|
|
try:
|
|
db.exec(sql"""BEGIN""")
|
|
db.exec(sql"delete from clip_items order by timestamp desc offset ?", KEEP_ITEMS)
|
|
db.exec(sql"""COMMIT""")
|
|
except:
|
|
echo "Error cleaning DB : " & getCurrentExceptionMsg()
|
|
|
|
proc escapeClip(str: string): string =
|
|
var clip = str
|
|
clip = clip.replace("`","\\`")
|
|
clip = escape(clip)
|
|
return strip(clip)
|
|
|
|
proc unescapeClip(str: string): string =
|
|
var clip = str
|
|
try:
|
|
clip = unescape(clip)
|
|
except:
|
|
echo getCurrentExceptionMsg()
|
|
|
|
return strip(clip)
|
|
|
|
proc readClipFile(): seq[string] =
|
|
var clips: seq[string] = @[]
|
|
let db = openDBConn()
|
|
defer: db.close()
|
|
try:
|
|
for row in db.fastRows(sql"select distinct(clip) from clip_items order by timestamp desc"):
|
|
var str = unescapeClip(row[0])
|
|
clips.add(str)
|
|
except:
|
|
echo "Error Reading Clip File : " & getCurrentExceptionMsg()
|
|
return clips
|
|
|
|
proc addClip(str: var string) =
|
|
if str == "":
|
|
return
|
|
elif str[0] == '\x89':
|
|
var t = str[1..3]
|
|
echo "Is a ", $t, " file , not storing"
|
|
str = "[" & t & " Image] (not stored)"
|
|
let db = openDBConn()
|
|
defer: db.close()
|
|
try:
|
|
db.exec(sql"""BEGIN""")
|
|
db.exec(sql"""insert into clip_items (timestamp, clip)
|
|
values (CURRENT_TIMESTAMP, ?)
|
|
""", escapeClip(str))
|
|
db.exec(sql"""COMMIT""")
|
|
except:
|
|
echo getCurrentExceptionMsg()
|
|
return
|
|
|
|
proc showClips() =
|
|
let clips = readClipFile()
|
|
let info = newInfo("Clipurr")
|
|
let option = outputData(info, clips)
|
|
if option != "":
|
|
copyToClipboard(option)
|
|
return
|
|
|
|
proc main() =
|
|
for idx, arg in args:
|
|
if arg == "daemon":
|
|
killOldRunningProcesses()
|
|
runDaemon()
|
|
return
|
|
if arg == "set":
|
|
var content = getCurrentClipboardContent()
|
|
addClip(content)
|
|
return
|
|
if arg == "clear":
|
|
clearHistory()
|
|
return
|
|
showClips()
|
|
return
|
|
|
|
block start:
|
|
if isMainModule:
|
|
main()
|
|
|
|
maintainDB()
|