-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfzf_rename_from_file.py
executable file
·53 lines (45 loc) · 1.47 KB
/
fzf_rename_from_file.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
#!/usr/bin/env python3
import argparse
import pathlib
import subprocess
def fzf(data, header=None, query=None):
args = [
'fzf', '--black', '--prompt=', '--no-info', '--exact', '--no-sort',
'--no-extended', '-i', '--layout=reverse', '--print-query',
]
if header is not None:
data = [header, *data]
args.append('--header-lines=1')
if query is not None:
args.append(f'--query={query}')
proc = subprocess.run(
args,
input='\n'.join(data).encode(),
stdout=subprocess.PIPE,
)
try:
result = proc.stdout.decode().splitlines()[-1]
except IndexError:
result = None
return result
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--titles', type=pathlib.Path)
parser.add_argument('path', type=pathlib.Path)
args = parser.parse_args()
titles = args.titles.read_text().splitlines()
for path in args.path.iterdir():
if path.is_dir():
raise NotImplementedError
if path.is_file() and path.suffix == '.mkv':
result = fzf(titles, header=path.name, query=path.name.split()[0])
if result is not None:
newpath = path.with_name(result + '.mkv')
if path == newpath:
continue
if newpath.exists():
raise RuntimeError
print(newpath)
path.rename(newpath)
if __name__ == '__main__':
main()