Skip to content

Commit

Permalink
feat: add option for custom fill characters if bar finishes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf authored Jun 4, 2024
1 parent 2c51e26 commit 8f05c89
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
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

0 comments on commit 8f05c89

Please sign in to comment.