File: fuzzy.py

package info (click to toggle)
python-inquirerpy 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: python: 9,463; makefile: 15
file content (58 lines) | stat: -rw-r--r-- 1,537 bytes parent folder | download
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
import urllib.request
from contextlib import ExitStack
from pathlib import Path

from InquirerPy import prompt


def get_choices(_):
    p = Path(__file__).resolve().parent.joinpath("sample.txt")
    choices = []
    result = []

    with ExitStack() as stack:
        if not p.exists():
            file = stack.enter_context(p.open("w+"))
            sample = stack.enter_context(
                urllib.request.urlopen(
                    "https://assets.kazhala.me/InquirerPy/sample.txt"
                )
            )
            file.write(sample.read().decode())
            file.seek(0, 0)
        else:
            file = stack.enter_context(p.open("r"))
        for line in file.readlines():
            choices.append(line[:-1])
        for choice in choices:
            if not choice:
                continue
            result.append(choice)
    return result


def main():
    questions = [
        {
            "type": "fuzzy",
            "message": "Select actions:",
            "choices": ["hello", "weather", "what", "whoa", "hey", "yo"],
            "default": "he",
            "max_height": "70%",
        },
        {
            "type": "fuzzy",
            "message": "Select preferred words:",
            "choices": get_choices,
            "multiselect": True,
            "validate": lambda result: len(result) > 1,
            "invalid_message": "minimum 2 selection",
            "max_height": "70%",
        },
    ]

    result = prompt(questions=questions)


if __name__ == "__main__":
    main()