aboutsummaryrefslogtreecommitdiffstats
path: root/examples/simple_library_example.py
blob: 5a6a6a37fbffef3aae06aadf8c7b0a0aa2757f69 (plain) (blame)
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
#!/usr/bin/env python3
"""
Simple example showing how to use selectui as a library.
"""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from selectui import SelectUI


def select_programming_language():
    """Let user select a programming language."""

    languages = [
        {
            "title": "Python",
            "subtitle": "High-level, interpreted language",
            "extension": ".py",
            "year": "1991"
        },
        {
            "title": "JavaScript",
            "subtitle": "Dynamic web programming language",
            "extension": ".js",
            "year": "1995"
        },
        {
            "title": "Rust",
            "subtitle": "Systems programming language",
            "extension": ".rs",
            "year": "2010"
        },
        {
            "title": "Go",
            "subtitle": "Concurrent programming language",
            "extension": ".go",
            "year": "2009"
        },
        {
            "title": "TypeScript",
            "subtitle": "Typed superset of JavaScript",
            "extension": ".ts",
            "year": "2012"
        },
    ]

    app = SelectUI(
        items=languages,
        oneshot=True,
        title_key="title",
        subtitle_key="subtitle",
        info_key="year"
    )

    app.run()

    return app.selected_item


def select_from_simple_list():
    """Select from a simple list of items."""

    items = [
        {"title": "Option A"},
        {"title": "Option B"},
        {"title": "Option C"},
        {"title": "Option D"},
    ]

    app = SelectUI(
        items=items,
        oneshot=True
    )

    app.run()

    return app.selected_item


def select_file():
    """Select a file from current directory."""
    import os

    files = []
    for filename in os.listdir('.'):
        if os.path.isfile(filename):
            size = os.path.getsize(filename)
            files.append({
                "title": filename,
                "subtitle": f"{size} bytes",
                "path": os.path.abspath(filename)
            })

    if not files:
        print("No files found in current directory", file=sys.stderr)
        return None

    app = SelectUI(
        items=files,
        oneshot=True,
        title_key="title",
        subtitle_key="subtitle"
    )

    app.run()

    return app.selected_item


if __name__ == "__main__":
    print("SelectUI Library Example\n", file=sys.stderr)
    print("Choose an example:", file=sys.stderr)
    print("1. Select a programming language", file=sys.stderr)
    print("2. Select from simple list", file=sys.stderr)
    print("3. Select a file", file=sys.stderr)

    choice = input("\nEnter choice (1-3): ")

    result = None
    if choice == "1":
        result = select_programming_language()
    elif choice == "2":
        result = select_from_simple_list()
    elif choice == "3":
        result = select_file()
    else:
        print("Invalid choice", file=sys.stderr)
        sys.exit(1)

    if result:
        print("\nYou selected:", file=sys.stderr)
        import json
        print(json.dumps(result, indent=2), file=sys.stderr)
    else:
        print("\nNo selection made", file=sys.stderr)