Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option for custom fill characters if bar finishes #3

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/glitzer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import glitzer/progress
pub fn sleep(ms: Int) -> a

pub fn main() {
let bar = progress.default_bar()
let bar =
progress.default_bar()
|> progress.with_fill_finished(progress.char_from_string("-"))

do_something(bar, 0)
}
Expand Down
44 changes: 38 additions & 6 deletions src/glitzer/progress.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/io
import gleam/option.{type Option}
import gleam/string
import gleam/string_builder.{type StringBuilder}

Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn string_from_char(from in: Char) -> String {
}

pub opaque type State {
State(progress: Int)
State(progress: Int, finished: Bool)
}

/// The style of a progress bar.
Expand All @@ -46,6 +47,7 @@ pub opaque type ProgressStyle {
right: String,
empty: Char,
fill: Char,
fill_finished: Option(Char),
length: Int,
state: State,
)
Expand All @@ -58,8 +60,9 @@ pub fn default_bar() -> ProgressStyle {
right: "]",
empty: Char(" "),
fill: Char("#"),
fill_finished: option.None,
length: 100,
state: State(progress: 0),
state: State(progress: 0, finished: False),
)
}

Expand All @@ -85,8 +88,9 @@ pub fn new_bar() -> ProgressStyle {
right: "",
empty: Char(" "),
fill: Char(" "),
fill_finished: option.None,
length: 0,
state: State(progress: 0),
state: State(progress: 0, finished: False),
)
}

Expand Down Expand Up @@ -118,19 +122,38 @@ pub fn with_fill(bar bar: ProgressStyle, fill char: Char) -> ProgressStyle {
ProgressStyle(..bar, fill: char)
}

/// Add a character that will be used instead of `fill` if the bar is finished.
pub fn with_fill_finished(
bar bar: ProgressStyle,
fill char: Char,
) -> ProgressStyle {
ProgressStyle(..bar, fill_finished: option.Some(char))
}

/// Add length to a progress bar.
pub fn with_length(bar bar: ProgressStyle, length len: Int) -> ProgressStyle {
ProgressStyle(..bar, length: len)
}

/// Increase the progress of the bar by one.
pub fn tick(bar bar: ProgressStyle) -> ProgressStyle {
ProgressStyle(..bar, state: State(progress: bar.state.progress + 1))
case bar.state.progress < bar.length + 1 {
True ->
ProgressStyle(
..bar,
state: State(..bar.state, progress: bar.state.progress + 1),
)
False ->
ProgressStyle(
..bar,
state: State(finished: True, progress: bar.state.progress + 1),
)
}
}

/// Completely fill the progress bar.
pub fn finish(bar bar: ProgressStyle) -> ProgressStyle {
ProgressStyle(..bar, state: State(progress: bar.length + 1))
ProgressStyle(..bar, state: State(finished: True, progress: bar.length + 1))
}

/// Print the progress bar to stderr.
Expand Down Expand Up @@ -180,7 +203,16 @@ fn build_progress_fill(
count: Int,
) -> StringBuilder {
let fill = case left_nonempty > 0 {
True -> string_builder.append(fill, bar.fill.char)
True -> {
case bar.state.finished {
True ->
string_builder.append(
fill,
option.unwrap(bar.fill_finished, bar.fill).char,
)
False -> string_builder.append(fill, bar.fill.char)
}
}
False -> string_builder.append(fill, bar.empty.char)
}

Expand Down