added clip_wl - clipboard manager for wayland
This commit is contained in:
parent
8875f99279
commit
a745fb0785
2 changed files with 59 additions and 24 deletions
16
base.nim
16
base.nim
|
@ -224,6 +224,22 @@ proc copyToClipboard*(str: string) =
|
|||
else:
|
||||
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.} =
|
||||
var output = ""
|
||||
if tool == "rofi":
|
||||
|
|
|
@ -1,9 +1,33 @@
|
|||
import base
|
||||
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
|
||||
|
||||
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:
|
||||
|
@ -25,16 +49,6 @@ proc clearHistory() =
|
|||
except:
|
||||
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() =
|
||||
let db = openDBConn()
|
||||
try:
|
||||
|
@ -42,13 +56,13 @@ proc maintainDB() =
|
|||
db.exec(sql"delete from clip_items order by timestamp desc offset ?", KEEP_ITEMS)
|
||||
db.exec(sql"""COMMIT""")
|
||||
except:
|
||||
echo getCurrentExceptionMsg()
|
||||
echo "Error cleaning DB : " & getCurrentExceptionMsg()
|
||||
|
||||
proc escapeClip(str: string): string =
|
||||
var clip = str
|
||||
clip = clip.replace("`","\\`")
|
||||
clip = escape(clip)
|
||||
return clip
|
||||
return strip(clip)
|
||||
|
||||
proc unescapeClip(str: string): string =
|
||||
var clip = str
|
||||
|
@ -57,23 +71,27 @@ proc unescapeClip(str: string): string =
|
|||
except:
|
||||
echo getCurrentExceptionMsg()
|
||||
|
||||
return clip
|
||||
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 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])
|
||||
clips.add(str)
|
||||
except:
|
||||
echo getCurrentExceptionMsg()
|
||||
echo "Error Reading Clip File : " & getCurrentExceptionMsg()
|
||||
return clips
|
||||
|
||||
proc addClip(str: string) =
|
||||
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:
|
||||
|
@ -88,7 +106,7 @@ proc addClip(str: string) =
|
|||
|
||||
proc showClips() =
|
||||
let clips = readClipFile()
|
||||
let info = newInfo("Clips")
|
||||
let info = newInfo("Clipurr")
|
||||
let option = outputData(info, clips)
|
||||
if option != "":
|
||||
copyToClipboard(option)
|
||||
|
@ -96,8 +114,13 @@ proc showClips() =
|
|||
|
||||
proc main() =
|
||||
for idx, arg in args:
|
||||
if arg == "daemon":
|
||||
killOldRunningProcesses()
|
||||
runDaemon()
|
||||
return
|
||||
if arg == "set":
|
||||
addClip(getCurrentClipboardContent())
|
||||
var content = getCurrentClipboardContent()
|
||||
addClip(content)
|
||||
return
|
||||
if arg == "clear":
|
||||
clearHistory()
|
||||
|
@ -107,10 +130,6 @@ proc main() =
|
|||
|
||||
block start:
|
||||
if isMainModule:
|
||||
if not wayland:
|
||||
echo "Not a wayland session, exiting..."
|
||||
break start
|
||||
else:
|
||||
main()
|
||||
main()
|
||||
|
||||
maintainDB()
|
Loading…
Reference in a new issue