Skip to content

Commit

Permalink
fixed buggyness of bars + added iterator.each
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Jun 5, 2024
1 parent e64a193 commit d92649e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/glitzer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ fn do_something_else() {
|> progress.with_length(10)

iterator.range(0, 10)
|> progress.map_iterator(bar, fn(bar, i) {
progress.with_left_text(bar, int.to_string(i))
|> progress.each_iterator(bar, fn(bar, i) {
progress.with_left_text(bar, int.to_string(i) <> " ")
|> progress.print_bar
sleep(100)
})
|> iterator.run
}
42 changes: 29 additions & 13 deletions src/glitzer/progress.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,28 @@ pub fn finish(bar bar: ProgressStyle) -> ProgressStyle {
/// }
/// ```
pub fn print_bar(bar bar: ProgressStyle) {
let bar =
ProgressStyle(
..bar,
state: State(..bar.state, finished: bar.state.progress >= bar.length),
)
let fill =
build_progress_fill(string_builder.new(), bar, bar.state.progress, 0)
build_progress_fill(string_builder.new(), bar, bar.state.progress + 1, 0)
|> string_builder.to_string

let end = case bar.state.finished {
True -> "\n"
False -> ""
}

io.print_error(
codes.hide_cursor_code
<> codes.clear_line_code
<> codes.return_line_start_code
<> bar.left
<> fill
<> bar.right,
<> bar.right
<> end,
)
}

Expand All @@ -304,14 +315,6 @@ fn build_progress_fill(
) -> StringBuilder {
let fill = case left_nonempty > 0 {
True -> {
let bar =
ProgressStyle(
..bar,
state: State(
..bar.state,
finished: bar.state.progress >= bar.length + 1,
),
)
case left_nonempty == 1 {
True -> get_finished_head_fill(fill, bar)
False -> get_finished_fill(fill, bar)
Expand Down Expand Up @@ -391,14 +394,27 @@ pub fn map_iterator(
iterator.index(i)
|> iterator.map(fn(pair) {
let #(el, i) = pair
map_tick_bar(bar, i)
tick_bar_by_i(bar, i)
|> fun(el)
})
}

fn map_tick_bar(bar, i) -> ProgressStyle {
fn tick_bar_by_i(bar, i) -> ProgressStyle {
case i > 0 {
True -> map_tick_bar(tick(bar), i - 1)
True -> tick_bar_by_i(tick(bar), i - 1)
False -> bar
}
}

pub fn each_iterator(
over i: Iterator(a),
bar bar: ProgressStyle,
with fun: fn(ProgressStyle, a) -> b,
) -> Nil {
iterator.index(i)
|> iterator.each(fn(pair) {
let #(el, i) = pair
tick_bar_by_i(bar, i)
|> fun(el)
})
}

0 comments on commit d92649e

Please sign in to comment.