added clip_wl - clipboard manager for wayland

This commit is contained in:
Paul Wilde 2022-07-16 18:17:49 +01:00
parent 8875f99279
commit a745fb0785
2 changed files with 59 additions and 24 deletions

View file

@ -224,6 +224,22 @@ proc copyToClipboard*(str: string) =
else: else:
discard execCmd("echo -n " & quote(str) & " | xclip -selection clipboard") discard execCmd("echo -n " & quote(str) & " | xclip -selection clipboard")
proc getCurrentClipboardContent*(): string =
var str = ""
if wayland:
let cur = execCmdEx("wl-paste")
if cur.exitcode == 0:
str = cur[0]
else:
echo cur
else:
let cur = execCmdEx("xsel -o -b")
if cur.exitcode == 0:
str = cur[0]
else:
echo cur
return strip(str)
proc outputData*(data: Info, args: varargs[string]): string {.discardable.} = proc outputData*(data: Info, args: varargs[string]): string {.discardable.} =
var output = "" var output = ""
if tool == "rofi": if tool == "rofi":

View file

@ -1,9 +1,33 @@
import base import base
import std/[strutils,os,db_sqlite,osproc] import std/[strutils,os,db_sqlite,osproc]
const CLIP_DB = WM_TOOLS_DIR & ".clipboard_cache.sqlite" const CLIP_DB = WM_TOOLS_DIR & ".clipurr_cache.sqlite"
const KEEP_ITEMS = 20 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 = proc openDBConn(): DBConn =
let db: DBconn = open(CLIP_DB,"","","") let db: DBconn = open(CLIP_DB,"","","")
try: try:
@ -25,16 +49,6 @@ proc clearHistory() =
except: except:
echo getCurrentExceptionMsg() echo getCurrentExceptionMsg()
proc getCurrentClipboardContent(): string =
var str = ""
if wayland:
let cur = execCmdEx("wl-paste")
if cur.exitcode == 0:
str = cur[0]
else:
echo cur
return str
proc maintainDB() = proc maintainDB() =
let db = openDBConn() let db = openDBConn()
try: try:
@ -42,13 +56,13 @@ proc maintainDB() =
db.exec(sql"delete from clip_items order by timestamp desc offset ?", KEEP_ITEMS) db.exec(sql"delete from clip_items order by timestamp desc offset ?", KEEP_ITEMS)
db.exec(sql"""COMMIT""") db.exec(sql"""COMMIT""")
except: except:
echo getCurrentExceptionMsg() echo "Error cleaning DB : " & getCurrentExceptionMsg()
proc escapeClip(str: string): string = proc escapeClip(str: string): string =
var clip = str var clip = str
clip = clip.replace("`","\\`") clip = clip.replace("`","\\`")
clip = escape(clip) clip = escape(clip)
return clip return strip(clip)
proc unescapeClip(str: string): string = proc unescapeClip(str: string): string =
var clip = str var clip = str
@ -57,23 +71,27 @@ proc unescapeClip(str: string): string =
except: except:
echo getCurrentExceptionMsg() echo getCurrentExceptionMsg()
return clip return strip(clip)
proc readClipFile(): seq[string] = proc readClipFile(): seq[string] =
var clips: seq[string] = @[] var clips: seq[string] = @[]
let db = openDBConn() let db = openDBConn()
defer: db.close() defer: db.close()
try: try:
for row in db.fastRows(sql"select clip from clip_items order by timestamp desc"): for row in db.fastRows(sql"select distinct(clip) from clip_items order by timestamp desc"):
var str = unescapeClip(row[0]) var str = unescapeClip(row[0])
clips.add(str) clips.add(str)
except: except:
echo getCurrentExceptionMsg() echo "Error Reading Clip File : " & getCurrentExceptionMsg()
return clips return clips
proc addClip(str: string) = proc addClip(str: var string) =
if str == "": if str == "":
return 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() let db = openDBConn()
defer: db.close() defer: db.close()
try: try:
@ -88,7 +106,7 @@ proc addClip(str: string) =
proc showClips() = proc showClips() =
let clips = readClipFile() let clips = readClipFile()
let info = newInfo("Clips") let info = newInfo("Clipurr")
let option = outputData(info, clips) let option = outputData(info, clips)
if option != "": if option != "":
copyToClipboard(option) copyToClipboard(option)
@ -96,8 +114,13 @@ proc showClips() =
proc main() = proc main() =
for idx, arg in args: for idx, arg in args:
if arg == "daemon":
killOldRunningProcesses()
runDaemon()
return
if arg == "set": if arg == "set":
addClip(getCurrentClipboardContent()) var content = getCurrentClipboardContent()
addClip(content)
return return
if arg == "clear": if arg == "clear":
clearHistory() clearHistory()
@ -107,10 +130,6 @@ proc main() =
block start: block start:
if isMainModule: if isMainModule:
if not wayland:
echo "Not a wayland session, exiting..."
break start
else:
main() main()
maintainDB() maintainDB()