Created
March 1, 2023 09:36
-
-
Save escherlies/2bdb3910bc82dceb16baab795f5920eb to your computer and use it in GitHub Desktop.
An ascii progress bar in Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | An ascii progress bar. | |
-- | Should be used with a monospace font. | |
-- | Renders like this: | |
-- | | |
-- | [============ ] | |
-- | | |
-- | or half steps | |
-- | | |
-- | [======- ] | |
-- | | |
progressBar : Int -> Float -> String | |
progressBar max p = | |
let | |
progressAbs = | |
p * toFloat max | |
left = | |
truncate progressAbs | |
half = | |
(progressAbs - toFloat left) >= 0.5 | |
leftString = | |
List.repeat left "=" | |
|> String.join "" | |
|> (\s -> | |
if half then | |
s ++ "-" | |
else | |
s | |
) | |
remaining = | |
max - String.length leftString | |
filler = | |
List.repeat remaining " " |> String.join "" | |
in | |
"[" ++ leftString ++ filler ++ "]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment