Skip to content

Instantly share code, notes, and snippets.

@willwade
Last active May 25, 2022 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willwade/3b4df68cffb1db895c8a3c208dd931e9 to your computer and use it in GitHub Desktop.
Save willwade/3b4df68cffb1db895c8a3c208dd931e9 to your computer and use it in GitHub Desktop.
pasteboard buffer. Two commands. Set one to listen - and then you can run it again with rewrite and it will rewrite your copy history to -1
import pyclip
import typer
import time
from typing import Optional
import sqlite3
conn = sqlite3.connect('copyhistory.sqlite')
global max_history
max_history = 5
now_list = ""
def setup_db():
conn = sqlite3.connect('copyhistory.sqlite')
cur = conn.cursor()
cur.execute('''CREATE TABLE "history" ("copy_string" TEXT, "id" INTEGER UNIQUE);''')
cur.execute('CREATE TABLE "last_id" ("ID" INTEGER);')
cur.execute('INSERT INTO last_id (ID) VALUES (1);')
for x in range(1,max_history+1):
cur.execute("INSERT INTO history (ID,copy_string) VALUES ("+str(x)+", 'Empty' )");
conn.commit()
conn.close()
def add_history(copyBuffer: str = ""):
conn = sqlite3.connect('copyhistory.sqlite')
# Get last id
cur = conn.cursor()
cur.execute("select ID from last_id")
row = cur.fetchone()
last_id = int(row[0])+1
# Reset-
if (last_id==max_history):
last_id = 1
cur.execute("UPDATE history set copy_string = :copyBuffer where ID = :histID",{"copyBuffer": copyBuffer, "histID":last_id })
cur.execute("UPDATE last_id set ID=:histID",{'histID':last_id} )
conn.commit()
conn.close()
def get_history(history: int = 0):
# Get last id
cur = conn.cursor()
cur.execute("select ID from last_id")
row = cur.fetchone()
last_id = int(row[0])
#This is going to mess up byt cant figure out logic
if (last_id==1 and history !=0):
# End of list
last_id = max_history-history
else:
last_id = last_id - history
# Get string with that ID
cur.execute("SELECT copy_string from history where ID = "+str(last_id))
row = cur.fetchone()
cur.close()
return row[0]
def check():
last_item = get_history()
now_item = pyclip.paste(text=True)
if now_item != last_item:
add_history(now_item)
return True
app = typer.Typer()
@app.command()
def setup():
"""
Never used this before? Run this FIRST. It will setup the db. NB: No checking that youve done this.
"""
setup_db()
@app.command()
def listen():
"""
Listen for every 1 second for the pasteboard. Store the current pasteboard (text only) and just keep a history for the last max_hostory times
"""
while True:
check()
time.sleep(1)
@app.command()
def rewrite(history: int = 1):
"""
Return a copy buffer with something from history. Provide an int that is the history minus that number,
"""
typer.echo(f"Changing pasteboard to history -"+str(history))
copyString = get_history(history)
pyclip.copy(copyString)
if __name__ == "__main__":
app()
add_history(pyclip.paste(text=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment