-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrakto_kitty.py
executable file
·177 lines (122 loc) · 4.44 KB
/
extrakto_kitty.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
import sys
import re
import subprocess
from argparse import ArgumentParser
from collections import OrderedDict
RE_PATH = (
r'(?=[ \t\n]|"|\(|\[|<|\')?'
'(~/|/)?'
'([-a-zA-Z0-9_+-,.]+/[^ \t\n\r|:"\'$%&)>\]]*)'
)
RE_URL = (r"(https?://|git@|git://|ssh://|s*ftp://|file:///)"
"[a-zA-Z0-9?=%/_.:,;~@!#$&()*+-]*")
RE_URL_OR_PATH = RE_PATH + "|" + RE_URL
# "words" consist of anything but the following characters:
# [](){}
# unicode range 2500-27BF which includes:
# - Box Drawing
# - Block Elements
# - Geometric Shapes
# - Miscellaneous Symbols
# - Dingbats
# unicode range E000-F8FF (private use/Powerline)
# and whitespace ( \t\n\r)
RE_WORD = u'[^][(){}\u2500-\u27BF\uE000-\uF8FF \\t\\n\\r]+'
# reg exp to exclude transfer speeds like 5k/s or m/s, and page 1/2
RE_SPEED = r'[kmgKMG]/s$|^\d+/\d+$'
def get_args(arguments):
parser = ArgumentParser(description='Extracts tokens from plaintext.')
parser.add_argument('-p', '--paths', action='store_true',
help='extract path tokens')
parser.add_argument('-u', '--urls', action='store_true',
help='extract url tokens')
parser.add_argument('-w', '--words', action='store_true',
help='extract "word" tokens')
parser.add_argument('-l', '--lines', action='store_true',
help='extract lines')
parser.add_argument('-r', '--reverse', action='store_true',
help='reverse output')
parser.add_argument('-m', '--min-length', default=5,
help='minimum token length')
args = parser.parse_args(arguments)
return args
def process_urls_and_paths(find, text, min_length):
res = list()
for m in re.finditer(find, "\n" + text, flags=re.I):
item = m.group()
# remove invalid end characters (like punctuation
# or markdown syntax)
if item[-1] in ",):":
item = item[:-1]
# exclude transfer speeds like 5k/s or m/s, and page 1/2
if not re.search(RE_SPEED, item, re.I):
if len(item) >= min_length:
res.append(item)
return res
def get_urls(text, min_length=0):
return process_urls_and_paths(RE_URL, text, min_length)
def get_paths(text, min_length=0):
return process_urls_and_paths(RE_PATH, text, min_length)
def get_urls_or_paths(text, min_length=0):
return process_urls_and_paths(RE_URL_OR_PATH, text, min_length)
def get_words(text, min_length):
words = []
for m in re.finditer(RE_WORD, text):
item = m.group().strip(',:;()[]{}<>\'"|').rstrip('.')
if len(item) >= min_length:
words.append(item)
return words
def get_lines(text, min_length):
lines = []
for raw_line in text.splitlines():
line = raw_line.strip()
if len(line) >= min_length:
lines.append(line)
return lines
def main(arguments):
def get_input():
return sys.stdin.read()
def print_result(t):
print(t)
def print_result_py2(t):
print(t.encode('utf-8'))
if (sys.version_info < (3, 0)):
import codecs
sys.stdin = codecs.getreader('utf8')(sys.stdin)
print_result = print_result_py2
args = get_args(arguments[1:])
if args.words:
res = get_words(get_input(), args.min_length)
elif args.paths:
if args.urls:
res = get_urls_or_paths(get_input(), args.min_length)
else:
res = get_paths(get_input(), args.min_length)
elif args.urls:
res = get_urls(get_input(), args.min_length)
elif args.lines:
res = get_lines(get_input(), args.min_length)
else:
print('unknown option, see --help')
sys.exit(1)
if args.reverse:
res.reverse()
# remove duplicates and pass to fzf
results = OrderedDict.fromkeys(res)
proc = subprocess.Popen(["fzf"], universal_newlines=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdin = proc.stdin
stdin.write("\n".join(results))
stdin.flush()
stdout = proc.stdout
values = [l.strip('\r\n') for l in iter(stdout.readline, '')]
return " ".join(values)
def handle_result(args, answer, target_window_id, boss):
# get the kitty window into which to paste the answer
w = boss.window_id_map.get(target_window_id)
if w is not None:
w.paste(answer)
if __name__ == "__main__":
main(sys.argv)
handle_result.type_of_input = 'history'