-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
239 lines (192 loc) · 7.81 KB
/
Copy pathapp.py
File metadata and controls
239 lines (192 loc) · 7.81 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""File picker — scrollable directory browser with saga-driven I/O.
Demonstrates: scroll viewport, saga for directory reads, frozen tuples,
derived scroll offset, ReducerResult, quit_on combinator, App.from_dir.
uv run python examples/filepicker/app.py
"""
from __future__ import annotations
import os
from dataclasses import dataclass, replace
from pathlib import Path
from milo import Action, App, Call, Key, Put, Quit, ReducerResult, SpecialKey
VIEWPORT_HEIGHT = 15
# ---------------------------------------------------------------------------
# Entry dataclass — one per file/directory in the listing
# ---------------------------------------------------------------------------
@dataclass(frozen=True, slots=True)
class Entry:
name: str
is_dir: bool = False
size: int = 0
# ---------------------------------------------------------------------------
# State
# ---------------------------------------------------------------------------
@dataclass(frozen=True, slots=True)
class State:
cwd: str = ""
entries: tuple[Entry, ...] = ()
cursor: int = 0
scroll_offset: int = 0
selected: str = "" # non-empty when a file is chosen
cancelled: bool = False
loading: bool = True
error: str = ""
# ---------------------------------------------------------------------------
# Saga — read directory listing
# ---------------------------------------------------------------------------
def read_directory(path: str) -> list[dict]:
"""Read directory contents. Returns list of dicts for each entry."""
p = Path(path)
dirs: list[dict] = []
files: list[dict] = []
try:
for child in sorted(p.iterdir(), key=lambda c: c.name.lower()):
try:
if child.is_dir():
dirs.append({"name": child.name, "is_dir": True, "size": 0})
else:
size = child.stat().st_size
files.append({"name": child.name, "is_dir": False, "size": size})
except PermissionError:
files.append({"name": child.name, "is_dir": False, "size": 0})
except PermissionError:
return []
# Directories first, then files
return dirs + files
def load_dir_saga():
"""Saga: read cwd from state, list directory, dispatch result."""
from milo import Select
state = yield Select()
cwd = state.cwd
try:
result = yield Call(read_directory, (cwd,))
yield Put(Action("DIR_LOADED", payload=result))
except Exception as e:
yield Put(Action("DIR_ERROR", payload=str(e)))
# ---------------------------------------------------------------------------
# Scroll offset derivation
# ---------------------------------------------------------------------------
def derive_scroll_offset(cursor: int, current_offset: int, total: int) -> int:
"""Compute scroll offset so the cursor stays within the viewport."""
if total <= VIEWPORT_HEIGHT:
return 0
# If cursor moved below visible area, scroll down
if cursor >= current_offset + VIEWPORT_HEIGHT:
return cursor - VIEWPORT_HEIGHT + 1
# If cursor moved above visible area, scroll up
if cursor < current_offset:
return cursor
return current_offset
# ---------------------------------------------------------------------------
# Format helpers exposed to template via state
# ---------------------------------------------------------------------------
def format_size(size: int) -> str:
"""Human-readable file size."""
if size == 0:
return ""
for unit in ("B", "KB", "MB", "GB", "TB"):
if size < 1024:
if unit == "B":
return f"{size} {unit}"
return f"{size:.1f} {unit}"
size /= 1024 # type: ignore[assignment]
return f"{size:.1f} PB"
# ---------------------------------------------------------------------------
# Reducer
#
# Note: This reducer handles quit and cursor navigation manually
# because quit sets cancelled=True on state and scroll_offset must
# be derived alongside each cursor move.
# ---------------------------------------------------------------------------
def reducer(state: State | None, action: Action) -> State | ReducerResult | Quit:
if state is None:
# Initial state triggers directory load
cwd = os.getcwd()
return ReducerResult(
state=State(cwd=cwd, loading=True),
sagas=(load_dir_saga,),
)
match action.type:
case "@@KEY":
key: Key = action.payload
# Quit
if key.name == SpecialKey.ESCAPE or key.char == "q":
return Quit(replace(state, cancelled=True))
# Navigation
if key.name == SpecialKey.UP:
new_cursor = max(0, state.cursor - 1)
new_offset = derive_scroll_offset(
new_cursor, state.scroll_offset, len(state.entries)
)
return replace(
state, cursor=new_cursor, scroll_offset=new_offset
)
if key.name == SpecialKey.DOWN:
new_cursor = min(len(state.entries) - 1, state.cursor + 1)
new_offset = derive_scroll_offset(
new_cursor, state.scroll_offset, len(state.entries)
)
return replace(
state, cursor=new_cursor, scroll_offset=new_offset
)
# Enter — open directory or select file
if key.name == SpecialKey.ENTER and state.entries:
entry = state.entries[state.cursor]
if entry.is_dir:
new_cwd = str(Path(state.cwd) / entry.name)
return ReducerResult(
state=replace(
state,
cwd=new_cwd,
entries=(),
cursor=0,
scroll_offset=0,
loading=True,
error="",
),
sagas=(load_dir_saga,),
)
else:
selected = str(Path(state.cwd) / entry.name)
return Quit(replace(state, selected=selected))
# Backspace — go up one directory
if key.name == SpecialKey.BACKSPACE:
parent = str(Path(state.cwd).parent)
if parent != state.cwd:
return ReducerResult(
state=replace(
state,
cwd=parent,
entries=(),
cursor=0,
scroll_offset=0,
loading=True,
error="",
),
sagas=(load_dir_saga,),
)
case "DIR_LOADED":
raw: list[dict] = action.payload
entries = tuple(
Entry(name=e["name"], is_dir=e["is_dir"], size=e["size"]) for e in raw
)
return replace(state, entries=entries, loading=False, error="")
case "DIR_ERROR":
return replace(
state, entries=(), loading=False, error=str(action.payload)
)
return state
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
if __name__ == "__main__":
app = App.from_dir(
__file__,
template="filepicker.kida",
reducer=reducer,
initial_state=State(),
exit_template="exit.kida",
)
# Add format_size as a global so the template can use it
app._env.globals["format_size"] = format_size
app._env.globals["viewport_height"] = VIEWPORT_HEIGHT
app.run()