/
spinner.ex
351 lines (287 loc) · 12 KB
/
spinner.ex
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
defmodule Owl.Spinner do
@moduledoc ~S"""
A spinner widget.
Simply run any long-running task using `run/2`:
Owl.Spinner.run(
fn -> Process.sleep(5_000) end,
labels: [ok: "Done", error: "Failed", processing: "Please wait..."]
)
Multiple spinners can be run simultaneously:
long_running_tasks =
Enum.map([9000, 8000, 4000, 6000], fn delay ->
fn -> Process.sleep(delay) end
end)
long_running_tasks
|> Task.async_stream(&Owl.Spinner.run/1, timeout: :infinity)
|> Stream.run()
Multiline frames are supported as well:
Owl.Spinner.run(fn -> Process.sleep(5_000) end,
frames: [
processing: [
"╔════╤╤╤╤════╗\n║ │││ \\ ║\n║ │││ O ║\n║ OOO ║",
"╔════╤╤╤╤════╗\n║ ││││ ║\n║ ││││ ║\n║ OOOO ║",
"╔════╤╤╤╤════╗\n║ / │││ ║\n║ O │││ ║\n║ OOO ║",
"╔════╤╤╤╤════╗\n║ ││││ ║\n║ ││││ ║\n║ OOOO ║"
]
]
)
### Where can I get alternative frames?
* https://github.com/blackode/elixir_cli_spinners/blob/master/lib/cli_spinners/spinners.ex
* https://www.google.com/search?q=ascii+spinners
"""
use GenServer, restart: :transient
@type id :: any()
@type label :: Owl.Data.t()
@type frame :: Owl.Data.t()
@default_processing_frames ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
@doc false
def start_link(opts) do
id = Keyword.fetch!(opts, :id)
GenServer.start_link(__MODULE__, opts, name: {:via, Registry, {Owl.WidgetsRegistry, id}})
end
# we define child_spec just to disable doc
@doc false
def child_spec(init_arg) do
super(init_arg)
end
@doc """
Runs a spinner during execution of `process_function` and returns its result.
The spinner is started, and automatically stopped after the function returns, regardless if there was an error when executing the function.
It is a wrapper around `start/1` and `stop/1`. The only downside of `run/2` is that it is not possible to update
a label while `process_function` is executing.
If function returns `:ok` or `{:ok, value}` then spinner will be stopped with `:ok` resolution.
If function returns `:error` or `{:error, reason}` then spinner will be stopped with `:error` resolution.
## Options
* `:refresh_every` - period of changing frames. Defaults to `100`.
* `:frames` - allows to set frames for different states of spinner:
* `:processing` - list of frames which are rendered until spinner is stopped.
Defaults to `#{inspect(@default_processing_frames)}`.
* `:ok` - frame that is rendered when spinner is stopped with `:ok` resolution.
Defaults to `Owl.Data.tag("✔", :green)`.
* `:error` - frame that is rendered when spinner is stopped with `:error` resolution.
Defaults to `Owl.Data.tag("✖", :red)`.
* `:labels` - allows to set labels for different states of spinner:
* `:processing` - label that is rendered during processing. Cannot be changed during execution of `process_function`.
Defaults to `nil`.
* `:ok` - label that is rendered when spinner is stopped with `:ok` resolution. A function with arity 1 can be
passed in order to format a label based on result of `process_function`.
Defaults to `nil`.
* `:error` - label that is rendered when spinner is stopped with `:error` resolution. A function with arity 1
can be passed in order to format a label based on result of `process_function`.
Defaults to `nil`.
* `:live_screen_server` - a reference to `Owl.LiveScreen` server. Defaults to `Owl.LiveScreen`.
## Examples
Owl.Spinner.run(fn -> Process.sleep(5_000) end)
=> :ok
Owl.Spinner.run(fn -> Process.sleep(5_000) end,
frames: [
# an ASCII fish going back and forth
processing: [
">))'>",
" >))'>",
" >))'>",
" <'((<",
"<'((<"
]
]
)
=> :ok
Owl.Spinner.run(
fn ->
Process.sleep(5_000)
{:error, :oops}
end,
labels: [
error: fn reason -> "Failed: \#{inspect(reason)}" end,
processing: "Processing..."
]
)
=> {:error, :oops}
"""
@spec run(process_function :: (() -> :ok | :error | {:ok, value} | {:error, reason}),
refresh_every: non_neg_integer(),
frames: [ok: frame(), error: frame(), processing: [frame()]],
labels: [
ok: label() | (nil | value -> label() | nil) | nil,
error: label() | (nil | reason -> label()) | nil,
processing: label() | nil
],
live_screen_server: GenServer.server()
) :: :ok | :error | {:ok, value} | {:error, reason}
when value: any, reason: any
def run(process_function, opts \\ []) do
id = make_ref()
with {:ok, _server_pid} <-
start(
opts
|> Keyword.take([:refresh_every, :live_screen_server, :frames, :labels])
|> Keyword.update(:labels, [], fn labels -> Keyword.take(labels, [:processing]) end)
|> Keyword.put(:id, id)
) do
try do
result = process_function.()
labels = Keyword.get(opts, :labels, [])
case result do
:ok ->
label = maybe_get_lazy_label(labels, :ok, nil)
stop(id: id, resolution: :ok, label: label)
{:ok, value} ->
label = maybe_get_lazy_label(labels, :ok, value)
stop(id: id, resolution: :ok, label: label)
:error ->
label = maybe_get_lazy_label(labels, :error, nil)
stop(id: id, resolution: :error, label: label)
{:error, reason} ->
label = maybe_get_lazy_label(labels, :error, reason)
stop(id: id, resolution: :error, label: label)
end
result
rescue
e ->
stop(id: id, resolution: :error)
reraise(e, __STACKTRACE__)
end
end
end
defp maybe_get_lazy_label(labels, key, value) do
case labels[key] do
callback when is_function(callback, 1) -> callback.(value)
label -> label
end
end
@doc """
Starts a new spinner.
Must be stopped manually by calling `stop/1`.
## Options
* `:id` - an id of the spinner. Required.
* `:refresh_every` - period of changing frames. Defaults to `100`.
* `:frames` - allows to set frames for different states of spinner:
* `:processing` - list of frames which are rendered until spinner is stopped.
Defaults to `#{inspect(@default_processing_frames)}`.
* `:ok` - frame that is rendered when spinner is stopped with `:ok` resolution.
Defaults to `Owl.Data.tag("✔", :green)`.
* `:error` - frame that is rendered when spinner is stopped with `:error` resolution.
Defaults to `Owl.Data.tag("✖", :red)`.
* `:labels` - allows to set labels for different states of spinner:
* `:processing` - label that is rendered during processing. Can be changed with `update_label/1`.
Defaults to `nil`.
* `:ok` - label that is rendered when spinner is stopped with `:ok` resolution.
Defaults to `nil`.
* `:error` - label that is rendered when spinner is stopped with `:error` resolution.
Defaults to `nil`.
* `:live_screen_server` - a reference to `Owl.LiveScreen` server. Defaults to `Owl.LiveScreen`.
## Example
Owl.Spinner.start(id: :my_spinner)
Process.sleep(1000)
Owl.Spinner.stop(id: :my_spinner, resolution: :ok)
"""
@spec start(
id: id(),
frames: [ok: frame(), error: frame(), processing: [frame()]],
labels: [ok: label() | nil, error: label() | nil, processing: label() | nil],
refresh_every: non_neg_integer(),
live_screen_server: GenServer.server()
) :: DynamicSupervisor.on_start_child()
def start(opts) do
DynamicSupervisor.start_child(Owl.WidgetsSupervisor, {__MODULE__, opts})
end
@doc """
Updates a label of the running spinner.
Overrides a value that is set for `:processing` state on start.
## Options
* `:id` - an id of the spinner. Required.
* `:label` - a new value of the label. Required.
## Example
Owl.Spinner.start(id: :my_spinner)
Owl.Spinner.update_label(id: :my_spinner, label: "Downloading files...")
Process.sleep(1000)
Owl.Spinner.update_label(id: :my_spinner, label: "Checking signatures...")
Process.sleep(1000)
Owl.Spinner.stop(id: :my_spinner, resolution: :ok, label: "Done")
"""
@spec update_label(id: id(), label: label()) :: :ok
def update_label(opts) do
id = Keyword.fetch!(opts, :id)
label = Keyword.fetch!(opts, :label)
GenServer.cast({:via, Registry, {Owl.WidgetsRegistry, id}}, {:update_label, label})
end
@doc """
Stops the spinner.
## Options
* `:id` - an id of the spinner. Required.
* `:resolution` - an atom `:ok` or `:error`. Determines frame and label for final rendering. Required.
* `:label` - a label for final rendering. If not set, then values that are set on spinner start will be used.
## Example
Owl.Spinner.stop(id: :my_spinner, resolution: :ok)
"""
@spec stop(id: id(), resolution: :ok | :error, label: label()) :: :ok
def stop(opts) do
id = Keyword.fetch!(opts, :id)
GenServer.call({:via, Registry, {Owl.WidgetsRegistry, id}}, {:stop, opts})
end
@impl true
def init(opts) do
frames = Keyword.get(opts, :frames, [])
processing_frames = Keyword.get(frames, :processing, @default_processing_frames)
ok_frame = Keyword.get(frames, :ok, Owl.Data.tag("✔", :green))
error_frame = Keyword.get(frames, :error, Owl.Data.tag("✖", :red))
labels = Keyword.get(opts, :labels, [])
ok_label = Keyword.get(labels, :ok)
error_label = Keyword.get(labels, :error)
processing_label = Keyword.get(labels, :processing)
refresh_every = Keyword.get(opts, :refresh_every, 100)
live_screen_server = opts[:live_screen_server] || Owl.LiveScreen
live_screen_ref = make_ref()
{current_frame, next_processing_frames} = rotate_frames(processing_frames)
Owl.LiveScreen.add_block(live_screen_server, live_screen_ref,
state: %{frame: current_frame, label: processing_label},
render: &render/1
)
Process.send_after(self(), :tick, refresh_every)
{:ok,
%{
refresh_every: refresh_every,
live_screen_ref: live_screen_ref,
live_screen_server: live_screen_server,
processing_frames: next_processing_frames,
ok_frame: ok_frame,
error_frame: error_frame,
ok_label: ok_label,
error_label: error_label,
processing_label: processing_label
}}
end
@impl true
def handle_cast({:update_label, new_value}, state) do
{:noreply, %{state | processing_label: new_value}}
end
@impl true
def handle_call({:stop, opts}, _from, state) do
{frame, label} =
case Keyword.fetch!(opts, :resolution) do
:ok -> {state.ok_frame, Keyword.get(opts, :label, state.ok_label)}
:error -> {state.error_frame, Keyword.get(opts, :label, state.error_label)}
end
Owl.LiveScreen.update(state.live_screen_server, state.live_screen_ref, %{
frame: frame,
label: label
})
Owl.LiveScreen.await_render(state.live_screen_server)
{:stop, :normal, :ok, state}
end
@impl true
def handle_info(:tick, state) do
{current_frame, next_processing_frames} = rotate_frames(state.processing_frames)
Owl.LiveScreen.update(state.live_screen_server, state.live_screen_ref, %{
frame: current_frame,
label: state.processing_label
})
Process.send_after(self(), :tick, state.refresh_every)
{:noreply, %{state | processing_frames: next_processing_frames}}
end
defp rotate_frames([head | rest]) do
{head, rest ++ [head]}
end
defp render(%{frame: frame, label: nil}), do: frame
defp render(%{frame: frame, label: label}), do: [frame, " ", label]
end