2022-07-16 15:20:55 +02:00
|
|
|
import base
|
|
|
|
import std/[strutils,os,db_sqlite,osproc]
|
|
|
|
|
2022-07-16 19:17:49 +02:00
|
|
|
const CLIP_DB = WM_TOOLS_DIR & ".clipurr_cache.sqlite"
|
2022-07-16 15:20:55 +02:00
|
|
|
const KEEP_ITEMS = 20
|
|
|
|
|
2022-07-16 19:17:49 +02:00
|
|
|
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..."
|
|
|
|
|
2022-07-16 15:20:55 +02:00
|
|
|
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:
|
2022-07-16 19:17:49 +02:00
|
|
|
echo "Error cleaning DB : " & getCurrentExceptionMsg()
|
2022-07-16 15:20:55 +02:00
|
|
|
|
|
|
|
proc escapeClip(str: string): string =
|
|
|
|
var clip = str
|
|
|
|
clip = clip.replace("`","\\`")
|
|
|
|
clip = escape(clip)
|
2022-07-16 19:17:49 +02:00
|
|
|
return strip(clip)
|
2022-07-16 15:20:55 +02:00
|
|
|
|
|
|
|
proc unescapeClip(str: string): string =
|
|
|
|
var clip = str
|
|
|
|
try:
|
|
|
|
clip = unescape(clip)
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
|
2022-07-16 19:17:49 +02:00
|
|
|
return strip(clip)
|
2022-07-16 15:20:55 +02:00
|
|
|
|
|
|
|
proc readClipFile(): seq[string] =
|
|
|
|
var clips: seq[string] = @[]
|
|
|
|
let db = openDBConn()
|
|
|
|
defer: db.close()
|
|
|
|
try:
|
2022-07-16 19:17:49 +02:00
|
|
|
for row in db.fastRows(sql"select distinct(clip) from clip_items order by timestamp desc"):
|
2022-07-16 15:20:55 +02:00
|
|
|
var str = unescapeClip(row[0])
|
|
|
|
clips.add(str)
|
|
|
|
except:
|
2022-07-16 19:17:49 +02:00
|
|
|
echo "Error Reading Clip File : " & getCurrentExceptionMsg()
|
2022-07-16 15:20:55 +02:00
|
|
|
return clips
|
|
|
|
|
2022-07-16 19:17:49 +02:00
|
|
|
proc addClip(str: var string) =
|
2022-07-16 15:20:55 +02:00
|
|
|
if str == "":
|
|
|
|
return
|
2022-07-16 19:17:49 +02:00
|
|
|
elif str[0] == '\x89':
|
|
|
|
var t = str[1..3]
|
|
|
|
echo "Is a ", $t, " file , not storing"
|
|
|
|
str = "[" & t & " Image] (not stored)"
|
2022-07-16 15:20:55 +02:00
|
|
|
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()
|
2022-07-16 19:17:49 +02:00
|
|
|
let info = newInfo("Clipurr")
|
2022-07-16 15:20:55 +02:00
|
|
|
let option = outputData(info, clips)
|
|
|
|
if option != "":
|
|
|
|
copyToClipboard(option)
|
|
|
|
return
|
|
|
|
|
|
|
|
proc main() =
|
|
|
|
for idx, arg in args:
|
2022-07-16 19:17:49 +02:00
|
|
|
if arg == "daemon":
|
|
|
|
killOldRunningProcesses()
|
|
|
|
runDaemon()
|
|
|
|
return
|
2022-07-16 15:20:55 +02:00
|
|
|
if arg == "set":
|
2022-07-16 19:17:49 +02:00
|
|
|
var content = getCurrentClipboardContent()
|
|
|
|
addClip(content)
|
2022-07-16 15:20:55 +02:00
|
|
|
return
|
|
|
|
if arg == "clear":
|
|
|
|
clearHistory()
|
|
|
|
return
|
|
|
|
showClips()
|
|
|
|
return
|
|
|
|
|
|
|
|
block start:
|
|
|
|
if isMainModule:
|
2022-07-16 19:17:49 +02:00
|
|
|
main()
|
2022-07-16 15:20:55 +02:00
|
|
|
|
|
|
|
maintainDB()
|