This repository was archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfzf_installer_helpers.py
More file actions
64 lines (51 loc) · 1.51 KB
/
fzf_installer_helpers.py
File metadata and controls
64 lines (51 loc) · 1.51 KB
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
import yaml, json
def get_config():
with open('./config.yaml', 'r') as f:
try:
return yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
import sys
sys.exit(1)
import subprocess
def run_cmds(array):
return subprocess.run('\n'.join(array),shell=True, check=True,executable='/bin/bash')
from subprocess import Popen, PIPE
from collections import namedtuple
from typing import Iterable, Optional, List
import sys
Result = namedtuple("Result", ["index", "output"])
ENC = sys.getdefaultencoding()
def fzf(
iterable: Iterable,
options: Optional[str] = None,
) -> List[Result]:
cmd = ["fzf", "--with-nth=2.."]
if options:
cmd += options.split(" ")
proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=None)
stdin = proc.stdin
stdout = proc.stdout
for i, el in enumerate(iterable):
line = f"{i} {el}"
stdin.write(line.encode(ENC) + b"\n")
stdin.flush()
stdin.close()
proc.wait()
results = []
for ln in stdout.readlines():
sel = str(ln.strip(), encoding=ENC).split(" ", 1)
results.append(Result(int(sel[0]), sel[1]))
return results
def select_packages(packages_raw):
packages_selected_names = list(map(
lambda n: n.output,
fzf(
map(
lambda p: p["name"],
packages_raw
),
'--multi'
)
))
return list(filter(lambda p: p["name"] in packages_selected_names, packages_raw))