Skip to content

Commit

Permalink
feat(leaves): option to toggle percentage display on progress bar (#33)
Browse files Browse the repository at this point in the history
* feat(progress): option to hide percentage
  • Loading branch information
wesleimp authored Feb 16, 2024
1 parent bac2173 commit 1c5e171
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
Binary file modified examples/progress/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions examples/progress/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@ type model = {
gray_bar : Progress.t;
color_bar : Progress.t;
emoji_bar : Progress.t;
no_percentage_bar : Progress.t;
}

let initial_model =
Expand All @@ -21,6 22,10 @@ let initial_model =
Progress.make ~full_char:"" ~trail_char:"🚀" ~width:25
~color:(`Plain (Spices.color "#000000"))
();
no_percentage_bar =
Progress.make ~width
~color:(`Plain (Spices.color "#4776E6"))
~show_percentage:false ();
}

let update event m =
Expand All @@ -30,12 35,14 @@ let update event m =
let gray_bar = Progress.increment m.gray_bar 0.001 in
let color_bar = Progress.increment m.color_bar (Random.float 0.0001) in
let emoji_bar = Progress.increment m.emoji_bar 0.00005 in
({ gray_bar; color_bar; emoji_bar }, Command.Noop)
let no_percentage_bar = Progress.increment m.no_percentage_bar 0.00003 in
({ gray_bar; color_bar; emoji_bar; no_percentage_bar }, Command.Noop)
| _ -> (m, Command.Noop)

let view m =
Format.sprintf "\n\n%s\n\n%s\n\n%s\n\n" (Progress.view m.gray_bar)
Format.sprintf "\n\n%s\n\n%s\n\n%s\n\n%s\n\n" (Progress.view m.gray_bar)
(Progress.view m.color_bar)
(Progress.view m.emoji_bar)
(Progress.view m.no_percentage_bar)

let () = Minttea.app ~init ~update ~view () |> Minttea.start ~initial_model
8 changes: 6 additions & 2 deletions leaves/progress.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 6,26 @@ type t = {
full_char : string;
empty_char : string;
trail_char : string;
show_percentage : bool;
}

let default_full_char = ""
let default_empty_char = " "
let default_trail_char = ""
let default_color = `Plain (Spices.color "#00FFA3")
let default_show_percentage = true

let make ?(percent = 0.) ?(full_char = default_full_char)
?(trail_char = default_trail_char) ?(empty_char = default_empty_char)
?(color = default_color) ~width () =
?(color = default_color) ?(show_percentage = default_show_percentage) ~width
() =
{
width;
percent;
full_char;
empty_char;
trail_char;
show_percentage;
finished = false;
color =
(match color with
Expand Down Expand Up @@ -74,6 78,6 @@ let view t =
List.init empty_size (fun _ -> t.empty_char)
|> List.iter (fun cell -> Format.fprintf fmt "%s" cell);

Format.fprintf fmt " %4.1f%%%!" (percent *. 100.);
if t.show_percentage then Format.fprintf fmt " %4.1f%%%!" (percent *. 100.);

Buffer.contents buf
1 change: 1 addition & 0 deletions leaves/progress.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,7 @@ val make :
?color:
[< `Gradient of Spices.color * Spices.color
| `Plain of Spices.color > `Plain ] ->
?show_percentage:bool ->
width:int ->
unit ->
t
Expand Down

0 comments on commit 1c5e171

Please sign in to comment.