Skip to content

Instantly share code, notes, and snippets.

@escherlies
Created March 1, 2023 09:36
Show Gist options
  • Save escherlies/2bdb3910bc82dceb16baab795f5920eb to your computer and use it in GitHub Desktop.
Save escherlies/2bdb3910bc82dceb16baab795f5920eb to your computer and use it in GitHub Desktop.
An ascii progress bar in Elm
-- | 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