From 7e09271b29d68831e8a4ea231389c9d2877907a8 Mon Sep 17 00:00:00 2001 From: miampf Date: Sat, 8 Jun 2024 14:41:41 +0200 Subject: [PATCH 01/16] started implementation --- src/glitzer.gleam | 25 +++++++++++++++++++++ src/glitzer/multi.gleam | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/glitzer.gleam create mode 100644 src/glitzer/multi.gleam diff --git a/src/glitzer.gleam b/src/glitzer.gleam new file mode 100644 index 0000000..25f2062 --- /dev/null +++ b/src/glitzer.gleam @@ -0,0 +1,25 @@ +import gleam/iterator +import glitzer/spinner + +pub fn main() { + let s = + spinner.default_spinner() + |> spinner.with_left_text("Imma spin >:3 ") + |> spinner.with_finish_text("I'm dizzy :(") + |> spinner.spin + // this will continuously spin your spinner + + iterator.range(0, 100_000_000) + |> iterator.run + + // update the text + spinner.with_left_text(s, "Now imma spin some more :] ") + |> spinner.with_frame_transform(fn(s) { "<" <> s <> ">" }) + |> spinner.spin + + iterator.range(0, 100_000_000) + |> iterator.run + + spinner.finish(s) + // clear the line and print the finish text +} diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam new file mode 100644 index 0000000..6d793a5 --- /dev/null +++ b/src/glitzer/multi.gleam @@ -0,0 +1,49 @@ +import gleam/iterator +import gleam/list + +import glitzer/progress.{type ProgressStyle} +import glitzer/spinner.{type SpinnerStyle} + +pub opaque type StyleWrapper { + Progress(ProgressStyle) + Spinner(SpinnerStyle) +} + +pub opaque type SameLine { + SameLine(line: List(StyleWrapper)) +} + +pub fn new_same_line() -> SameLine { + SameLine([]) +} + +pub fn insert_progress_inline( + line l: SameLine, + progress p: ProgressStyle, +) -> SameLine { + SameLine(list.append(l.line, [Progress(p)])) +} + +pub fn insert_spinner_inline( + line l: SameLine, + spinner s: SpinnerStyle, +) -> SameLine { + SameLine(list.append(l.line, [Spinner(s)])) +} + +pub fn run_spinners(line l: SameLine) -> SameLine { + let new_line = + iterator.from_list(l.line) + |> iterator.map(fn(el) { + case el { + Progress(p) -> Progress(p) + Spinner(s) -> Spinner(spinner.spin(s)) + } + }) + |> iterator.to_list + SameLine(new_line) +} + +pub opaque type MultiLine { + MultiLine(lines: List(StyleWrapper)) +} From 23f533395b924edb9afa7a3c8ff1aa05dc166558 Mon Sep 17 00:00:00 2001 From: miampf Date: Sat, 8 Jun 2024 18:11:47 +0200 Subject: [PATCH 02/16] have to fix spinner.spin (new function that prints everything continuously) --- src/glitzer/multi.gleam | 61 ++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 6d793a5..831cbe1 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,5 +1,5 @@ -import gleam/iterator -import gleam/list +import gleam/dict.{type Dict} +import gleam/option.{type Option, None, Some} import glitzer/progress.{type ProgressStyle} import glitzer/spinner.{type SpinnerStyle} @@ -10,37 +10,72 @@ pub opaque type StyleWrapper { } pub opaque type SameLine { - SameLine(line: List(StyleWrapper)) + SameLine(line: Dict(String, Option(StyleWrapper))) } pub fn new_same_line() -> SameLine { - SameLine([]) + SameLine(dict.new()) } pub fn insert_progress_inline( line l: SameLine, + name n: String, progress p: ProgressStyle, ) -> SameLine { - SameLine(list.append(l.line, [Progress(p)])) + SameLine(dict.insert(l.line, n, Some(Progress(p)))) } pub fn insert_spinner_inline( line l: SameLine, + name n: String, spinner s: SpinnerStyle, ) -> SameLine { - SameLine(list.append(l.line, [Spinner(s)])) + SameLine(dict.insert(l.line, n, Some(Spinner(s)))) } -pub fn run_spinners(line l: SameLine) -> SameLine { +pub fn run_spinners_inline(line l: SameLine) -> SameLine { let new_line = - iterator.from_list(l.line) - |> iterator.map(fn(el) { - case el { - Progress(p) -> Progress(p) - Spinner(s) -> Spinner(spinner.spin(s)) + dict.map_values(l.line, fn(_key, value) { + case value { + Some(Progress(p)) -> Some(Progress(p)) + Some(Spinner(s)) -> Some(Spinner(spinner.spin(s))) + None -> None + } + }) + SameLine(new_line) +} + +pub fn run_spinner_inline(line l: SameLine, name n: String) -> SameLine { + let new_line = + dict.update(l.line, n, fn(spinner) { + case spinner { + Some(s) -> { + case s { + Some(Progress(p)) -> Some(Progress(p)) + Some(Spinner(s)) -> Some(Spinner(spinner.spin(s))) + None -> None + } + } + option.None -> option.None + } + }) + SameLine(new_line) +} + +pub fn tick_progress_inline(line l: SameLine, name n: String) -> SameLine { + let new_line = + dict.update(l.line, n, fn(progress) { + case progress { + Some(p) -> { + case p { + Some(Progress(p)) -> Some(Progress(progress.tick(p))) + Some(Spinner(s)) -> Some(Spinner(s)) + None -> None + } + } + None -> None } }) - |> iterator.to_list SameLine(new_line) } From 4866ab0e5d58acbc841cd0eb6527eadb38a95873 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 10 Jun 2024 11:52:08 +0200 Subject: [PATCH 03/16] multiple spinners in the same line --- src/glitzer.gleam | 25 +++------ src/glitzer/multi.gleam | 111 ++++++++++++++++++++++--------------- src/glitzer/progress.gleam | 15 +++++ src/glitzer/spinner.gleam | 6 +- 4 files changed, 92 insertions(+), 65 deletions(-) diff --git a/src/glitzer.gleam b/src/glitzer.gleam index 25f2062..3739d87 100644 --- a/src/glitzer.gleam +++ b/src/glitzer.gleam @@ -1,25 +1,14 @@ import gleam/iterator +import glitzer/multi import glitzer/spinner pub fn main() { - let s = - spinner.default_spinner() - |> spinner.with_left_text("Imma spin >:3 ") - |> spinner.with_finish_text("I'm dizzy :(") - |> spinner.spin - // this will continuously spin your spinner + let _multi = + multi.new_same_line() + |> multi.insert_spinner_inline("s1", spinner.pulsating_spinner()) + |> multi.insert_spinner_inline("s2", spinner.default_spinner()) + |> multi.run_line - iterator.range(0, 100_000_000) + iterator.range(0, 1_000_000_000) |> iterator.run - - // update the text - spinner.with_left_text(s, "Now imma spin some more :] ") - |> spinner.with_frame_transform(fn(s) { "<" <> s <> ">" }) - |> spinner.spin - - iterator.range(0, 100_000_000) - |> iterator.run - - spinner.finish(s) - // clear the line and print the finish text } diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 831cbe1..0e3591e 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,6 +1,11 @@ -import gleam/dict.{type Dict} +import gleam/io +import gleam/iterator +import gleam/list import gleam/option.{type Option, None, Some} +import repeatedly.{type Repeater} + +import glitzer/codes import glitzer/progress.{type ProgressStyle} import glitzer/spinner.{type SpinnerStyle} @@ -9,12 +14,22 @@ pub opaque type StyleWrapper { Spinner(SpinnerStyle) } +pub opaque type LineState { + LineState(line: List(#(String, StyleWrapper))) +} + pub opaque type SameLine { - SameLine(line: Dict(String, Option(StyleWrapper))) + // Dict of a name, the character offset and the kind of style + // that should be printed. + SameLine( + refresh_rate: Int, + repeater: Option(Repeater(LineState)), + state: LineState, + ) } pub fn new_same_line() -> SameLine { - SameLine(dict.new()) + SameLine(state: LineState(line: []), refresh_rate: 100, repeater: None) } pub fn insert_progress_inline( @@ -22,7 +37,14 @@ pub fn insert_progress_inline( name n: String, progress p: ProgressStyle, ) -> SameLine { - SameLine(dict.insert(l.line, n, Some(Progress(p)))) + SameLine( + ..l, + state: LineState( + line: list.append(l.state.line, [ + #(n, Progress(progress.with_newline_on_finished(p, False))), + ]), + ), + ) } pub fn insert_spinner_inline( @@ -30,53 +52,54 @@ pub fn insert_spinner_inline( name n: String, spinner s: SpinnerStyle, ) -> SameLine { - SameLine(dict.insert(l.line, n, Some(Spinner(s)))) + SameLine( + ..l, + state: LineState(line: list.append(l.state.line, [#(n, Spinner(s))])), + ) } -pub fn run_spinners_inline(line l: SameLine) -> SameLine { - let new_line = - dict.map_values(l.line, fn(_key, value) { - case value { - Some(Progress(p)) -> Some(Progress(p)) - Some(Spinner(s)) -> Some(Spinner(spinner.spin(s))) - None -> None - } - }) - SameLine(new_line) -} - -pub fn run_spinner_inline(line l: SameLine, name n: String) -> SameLine { - let new_line = - dict.update(l.line, n, fn(spinner) { - case spinner { - Some(s) -> { - case s { - Some(Progress(p)) -> Some(Progress(p)) - Some(Spinner(s)) -> Some(Spinner(spinner.spin(s))) - None -> None +pub fn run_line(line l: SameLine) -> SameLine { + let repeater = + repeatedly.call(l.refresh_rate, l.state, fn(state, _) { + // reset the current line + io.print( + codes.hide_cursor_code + <> codes.clear_line_code + <> codes.return_line_start_code, + ) + let new_line = + // tick all spinners in the line + iterator.from_list(state.line) + |> iterator.map(fn(el) { + let #(name, value) = el + let value = case value { + Progress(p) -> Progress(p) + Spinner(s) -> Spinner(spinner.tick(s)) } + #(name, value) + }) + |> iterator.to_list + // print the new line + iterator.from_list(new_line) + |> iterator.each(fn(el) { + let #(_, value) = el + case value { + Progress(p) -> progress.print_bar(p) + Spinner(s) -> print_spinner_inline(s) } - option.None -> option.None - } + }) + LineState(line: new_line) }) - SameLine(new_line) + SameLine(..l, repeater: Some(repeater)) } -pub fn tick_progress_inline(line l: SameLine, name n: String) -> SameLine { - let new_line = - dict.update(l.line, n, fn(progress) { - case progress { - Some(p) -> { - case p { - Some(Progress(p)) -> Some(Progress(progress.tick(p))) - Some(Spinner(s)) -> Some(Spinner(s)) - None -> None - } - } - None -> None - } - }) - SameLine(new_line) +fn print_spinner_inline(s: SpinnerStyle) { + io.print( + s.state.left_text + <> spinner.get_current_frame_string(s) + <> s.state.right_text + <> " ", + ) } pub opaque type MultiLine { diff --git a/src/glitzer/progress.gleam b/src/glitzer/progress.gleam index 3dff01d..997ce82 100644 --- a/src/glitzer/progress.gleam +++ b/src/glitzer/progress.gleam @@ -106,6 +106,7 @@ pub opaque type ProgressStyle { fill_finished: Option(Char), fill_head: Option(Char), fill_head_finished: Option(Char), + newline_on_finished: Bool, length: Int, state: State, ) @@ -123,6 +124,7 @@ pub fn default_bar() -> ProgressStyle { fill_finished: option.None, fill_head_finished: option.None, fill_head: option.None, + newline_on_finished: True, length: 100, state: State(progress: 0, finished: False), ) @@ -139,6 +141,7 @@ pub fn slim_bar() -> ProgressStyle { fill_finished: option.None, fill_head: option.None, fill_head_finished: option.None, + newline_on_finished: True, length: 100, state: State(progress: 0, finished: False), ) @@ -155,6 +158,7 @@ pub fn fancy_slim_bar() -> ProgressStyle { fill_finished: option.Some(Char(ansi.green(sym))), fill_head: option.None, fill_head_finished: option.None, + newline_on_finished: True, length: 100, state: State(progress: 0, finished: False), ) @@ -172,6 +176,7 @@ pub fn fancy_slim_arrow_bar() -> ProgressStyle { fill_finished: option.Some(Char(ansi.green(sym))), fill_head: option.Some(Char(ansi.red(sym_head))), fill_head_finished: option.Some(Char(ansi.green(sym_head))), + newline_on_finished: True, length: 100, state: State(progress: 0, finished: False), ) @@ -189,6 +194,7 @@ pub fn thick_bar() -> ProgressStyle { fill_finished: option.None, fill_head: option.None, fill_head_finished: option.None, + newline_on_finished: True, length: 100, state: State(progress: 0, finished: False), ) @@ -206,6 +212,7 @@ pub fn fancy_thick_bar() -> ProgressStyle { fill_finished: option.Some(Char(ansi.green(sym))), fill_head: option.None, fill_head_finished: option.None, + newline_on_finished: True, length: 100, state: State(progress: 0, finished: False), ) @@ -240,6 +247,7 @@ pub fn new_bar() -> ProgressStyle { fill_finished: option.None, fill_head: option.None, fill_head_finished: option.None, + newline_on_finished: True, length: 0, state: State(progress: 0, finished: False), ) @@ -295,6 +303,13 @@ pub fn with_fill_head_finished( ProgressStyle(..bar, fill_head_finished: option.Some(char)) } +pub fn with_newline_on_finished( + bar bar: ProgressStyle, + newline n: Bool, +) -> ProgressStyle { + ProgressStyle(..bar, newline_on_finished: n) +} + /// Add length to a progress bar. pub fn with_length(bar bar: ProgressStyle, length len: Int) -> ProgressStyle { ProgressStyle(..bar, length: len) diff --git a/src/glitzer/spinner.gleam b/src/glitzer/spinner.gleam index be42a62..6d22de3 100644 --- a/src/glitzer/spinner.gleam +++ b/src/glitzer/spinner.gleam @@ -35,7 +35,7 @@ import repeatedly.{type Repeater} import glitzer/codes /// Contains everything that might get changed during runtime. -pub opaque type State { +pub type State { State( frames: Frames, frame_transform: fn(String) -> String, @@ -51,7 +51,7 @@ pub opaque type Frames { Frames(frames: Array(String)) } -pub opaque type SpinnerStyle { +pub type SpinnerStyle { SpinnerStyle(tick_rate: Int, finish_text: String, state: State) } @@ -320,7 +320,7 @@ pub fn print_spinner(spinner s: SpinnerStyle) -> Nil { ) } -fn get_current_frame_string(s: SpinnerStyle) -> String { +pub fn get_current_frame_string(s: SpinnerStyle) -> String { case glearray.get( s.state.frames.frames, From a418855e09636dbd7d3a7207c516ff66a11e6133 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 10 Jun 2024 12:06:44 +0200 Subject: [PATCH 04/16] respect the tick rate of the spinners --- src/glitzer.gleam | 5 ++++- src/glitzer/multi.gleam | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/glitzer.gleam b/src/glitzer.gleam index 3739d87..f9b2098 100644 --- a/src/glitzer.gleam +++ b/src/glitzer.gleam @@ -6,7 +6,10 @@ pub fn main() { let _multi = multi.new_same_line() |> multi.insert_spinner_inline("s1", spinner.pulsating_spinner()) - |> multi.insert_spinner_inline("s2", spinner.default_spinner()) + |> multi.insert_spinner_inline( + "s2", + spinner.with_tick_rate(spinner.default_spinner(), 500), + ) |> multi.run_line iterator.range(0, 1_000_000_000) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 0e3591e..857a672 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,7 +1,9 @@ +import gleam/int import gleam/io import gleam/iterator import gleam/list import gleam/option.{type Option, None, Some} +import gleam/result import repeatedly.{type Repeater} @@ -29,7 +31,7 @@ pub opaque type SameLine { } pub fn new_same_line() -> SameLine { - SameLine(state: LineState(line: []), refresh_rate: 100, repeater: None) + SameLine(state: LineState(line: []), refresh_rate: 10, repeater: None) } pub fn insert_progress_inline( @@ -60,7 +62,7 @@ pub fn insert_spinner_inline( pub fn run_line(line l: SameLine) -> SameLine { let repeater = - repeatedly.call(l.refresh_rate, l.state, fn(state, _) { + repeatedly.call(l.refresh_rate, l.state, fn(state, i) { // reset the current line io.print( codes.hide_cursor_code @@ -74,7 +76,16 @@ pub fn run_line(line l: SameLine) -> SameLine { let #(name, value) = el let value = case value { Progress(p) -> Progress(p) - Spinner(s) -> Spinner(spinner.tick(s)) + Spinner(s) -> { + // how much time passed? + let time_gone_by = i * l.refresh_rate + // how many times should the spinner have been ticked? + let spin_count = + result.unwrap(int.floor_divide(time_gone_by, s.tick_rate), 0) + // how much do we have to tick now? + let left_to_tick = spin_count - s.state.progress + Spinner(spinner.tick_by(s, left_to_tick)) + } } #(name, value) }) From fd6b0b66487c6c1fbcf40281b395d5ff1776d539 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 10 Jun 2024 12:26:54 +0200 Subject: [PATCH 05/16] progress bar in same line integration --- src/glitzer.gleam | 11 +++++-- src/glitzer/multi.gleam | 62 ++++++++++++++++++++++++++++++++++++-- src/glitzer/progress.gleam | 18 +++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/glitzer.gleam b/src/glitzer.gleam index f9b2098..d498dd7 100644 --- a/src/glitzer.gleam +++ b/src/glitzer.gleam @@ -1,17 +1,24 @@ import gleam/iterator import glitzer/multi +import glitzer/progress import glitzer/spinner pub fn main() { - let _multi = + let multi = multi.new_same_line() |> multi.insert_spinner_inline("s1", spinner.pulsating_spinner()) |> multi.insert_spinner_inline( "s2", spinner.with_tick_rate(spinner.default_spinner(), 500), ) + |> multi.insert_progress_inline("p1", progress.fancy_slim_bar()) |> multi.run_line - iterator.range(0, 1_000_000_000) + iterator.range(0, 100_000_000) + |> iterator.run + + multi.tick_by_inline(multi, "p1", 50) + + iterator.range(0, 100_000_000) |> iterator.run } diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 857a672..135702e 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -95,7 +95,7 @@ pub fn run_line(line l: SameLine) -> SameLine { |> iterator.each(fn(el) { let #(_, value) = el case value { - Progress(p) -> progress.print_bar(p) + Progress(p) -> print_bar_inline(p) Spinner(s) -> print_spinner_inline(s) } }) @@ -104,8 +104,12 @@ pub fn run_line(line l: SameLine) -> SameLine { SameLine(..l, repeater: Some(repeater)) } +fn print_bar_inline(p: ProgressStyle) { + io.print_error(progress.get_raw_bar(p) <> " ") +} + fn print_spinner_inline(s: SpinnerStyle) { - io.print( + io.print_error( s.state.left_text <> spinner.get_current_frame_string(s) <> s.state.right_text @@ -113,6 +117,60 @@ fn print_spinner_inline(s: SpinnerStyle) { ) } +pub fn tick_inline(line l: SameLine, name n: String) -> Nil { + let value = list.key_find(l.state.line, n) + let new_state = case value { + Ok(value) -> { + case value { + Progress(p) -> + LineState(line: list.key_set( + l.state.line, + n, + Progress(progress.tick(p)), + )) + Spinner(s) -> + LineState(line: list.key_set( + l.state.line, + n, + Spinner(spinner.tick(s)), + )) + } + } + Error(_) -> l.state + } + case l.repeater { + Some(r) -> repeatedly.set_state(r, new_state) + None -> Nil + } +} + +pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> Nil { + let value = list.key_find(l.state.line, n) + let new_state = case value { + Ok(value) -> { + case value { + Progress(p) -> + LineState(line: list.key_set( + l.state.line, + n, + Progress(progress.tick_by(p, i)), + )) + Spinner(s) -> + LineState(line: list.key_set( + l.state.line, + n, + Spinner(spinner.tick_by(s, i)), + )) + } + } + Error(_) -> l.state + } + case l.repeater { + Some(r) -> repeatedly.set_state(r, new_state) + None -> Nil + } +} + pub opaque type MultiLine { MultiLine(lines: List(StyleWrapper)) } diff --git a/src/glitzer/progress.gleam b/src/glitzer/progress.gleam index 997ce82..b7b1957 100644 --- a/src/glitzer/progress.gleam +++ b/src/glitzer/progress.gleam @@ -401,6 +401,24 @@ pub fn print_bar(bar bar: ProgressStyle) { ) } +pub fn get_raw_bar(bar bar: ProgressStyle) -> String { + 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 + 1, 0) + |> string_builder.to_string + + let end = case bar.state.finished && bar.newline_on_finished { + True -> "\n" + False -> "" + } + + bar.left <> fill <> bar.right <> end +} + fn build_progress_fill( fill: StringTree, bar: ProgressStyle, From 5fd018e544df290c6e781ac985155ea43e858130 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 10 Jun 2024 12:39:54 +0200 Subject: [PATCH 06/16] fixed bug for updating tick state --- src/glitzer.gleam | 5 +++++ src/glitzer/multi.gleam | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/glitzer.gleam b/src/glitzer.gleam index d498dd7..ea482e1 100644 --- a/src/glitzer.gleam +++ b/src/glitzer.gleam @@ -12,12 +12,17 @@ pub fn main() { spinner.with_tick_rate(spinner.default_spinner(), 500), ) |> multi.insert_progress_inline("p1", progress.fancy_slim_bar()) + |> multi.insert_progress_inline( + "p2", + progress.with_length(progress.fancy_thick_bar(), 10), + ) |> multi.run_line iterator.range(0, 100_000_000) |> iterator.run multi.tick_by_inline(multi, "p1", 50) + |> multi.tick_by_inline("p2", 10) iterator.range(0, 100_000_000) |> iterator.run diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 135702e..57b74d1 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -117,7 +117,7 @@ fn print_spinner_inline(s: SpinnerStyle) { ) } -pub fn tick_inline(line l: SameLine, name n: String) -> Nil { +pub fn tick_inline(line l: SameLine, name n: String) -> SameLine { let value = list.key_find(l.state.line, n) let new_state = case value { Ok(value) -> { @@ -142,9 +142,10 @@ pub fn tick_inline(line l: SameLine, name n: String) -> Nil { Some(r) -> repeatedly.set_state(r, new_state) None -> Nil } + SameLine(..l, state: new_state) } -pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> Nil { +pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> SameLine { let value = list.key_find(l.state.line, n) let new_state = case value { Ok(value) -> { @@ -169,6 +170,7 @@ pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> Nil { Some(r) -> repeatedly.set_state(r, new_state) None -> Nil } + SameLine(..l, state: new_state) } pub opaque type MultiLine { From 7887f5b54f62fa041431a70d7eccc9ca578ced71 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 10 Jun 2024 12:47:56 +0200 Subject: [PATCH 07/16] added doc comments --- src/glitzer/multi.gleam | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 57b74d1..135d258 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,3 +1,5 @@ +///// ``` + import gleam/int import gleam/io import gleam/iterator @@ -30,10 +32,13 @@ pub opaque type SameLine { ) } +/// Create a new `SameLine` that can print multiple spinners/progress bars on +/// the same line. pub fn new_same_line() -> SameLine { SameLine(state: LineState(line: []), refresh_rate: 10, repeater: None) } +/// Insert a `ProgressStyle` at the end of a given `SameLine`. pub fn insert_progress_inline( line l: SameLine, name n: String, @@ -49,6 +54,7 @@ pub fn insert_progress_inline( ) } +/// Insert a `SpinnerStyle` at the end of a given `SameLine`. pub fn insert_spinner_inline( line l: SameLine, name n: String, @@ -60,6 +66,24 @@ pub fn insert_spinner_inline( ) } +/// Run the line. This will print the whole line every `line.refresh_rate` +/// milliseconds. It will also automatically tick all inline spinners with +/// regard to their tick rate. +/// +///
+/// Example: +/// +/// ```gleam +/// let multi = +/// multi.new_same_line() +/// |> multi.insert_spinner_inline("s1", spinner.pulsating_spinner()) +/// |> multi.insert_spinner_inline( +/// "s2", +/// spinner.with_tick_rate(spinner.default_spinner(), 500), +/// ) +/// |> multi.run_line +/// ``` +///
pub fn run_line(line l: SameLine) -> SameLine { let repeater = repeatedly.call(l.refresh_rate, l.state, fn(state, i) { @@ -117,6 +141,8 @@ fn print_spinner_inline(s: SpinnerStyle) { ) } +/// Tick a spinner or progress bar with the given name by 1. Does nothing if +/// the spinner/progress bar doesn't exist. pub fn tick_inline(line l: SameLine, name n: String) -> SameLine { let value = list.key_find(l.state.line, n) let new_state = case value { @@ -145,6 +171,8 @@ pub fn tick_inline(line l: SameLine, name n: String) -> SameLine { SameLine(..l, state: new_state) } +/// Tick a spinner or progress bar with the given name by `i`. Does nothing if +/// the spinner/progress bar doesn't exist. pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> SameLine { let value = list.key_find(l.state.line, n) let new_state = case value { From 78746667999b3d58f7196d0f9253ef2e0fe42368 Mon Sep 17 00:00:00 2001 From: miampf Date: Wed, 4 Sep 2024 10:03:00 +0200 Subject: [PATCH 08/16] structure MultiLine --- src/glitzer/multi.gleam | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 135d258..8b19583 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,5 +1,3 @@ -///// ``` - import gleam/int import gleam/io import gleam/iterator @@ -201,6 +199,11 @@ pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> SameLine { SameLine(..l, state: new_state) } +pub opaque type MultiLineWrapper { + Style(StyleWrapper) + Line(SameLine) +} + pub opaque type MultiLine { - MultiLine(lines: List(StyleWrapper)) + MultiLine(lines: List(MultiLineWrapper)) } From 9330100987cfd9afbe3d00db34121811fdbc6fa9 Mon Sep 17 00:00:00 2001 From: miampf Date: Sat, 30 Nov 2024 13:02:27 +0100 Subject: [PATCH 09/16] switch to yielder --- src/glitzer/multi.gleam | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 8b19583..798a781 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,9 +1,9 @@ import gleam/int import gleam/io -import gleam/iterator import gleam/list import gleam/option.{type Option, None, Some} import gleam/result +import gleam/yielder import repeatedly.{type Repeater} @@ -93,8 +93,8 @@ pub fn run_line(line l: SameLine) -> SameLine { ) let new_line = // tick all spinners in the line - iterator.from_list(state.line) - |> iterator.map(fn(el) { + yielder.from_list(state.line) + |> yielder.map(fn(el) { let #(name, value) = el let value = case value { Progress(p) -> Progress(p) @@ -111,10 +111,10 @@ pub fn run_line(line l: SameLine) -> SameLine { } #(name, value) }) - |> iterator.to_list + |> yielder.to_list // print the new line - iterator.from_list(new_line) - |> iterator.each(fn(el) { + yielder.from_list(new_line) + |> yielder.each(fn(el) { let #(_, value) = el case value { Progress(p) -> print_bar_inline(p) From b2637cdc5dd42de96364d2f413f131890f94d7a7 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 11:54:05 +0100 Subject: [PATCH 10/16] use `string_tree` --- src/glitzer/progress.gleam | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/glitzer/progress.gleam b/src/glitzer/progress.gleam index b7b1957..06ba9f6 100644 --- a/src/glitzer/progress.gleam +++ b/src/glitzer/progress.gleam @@ -408,8 +408,8 @@ pub fn get_raw_bar(bar bar: ProgressStyle) -> String { state: State(..bar.state, finished: bar.state.progress >= bar.length), ) let fill = - build_progress_fill(string_builder.new(), bar, bar.state.progress + 1, 0) - |> string_builder.to_string + build_progress_fill(string_tree.new(), bar, bar.state.progress + 1, 0) + |> string_tree.to_string let end = case bar.state.finished && bar.newline_on_finished { True -> "\n" From c39838f56d504e357c29898e91c9a1229100da08 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 12:00:33 +0100 Subject: [PATCH 11/16] use `yielder` in test main --- src/glitzer.gleam | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/glitzer.gleam b/src/glitzer.gleam index ea482e1..efd783f 100644 --- a/src/glitzer.gleam +++ b/src/glitzer.gleam @@ -1,4 +1,4 @@ -import gleam/iterator +import gleam/yielder import glitzer/multi import glitzer/progress import glitzer/spinner @@ -18,12 +18,12 @@ pub fn main() { ) |> multi.run_line - iterator.range(0, 100_000_000) - |> iterator.run + yielder.range(0, 100_000_000) + |> yielder.run multi.tick_by_inline(multi, "p1", 50) |> multi.tick_by_inline("p2", 10) - iterator.range(0, 100_000_000) - |> iterator.run + yielder.range(0, 100_000_000) + |> yielder.run } From be8ed246b2583a8174e04e462b9a0e0ed77915b8 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 12:17:52 +0100 Subject: [PATCH 12/16] adapt refresh rate to spinner default (also prevents flickering) This also removes types for MultiLine for later implementation --- src/glitzer/multi.gleam | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 798a781..786b9d9 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -33,7 +33,7 @@ pub opaque type SameLine { /// Create a new `SameLine` that can print multiple spinners/progress bars on /// the same line. pub fn new_same_line() -> SameLine { - SameLine(state: LineState(line: []), refresh_rate: 10, repeater: None) + SameLine(state: LineState(line: []), refresh_rate: 100, repeater: None) } /// Insert a `ProgressStyle` at the end of a given `SameLine`. @@ -198,12 +198,3 @@ pub fn tick_by_inline(line l: SameLine, name n: String, i i: Int) -> SameLine { } SameLine(..l, state: new_state) } - -pub opaque type MultiLineWrapper { - Style(StyleWrapper) - Line(SameLine) -} - -pub opaque type MultiLine { - MultiLine(lines: List(MultiLineWrapper)) -} From e0a4d9169810055252a05f00d9416a421a7cf1c2 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 12:23:52 +0100 Subject: [PATCH 13/16] remove testing main --- src/glitzer.gleam | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 src/glitzer.gleam diff --git a/src/glitzer.gleam b/src/glitzer.gleam deleted file mode 100644 index efd783f..0000000 --- a/src/glitzer.gleam +++ /dev/null @@ -1,29 +0,0 @@ -import gleam/yielder -import glitzer/multi -import glitzer/progress -import glitzer/spinner - -pub fn main() { - let multi = - multi.new_same_line() - |> multi.insert_spinner_inline("s1", spinner.pulsating_spinner()) - |> multi.insert_spinner_inline( - "s2", - spinner.with_tick_rate(spinner.default_spinner(), 500), - ) - |> multi.insert_progress_inline("p1", progress.fancy_slim_bar()) - |> multi.insert_progress_inline( - "p2", - progress.with_length(progress.fancy_thick_bar(), 10), - ) - |> multi.run_line - - yielder.range(0, 100_000_000) - |> yielder.run - - multi.tick_by_inline(multi, "p1", 50) - |> multi.tick_by_inline("p2", 10) - - yielder.range(0, 100_000_000) - |> yielder.run -} From a69c5da2a71bcf12cf4b385d979b6250daa40434 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 12:26:30 +0100 Subject: [PATCH 14/16] adjust birdie tests --- birdie_snapshots/test_progress_default_bar.accepted | 3 ++- birdie_snapshots/test_progress_fancy_slim_arrow_bar.accepted | 3 ++- birdie_snapshots/test_progress_fancy_slim_bar.accepted | 3 ++- birdie_snapshots/test_progress_fancy_thick_bar.accepted | 3 ++- birdie_snapshots/test_progress_new_bar.accepted | 3 ++- birdie_snapshots/test_progress_slim_bar.accepted | 3 ++- birdie_snapshots/test_progress_tick.accepted | 3 ++- birdie_snapshots/test_progress_tick_bar.accepted | 3 ++- birdie_snapshots/test_progress_tick_by.accepted | 3 ++- birdie_snapshots/test_progress_with_empty.accepted | 3 ++- birdie_snapshots/test_progress_with_fill.accepted | 3 ++- birdie_snapshots/test_progress_with_fill_finished.accepted | 3 ++- birdie_snapshots/test_progress_with_fill_head.accepted | 3 ++- .../test_progress_with_fill_head_finished.accepted | 3 ++- birdie_snapshots/test_progress_with_left_text.accepted | 3 ++- birdie_snapshots/test_progress_with_length.accepted | 3 ++- birdie_snapshots/test_progress_with_right_text.accepted | 3 ++- birdie_snapshots/test_progresss_finish.accepted | 3 ++- 18 files changed, 36 insertions(+), 18 deletions(-) diff --git a/birdie_snapshots/test_progress_default_bar.accepted b/birdie_snapshots/test_progress_default_bar.accepted index 98ad664..649070b 100644 --- a/birdie_snapshots/test_progress_default_bar.accepted +++ b/birdie_snapshots/test_progress_default_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.default_bar file: ./test/glitzer_test.gleam test_name: progress_default_bar_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_fancy_slim_arrow_bar.accepted b/birdie_snapshots/test_progress_fancy_slim_arrow_bar.accepted index 880a2b0..c9d5d14 100644 --- a/birdie_snapshots/test_progress_fancy_slim_arrow_bar.accepted +++ b/birdie_snapshots/test_progress_fancy_slim_arrow_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.fancy_slim_arrow_bar file: ./test/glitzer_test.gleam test_name: progress_fancy_slim_arrow_bar_test @@ -12,6 +12,7 @@ ProgressStyle( Some(Char("—")), Some(Char("→")), Some(Char("→")), + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_fancy_slim_bar.accepted b/birdie_snapshots/test_progress_fancy_slim_bar.accepted index 575e788..6bb7b57 100644 --- a/birdie_snapshots/test_progress_fancy_slim_bar.accepted +++ b/birdie_snapshots/test_progress_fancy_slim_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.fancy_slim_bar file: ./test/glitzer_test.gleam test_name: progress_fancy_slim_bar_test @@ -12,6 +12,7 @@ ProgressStyle( Some(Char("—")), None, None, + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_fancy_thick_bar.accepted b/birdie_snapshots/test_progress_fancy_thick_bar.accepted index 0f94d6e..3d4dfe3 100644 --- a/birdie_snapshots/test_progress_fancy_thick_bar.accepted +++ b/birdie_snapshots/test_progress_fancy_thick_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.fancy_thick_bar file: ./test/glitzer_test.gleam test_name: progress_fancy_thick_bar_test @@ -12,6 +12,7 @@ ProgressStyle( Some(Char("█")), None, None, + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_new_bar.accepted b/birdie_snapshots/test_progress_new_bar.accepted index 0a6c72f..5a5c3cc 100644 --- a/birdie_snapshots/test_progress_new_bar.accepted +++ b/birdie_snapshots/test_progress_new_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.new_bar file: ./test/glitzer_test.gleam test_name: progress_new_bar_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_slim_bar.accepted b/birdie_snapshots/test_progress_slim_bar.accepted index 88447be..dd66fbc 100644 --- a/birdie_snapshots/test_progress_slim_bar.accepted +++ b/birdie_snapshots/test_progress_slim_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.slim_bar file: ./test/glitzer_test.gleam test_name: progress_slim_bar_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_tick.accepted b/birdie_snapshots/test_progress_tick.accepted index 3cb30d0..42ae07a 100644 --- a/birdie_snapshots/test_progress_tick.accepted +++ b/birdie_snapshots/test_progress_tick.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.tick file: ./test/glitzer_test.gleam test_name: progress_tick_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(1, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_tick_bar.accepted b/birdie_snapshots/test_progress_tick_bar.accepted index 4e24904..86297b6 100644 --- a/birdie_snapshots/test_progress_tick_bar.accepted +++ b/birdie_snapshots/test_progress_tick_bar.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.tick_bar file: ./test/glitzer_test.gleam test_name: progress_thick_bar_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_tick_by.accepted b/birdie_snapshots/test_progress_tick_by.accepted index 4ce230b..8b7804e 100644 --- a/birdie_snapshots/test_progress_tick_by.accepted +++ b/birdie_snapshots/test_progress_tick_by.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.tick_by file: ./test/glitzer_test.gleam test_name: progress_tick_by_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(10, True), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_empty.accepted b/birdie_snapshots/test_progress_with_empty.accepted index 0fd9598..69f7dd2 100644 --- a/birdie_snapshots/test_progress_with_empty.accepted +++ b/birdie_snapshots/test_progress_with_empty.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_empty file: ./test/glitzer_test.gleam test_name: progress_with_empty_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_fill.accepted b/birdie_snapshots/test_progress_with_fill.accepted index f2d93c0..2980336 100644 --- a/birdie_snapshots/test_progress_with_fill.accepted +++ b/birdie_snapshots/test_progress_with_fill.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_fill file: ./test/glitzer_test.gleam test_name: progress_with_fill_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_fill_finished.accepted b/birdie_snapshots/test_progress_with_fill_finished.accepted index 7e285a8..bcba8f3 100644 --- a/birdie_snapshots/test_progress_with_fill_finished.accepted +++ b/birdie_snapshots/test_progress_with_fill_finished.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_fill_finished file: ./test/glitzer_test.gleam test_name: progress_with_fill_finished_test @@ -12,6 +12,7 @@ ProgressStyle( Some(Char("#")), None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_fill_head.accepted b/birdie_snapshots/test_progress_with_fill_head.accepted index 23ed4b5..9145f4d 100644 --- a/birdie_snapshots/test_progress_with_fill_head.accepted +++ b/birdie_snapshots/test_progress_with_fill_head.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_fill_head file: ./test/glitzer_test.gleam test_name: progress_with_fill_head_test @@ -12,6 +12,7 @@ ProgressStyle( None, Some(Char("#")), None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_fill_head_finished.accepted b/birdie_snapshots/test_progress_with_fill_head_finished.accepted index 18f1570..00945a9 100644 --- a/birdie_snapshots/test_progress_with_fill_head_finished.accepted +++ b/birdie_snapshots/test_progress_with_fill_head_finished.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_fill_head_finished file: ./test/glitzer_test.gleam test_name: progress_with_fill_head_finished_test @@ -12,6 +12,7 @@ ProgressStyle( Some(Char("#")), None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_left_text.accepted b/birdie_snapshots/test_progress_with_left_text.accepted index 42565c1..be68104 100644 --- a/birdie_snapshots/test_progress_with_left_text.accepted +++ b/birdie_snapshots/test_progress_with_left_text.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_left_text file: ./test/glitzer_test.gleam test_name: progress_with_left_text_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_length.accepted b/birdie_snapshots/test_progress_with_length.accepted index 92f5c07..7d03d19 100644 --- a/birdie_snapshots/test_progress_with_length.accepted +++ b/birdie_snapshots/test_progress_with_length.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_length file: ./test/glitzer_test.gleam test_name: progress_with_length_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 100, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progress_with_right_text.accepted b/birdie_snapshots/test_progress_with_right_text.accepted index 8d3987a..8ee5283 100644 --- a/birdie_snapshots/test_progress_with_right_text.accepted +++ b/birdie_snapshots/test_progress_with_right_text.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progress.with_right_text file: ./test/glitzer_test.gleam test_name: progress_with_right_text_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 0, State(0, False), ) \ No newline at end of file diff --git a/birdie_snapshots/test_progresss_finish.accepted b/birdie_snapshots/test_progresss_finish.accepted index f43e825..25d6add 100644 --- a/birdie_snapshots/test_progresss_finish.accepted +++ b/birdie_snapshots/test_progresss_finish.accepted @@ -1,5 +1,5 @@ --- -version: 1.1.6 +version: 1.2.4 title: Test progresss.finish file: ./test/glitzer_test.gleam test_name: progress_finish_test @@ -12,6 +12,7 @@ ProgressStyle( None, None, None, + True, 100, State(101, True), ) \ No newline at end of file From d2484eba2a6fffdbc7f6836ef2454fb6281949b5 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 12:35:26 +0100 Subject: [PATCH 15/16] add tests for `multi` --- ..._and_multi_insert_progress_inline.accepted | 43 +++++++++++++++++++ .../test_multi_new_same_line.accepted | 7 +++ test/glitzer_test.gleam | 17 ++++++++ 3 files changed, 67 insertions(+) create mode 100644 birdie_snapshots/test_multi_insert_spinner_inline_and_multi_insert_progress_inline.accepted create mode 100644 birdie_snapshots/test_multi_new_same_line.accepted diff --git a/birdie_snapshots/test_multi_insert_spinner_inline_and_multi_insert_progress_inline.accepted b/birdie_snapshots/test_multi_insert_spinner_inline_and_multi_insert_progress_inline.accepted new file mode 100644 index 0000000..4646e9e --- /dev/null +++ b/birdie_snapshots/test_multi_insert_spinner_inline_and_multi_insert_progress_inline.accepted @@ -0,0 +1,43 @@ +--- +version: 1.2.4 +title: Test multi.insert_spinner_inline and multi.insert_progress_inline +file: ./test/glitzer_test.gleam +test_name: multi_insert_spinner_progress_test +--- +SameLine( + 100, + None, + LineState([ + #( + "s1", + Spinner(SpinnerStyle( + 100, + "", + State( + Frames(#("|", "/", "-", "\")), + //fn(a) { ... }, + "", + "", + 0, + False, + None, + ), + )), + ), + #( + "p1", + Progress(ProgressStyle( + "[", + "]", + Char(" "), + Char("#"), + None, + None, + None, + False, + 100, + State(0, False), + )), + ), + ]), +) \ No newline at end of file diff --git a/birdie_snapshots/test_multi_new_same_line.accepted b/birdie_snapshots/test_multi_new_same_line.accepted new file mode 100644 index 0000000..08cea75 --- /dev/null +++ b/birdie_snapshots/test_multi_new_same_line.accepted @@ -0,0 +1,7 @@ +--- +version: 1.2.4 +title: Test multi.new_same_line +file: ./test/glitzer_test.gleam +test_name: multi_new_same_line_test +--- +SameLine(100, None, LineState([])) \ No newline at end of file diff --git a/test/glitzer_test.gleam b/test/glitzer_test.gleam index 332d572..ed2b929 100644 --- a/test/glitzer_test.gleam +++ b/test/glitzer_test.gleam @@ -1,6 +1,7 @@ import gleam/iterator import gleam/yielder import gleeunit +import glitzer/multi import birdie import pprint @@ -281,3 +282,19 @@ pub fn spinner_tick_by_test() { |> pprint.format |> birdie.snap(title: "Test spinner.tick_by") } + +pub fn multi_new_same_line_test() { + multi.new_same_line() + |> pprint.format + |> birdie.snap(title: "Test multi.new_same_line") +} + +pub fn multi_insert_spinner_progress_test() { + multi.new_same_line() + |> multi.insert_spinner_inline("s1", spinner.default_spinner()) + |> multi.insert_progress_inline("p1", progress.default_bar()) + |> pprint.format + |> birdie.snap( + title: "Test multi.insert_spinner_inline and multi.insert_progress_inline", + ) +} From e0d6bb7772d9343dbefe215a038450d4b739b2dc Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 2 Dec 2024 12:49:08 +0100 Subject: [PATCH 16/16] add main doc comment --- src/glitzer/multi.gleam | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/glitzer/multi.gleam b/src/glitzer/multi.gleam index 786b9d9..6e85023 100644 --- a/src/glitzer/multi.gleam +++ b/src/glitzer/multi.gleam @@ -1,3 +1,48 @@ +/// Run multiple spinners and progress bars at the same time! +/// For when you really have someting to say. +/// +/// Currently, this only supports running multiple indicators on the +/// same line using `multi.new_same_line`. +/// +/// Example: +/// +/// ```gleam +/// import gleam/yielder +/// import glitzer/multi +/// import glitzer/progress +/// import glitzer/spinner +/// +/// pub fn main() { +/// let multi = +/// multi.new_same_line() +/// |> multi.insert_spinner_inline("s1", spinner.pulsating_spinner()) +/// |> multi.insert_spinner_inline( +/// "s2", +/// spinner.with_tick_rate(spinner.default_spinner(), 500), +/// ) +/// |> multi.insert_progress_inline("p1", progress.fancy_slim_bar()) +/// |> multi.insert_progress_inline( +/// "p2", +/// progress.with_length(progress.fancy_thick_bar(), 10), +/// ) +/// |> multi.run_line +/// +/// yielder.range(0, 100_000_000) +/// |> yielder.run +/// +/// multi.tick_by_inline(multi, "p1", 50) +/// |> multi.tick_by_inline("p2", 10) +/// +/// yielder.range(0, 100_000_000) +/// |> yielder.run +/// } +/// ``` +/// +/// You can see that, when inserting an indicator using +/// `multi.insert_{progress,spinner}_inline`, you hand over a string before the +/// actual progress indicator. This string is the identifier of the indicator +/// and **has to be unique**. You use this ID to later address the spinner or +/// progress bar (e.g. with `multi.tick_by_inline`). import gleam/int import gleam/io import gleam/list