Created
June 17, 2022 05:23
-
-
Save akdor1154/85dc952b133450b9a2efe1fa305eeb76 to your computer and use it in GitHub Desktop.
clipfix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| from pprint import pprint | |
| from subprocess import run, PIPE | |
| import csv | |
| class Clip: | |
| def getText(self) -> str: ... | |
| def hasExistingHtml(self) -> bool: ... | |
| def setHtml(self, v: str): ... | |
| class ClipX(Clip): | |
| def getText(self): | |
| textResult = run([ | |
| 'xclip', '-sel', 'clipboard', '-out' | |
| ], stdout=PIPE, check=True, encoding='utf-8') | |
| return textResult.stdout | |
| def hasExistingHtml(self): | |
| htmlResult = run([ | |
| 'xclip', '-sel', 'clipboard', '-out', '-target', 'text/html' | |
| ], stdout=PIPE, stderr=PIPE, check=False, encoding='utf-8') | |
| return (htmlResult.returncode == 0) | |
| def setHtml(self, v: str): | |
| run([ | |
| 'xclip', '-sel', 'clipboard', '-in', '-target', 'text/html' | |
| ], input=v, check=True, encoding='utf-8') | |
| class ClipWay(Clip): | |
| def getText(self): | |
| return run([ | |
| 'wl-paste', '-t', 'text/plain' | |
| ], stdout=PIPE, stderr=PIPE, check=True, encoding='utf-8').stdout | |
| def hasExistingHtml(self): | |
| types = run(['wl-paste', '-l'], stdout=PIPE, stderr=PIPE, check=True, encoding='utf-8').stdout.splitlines() | |
| return ('text/html' in types) | |
| def setHtml(self, v: str): | |
| run([ | |
| 'wl-copy', '-t', 'text/html' | |
| ], input=v, check=True, encoding='utf-8') | |
| cl = ClipWay() | |
| text = cl.getText() | |
| if cl.hasExistingHtml(): | |
| #there is an existing html selection | |
| print('Not touching existing html selection') | |
| #exit(1) | |
| def c(ss): | |
| return ''.join(ss) | |
| tsvReader = csv.reader(text.splitlines(), dialect='excel-tab') | |
| textAsHtml = c([ | |
| '<table>', | |
| *[f'<tr>{c(f"<td>{cell}</td>" for cell in row)}</tr>' for row in tsvReader], | |
| '</table>' | |
| ]) | |
| fixed = f'<meta http-equiv="content-type" content="text/html; charset=utf-8">{textAsHtml}' | |
| print(fixed) | |
| cl.setHtml(fixed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment