|
1 | 1 | import gleam/io
|
| 2 | +import gleam/iterator.{type Iterator} |
2 | 3 | import gleam/option.{type Option}
|
3 | 4 | import gleam/string
|
4 | 5 | import gleam/string_builder.{type StringBuilder}
|
@@ -281,17 +282,28 @@ pub fn finish(bar bar: ProgressStyle) -> ProgressStyle {
|
281 | 282 | /// }
|
282 | 283 | /// ```
|
283 | 284 | pub fn print_bar(bar bar: ProgressStyle) {
|
| 285 | + let bar = |
| 286 | + ProgressStyle( |
| 287 | + ..bar, |
| 288 | + state: State(..bar.state, finished: bar.state.progress >= bar.length), |
| 289 | + ) |
284 | 290 | let fill =
|
285 |
| - build_progress_fill(string_builder.new(), bar, bar.state.progress, 0) |
| 291 | + build_progress_fill(string_builder.new(), bar, bar.state.progress + 1, 0) |
286 | 292 | |> string_builder.to_string
|
287 | 293 |
|
| 294 | + let end = case bar.state.finished { |
| 295 | + True -> "\n" |
| 296 | + False -> "" |
| 297 | + } |
| 298 | + |
288 | 299 | io.print_error(
|
289 | 300 | codes.hide_cursor_code
|
290 | 301 | <> codes.clear_line_code
|
291 | 302 | <> codes.return_line_start_code
|
292 | 303 | <> bar.left
|
293 | 304 | <> fill
|
294 |
| - <> bar.right, |
| 305 | + <> bar.right |
| 306 | + <> end, |
295 | 307 | )
|
296 | 308 | }
|
297 | 309 |
|
@@ -356,3 +368,69 @@ fn get_finished_fill(fill: StringBuilder, bar: ProgressStyle) -> StringBuilder {
|
356 | 368 | False -> string_builder.append(fill, bar.fill.char)
|
357 | 369 | }
|
358 | 370 | }
|
| 371 | + |
| 372 | +/// Map an iterator to a function with a bar that ticks every run of the |
| 373 | +/// function. |
| 374 | +/// |
| 375 | +/// <details> |
| 376 | +/// <summary>Example:<summary> |
| 377 | +/// |
| 378 | +/// ```gleam |
| 379 | +/// import glitzer/progress |
| 380 | +/// |
| 381 | +/// fn example(bar) { |
| 382 | +/// iterator.range(0, 100) |
| 383 | +/// |> progress.map_iterator(fn(bar, element) { |
| 384 | +/// progress.print_bar(bar) |
| 385 | +/// // do some heavy calculations here >:) |
| 386 | +/// }) |
| 387 | +/// } |
| 388 | +/// ``` |
| 389 | +pub fn map_iterator( |
| 390 | + over i: Iterator(a), |
| 391 | + bar bar: ProgressStyle, |
| 392 | + with fun: fn(ProgressStyle, a) -> b, |
| 393 | +) -> Iterator(b) { |
| 394 | + iterator.index(i) |
| 395 | + |> iterator.map(fn(pair) { |
| 396 | + let #(el, i) = pair |
| 397 | + tick_bar_by_i(bar, i) |
| 398 | + |> fun(el) |
| 399 | + }) |
| 400 | +} |
| 401 | + |
| 402 | +fn tick_bar_by_i(bar, i) -> ProgressStyle { |
| 403 | + case i > 0 { |
| 404 | + True -> tick_bar_by_i(tick(bar), i - 1) |
| 405 | + False -> bar |
| 406 | + } |
| 407 | +} |
| 408 | + |
| 409 | +pub fn map2_iterator( |
| 410 | + iterator1 i1: Iterator(a), |
| 411 | + iterator2 i2: Iterator(b), |
| 412 | + bar bar: ProgressStyle, |
| 413 | + with fun: fn(ProgressStyle, a, b) -> c, |
| 414 | +) -> Iterator(c) { |
| 415 | + iterator.zip(i1, i2) |
| 416 | + |> iterator.index |
| 417 | + |> iterator.map(fn(pair) { |
| 418 | + let #(pair, i) = pair |
| 419 | + let #(el1, el2) = pair |
| 420 | + tick_bar_by_i(bar, i) |
| 421 | + |> fun(el1, el2) |
| 422 | + }) |
| 423 | +} |
| 424 | + |
| 425 | +pub fn each_iterator( |
| 426 | + over i: Iterator(a), |
| 427 | + bar bar: ProgressStyle, |
| 428 | + with fun: fn(ProgressStyle, a) -> b, |
| 429 | +) -> Nil { |
| 430 | + iterator.index(i) |
| 431 | + |> iterator.each(fn(pair) { |
| 432 | + let #(el, i) = pair |
| 433 | + tick_bar_by_i(bar, i) |
| 434 | + |> fun(el) |
| 435 | + }) |
| 436 | +} |
0 commit comments