forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakecase.py
executable file
·48 lines (39 loc) · 1.13 KB
/
snakecase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Snake Case
# @raycast.mode inline
# @raycast.packageName Change Case
# Optional parameters:
# @raycast.icon ./images/snakecase-light.png
# @raycast.iconDark ./images/snakecase-dark.png
# Documentation:
# @raycast.author Robert Cooper
# @raycast.authorURL https://github.com/robertcoopercode
# @raycast.description Change to clipboard text to snake case
import subprocess
import re
def getClipboardData():
p = subprocess.Popen(["pbpaste"], stdout=subprocess.PIPE)
data = p.stdout.read()
return tryDecode(data)
def setClipboardData(data):
p = subprocess.Popen(["pbcopy"], stdin=subprocess.PIPE)
p.stdin.write(tryEncode(data))
p.stdin.close()
def tryDecode(s):
try:
return s.decode('utf-8')
except:
return s
def tryEncode(s):
try:
return s.encode('utf-8')
except:
return s
clipboard = str(getClipboardData())
result = re.sub(r"([a-z])([A-Z])", r"\1_\2", clipboard)
result = result.lower().replace(" ", "_").replace("-", "_")
setClipboardData(result)
print(result)