-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiphlow.py
More file actions
135 lines (106 loc) · 3.54 KB
/
miniphlow.py
File metadata and controls
135 lines (106 loc) · 3.54 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
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
from typing import Callable
class MiniPhlowView:
name = str
priority = int
def render(self):
print(f"\n--- {self.name} ---")
class MiniPhlowListView(MiniPhlowView):
items = Callable[[], object]
show = Callable[[object], str]
send = Callable[[int], object]
def render(self):
super().render()
items = self.items()
for i, item in enumerate(items):
print(f" {i + 1}. {self.show(item)}")
return items
class MiniPhlowColumnedListView(MiniPhlowListView):
items = Callable[[], object]
columns = dict[str, Callable[[object], str]]
send = Callable[[int], object]
def column(self, name, f):
self.columns[name] = f
def render(self):
MiniPhlowView.render(self)
items = self.items()
cols = list(self.columns.keys())
rows = [[f(item) for _, f in self.columns.items()] for item in items]
widths = [max(len(c), *(len(r[i]) for r in rows)) for i, c in enumerate(cols)]
header = " | ".join(c.ljust(w) for c, w in zip(cols, widths))
print(f" {header}")
print(f" {'-+-'.join('-' * w for w in widths)}")
for i, row in enumerate(rows):
print(f" {' | '.join(v.ljust(w) for v, w in zip(row, widths))}")
return items
def view(func):
setattr(func, "miniphlow", True)
return func
def _collect_views(obj):
views = []
for name in dir(obj):
attr = getattr(obj, name)
if callable(attr) and getattr(attr, "miniphlow", False):
views.append(attr)
views.sort(key=lambda v: getattr(v(), "priority", 0))
return views
def inspect(obj):
history = []
while True:
views = _collect_views(obj)
if not views:
print(f"No views for {obj!r}")
if history:
obj = history.pop()
continue
return
view_instances = [v() for v in views]
print(f"\n--- Inspecting: {obj!r} ---")
print("Views:")
for i, v in enumerate(view_instances):
print(f" [{i + 1}] {v.name}")
try:
raw = input("\n> ").strip()
except (EOFError, KeyboardInterrupt):
print()
return
if raw in ("q", "quit", "exit"):
return
if raw in ("b", "back"):
if history:
obj = history.pop()
else:
print("Nothing to go back to.")
continue
parts = raw.split(None, 1)
if not parts:
continue
# select a view
try:
view_idx = int(parts[0]) - 1
except ValueError:
print("Enter a view number, 'b' to go back, or 'q' to quit.")
continue
if view_idx < 0 or view_idx >= len(view_instances):
print(f"View number out of range (1-{len(view_instances)}).")
continue
selected = view_instances[view_idx]
selected.render()
while True:
try:
raw = input("\nitem> ").strip()
except (EOFError, KeyboardInterrupt):
print()
return
if raw in ("b", "back", ""):
break
if raw in ("q", "quit", "exit"):
return
try:
item_idx = int(raw) - 1
except ValueError:
print("Enter an item number or 'b' to go back.")
continue
next_obj = selected.send(item_idx)
history.append(obj)
obj = next_obj
break