• Stars
    star
    320
  • Rank 131,126 (Top 3 %)
  • Language
    Elixir
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Command-line progress bars and spinners for Elixir.

ProgressBar for Elixir

Build Status Module Version Hex Docs Total Download License Last Updated

Command-line progress bars and spinners.

Screenshot

Usage

You can render:

Do you have a use case not listed below? Please open an issue or pull request!

Progress bars

Specify the current value and the total value, and a bar will be rendered to STDOUT.

ProgressBar.render(2, 3)

Output:

|==================================                |  67%

Call the function again and it will overwrite the previous bar with the new value:

ProgressBar.render(2, 3)
ProgressBar.render(3, 3)

Output:

|==================================================| 100%

This basically works by printing "\r[===โ€ฆ" each time, without a newline. The text cursor will return to the beginning of the line and overwrite the previous value.

When the bar becomes full, a newline is automatically printed, so any subsequent output gets its own line.

It's up to you to re-render the bar when something changes. Here's a trivial example of an animated progress bar:

Enum.each 1..100, fn (i) ->
  ProgressBar.render(i, 100)
  :timer.sleep 25
end

Width

The bar will automatically set its width to fit the terminal. If the terminal width can't be determined automatically, an 80 column width will be assumed.

If you really want to, you may specify an explicit terminal column width to fit inside:

ProgressBar.render(97, 100, width: 30)

Even with a wide terminal, note that the bar proper maxes out at 100 characters wide (one per percentage point) and will not go wider.

Customize format

Replace the bar, blank, left or right values.

format = [
  bar: "X",   # default: "="
  blank: ".", # default: " "
  left: "(",  # default: "|"
  right: ")", # default: "|"
]

ProgressBar.render(97, 100, format)

Output:

โ€ฆXXXXXXXXX...)  97%

bar and blank don't have to be single characters. They can be any-length strings and will repeat and truncate as appropriate.

You can provide empty-string values to remove left and right entirely.

You can also provide left or right as chardata lists with IO.ANSI values:

format = [
  left: [IO.ANSI.magenta, "PROGRESS:", IO.ANSI.reset, " |"],
]

Customize color

Specify IO.ANSI values as bar_color or blank_color. Use lists for multiple values.

format = [
  bar_color: [IO.ANSI.white, IO.ANSI.green_background],
  blank_color: IO.ANSI.red_background,
]

ProgressBar.render(97, 100, format)

percent: false

Hides the percentage shown after the bar.

ProgressBar.render(1, 1, percent: false)

Output:

โ€ฆ============|

Instead of:

โ€ฆ============| 100%

suffix: :count

This option causes the values to be printed on the suffix of your progress bar.

ProgressBar.render(9_751, 10_000, suffix: :count)

Output:

โ€ฆ=========   |  97% (9751/10000)

suffix: :bytes

This option causes the values to be treated as bytes of data, showing those amounts next to the bar.

ProgressBar.render(2_034_237, 2_097_152, suffix: :bytes)

Output:

โ€ฆ=========   |  97% (1.94/2.00 MB)

The unit (KB or MB) is determined automatically.

This is great with progressive downloads.

Indeterminate progress bars

Indeterminate progress bars will animate on their own for the duration of a function you pass to it.

ProgressBar.render_indeterminate fn ->
  # Do something for an indeterminate amount of timeโ€ฆ
  :timer.sleep 2000
end

It will alternate between four forms by default:

|=---=---=---=---=---=---=---=---=---=---=---โ€ฆ
|-=---=---=---=---=---=---=---=---=---=---=--โ€ฆ
|--=---=---=---=---=---=---=---=---=---=---=-โ€ฆ
|---=---=---=---=---=---=---=---=---=---=---=โ€ฆ

And then show as done:

|============================================โ€ฆ

The return value of the function is passed through, if you want it:

data = ProgressBar.render_indeterminate fn ->
  ApiClient.painstakingly_retrieve_data
end

IO.puts "Finally got the data: #{inspect data}"

Customize format

You can customize the forms it alternates between, as well as the done state, and the left and right bookends.

ProgressBar.render_indeterminate [
  bars: [ "Oo", "oO" ],
  done: "X",
  left: "",
  right: "",
], fn -> end

The bars list can be any length. Each string in that list is a "frame" in the animation. The bar will alternate between these strings, and then start over.

Each string in the list can be any length and will repeat to fit the bar.

Customize color

You can customize the color of the bar, and of the completed state.

ProgressBar.render_indeterminate [
  bars_color: IO.ANSI.yellow,
  done_color: IO.ANSI.green,
], fn -> end

You can pass multiple IO.ANSI values, just as with a regular progress bar. The indeterminate bar intentionally doesn't alternate between colors, so as not to trigger epileptic seizuresโ€ฆ

Interval

You can customize the millisecond interval at which it alternates. The default is 500 milliseconds.

ProgressBar.render_indeterminate([interval: 10], fn -> end)

Spinners

A spinner is similar to an indeterminate progress bar. But instead of a bar, it shows a "spinning" animation next to some text.

ProgressBar.render_spinner [text: "Loadingโ€ฆ", done: "Loaded."], fn ->
  # Do something for an indeterminate amount of timeโ€ฆ
  :timer.sleep 2000
end

This is the default animation and text:

/ Loadingโ€ฆ
- Loadingโ€ฆ
\ Loadingโ€ฆ
| Loadingโ€ฆ

Then it shows as done:

Loaded.

You can customize some things:

format = [
  frames: ["/" , "-", "\\", "|"],  # Or an atom, see below
  text: "Loadingโ€ฆ",
  done: "Loaded.",
  spinner_color: IO.ANSI.magenta,
  interval: 100,  # milliseconds between frames
]

ProgressBar.render_spinner format, my_function

Colors can be lists just like with other progress bars.

If you want the done state to also be some colored symbol, just use chardata lists:

format = [
  done: [IO.ANSI.green, "โœ“", IO.ANSI.reset, " Loaded."],
]

Or you can pass done: :remove to stop showing this line of text entirely when it completes.

As with indeterminate progress bars, the return value of the function is passed through if you want it:

data = ProgressBar.render_spinner fn ->
  ApiClient.painstakingly_retrieve_data
end

IO.puts "Finally got the data: #{inspect data}"

Predefined spinners

Instead of specifying the frames as a list, you can assign one of the predefined styles as an atom:

ProgressBar.render_spinner([frames: :braille], fn -> end)
Name Frames
:strokes (default) / - \ |
:braille โ ‹ โ ™ โ น โ ธ โ ผ โ ด โ ฆ โ ง โ ‡ โ 
:bars โ– โ–‚ โ–ƒ โ–„ โ–… โ–† โ–‡ โ–ˆ โ–‡ โ–† โ–… โ–„ โ–ƒ

Examples

To see these bad boys in action, clone this repo and run the example scripts:

# Run all examples.
mix run examples/all.exs

# See what's available.
ls examples

# Run a single example.
mix run examples/02-color.exs

Or to see them in a real project, try Sipper.

Installation

Add the dependency to your project's mix.exs:

defp deps do
  [
    {:progress_bar, "> 0.0.0"},
  ]
end

Then fetch it:

mix deps.get

See Also

License

Released under the MIT License.

More Repositories

1

dotfiles

Dotfiles.
Shell
315
star
2

vim-indexed-search

Show "Match 123 of 456 /search term/" in Vim searches. By Yakov Lerner.
Vim Script
184
star
3

validates_url_format_of

Validate the format of a URL by regexp in Ruby on Rails.
Ruby
101
star
4

sipper

Elixir Sips downloader written in Elixir.
Elixir
96
star
5

fixme

FIXME comments that raise after a certain point in time.
Ruby
81
star
6

vim-ruby-runner

Vim plugin to execute Ruby into an output buffer, similar to โŒ˜R in TextMate.
Vim Script
72
star
7

augmentations

Rails plugin that provides a simple API for extending a model/class from a module.
Ruby
66
star
8

jquery.truncator.js

"more"/"less" style truncator for jQuery that handles HTML gracefully.
JavaScript
65
star
9

hipchat-emoticons

Sinatra site on Heroku listing hidden HipChat emoticons.
JavaScript
64
star
10

tasks.tmbundle

TextMate to-do lists modelled after TaskPaper.
56
star
11

old-henrik.nyh.se

My old, Jekyll-powered personal blog. No longer in use! Replaced with Octopress:
JavaScript
55
star
12

fixme-elixir

FIXME comments that raise after a certain point in time.
Elixir
48
star
13

slugalizer

Ruby slugalizer ("Fรถรถ Bรกr!" -> "foo-bar"). Uses ActiveSupport for platform-consistent normalization. Originally by Christoffer Sawicki.
Ruby
47
star
14

vim-yaml-flattener

Vim plugin to toggle a YAML file buffer from nested format to a flat format and back again. Useful for searching/editing Rails i18n .yml files.
Ruby
37
star
15

toolbox

A site to list Hex packages by category.
Elixir
32
star
16

remit

NOT MAINTAINED: A tool for commit-by-commit code review of repositories on GitHub.
Ruby
32
star
17

vim-reveal-in-finder

Reveal the current file in the OS X Finder.
Vim Script
29
star
18

henrik.nyh.se

My personal site.
Slim
26
star
19

title_helpers

Rails plugin providing helpers to set and show the site <title>.
Ruby
16
star
20

vim-open-url

Opens URLs on the current line in your OS X default browser.
Vim Script
15
star
21

styleguide

Making my coding style an explicit, considered and versioned thing.
15
star
22

plug_and_play

Set up an Elixir Plug application with less boilerplate.
Elixir
14
star
23

ectoo

Make simple things easy in Ecto, e.g. Ectoo.max(MyModel, :age). Also .count, .min, .max, .avg.
Elixir
13
star
24

qed

Quoted Expression Demonstrator website for Elixir.
Elixir
13
star
25

blocket_se_feeds

Ruby CGI script to provide Atom feeds of Blocket.se searches.
Ruby
13
star
26

bookmarks_bar_keyboard_shortcuts

Chrome/Chromium extension. Trigger the first ten Bookmarks Bar items with Ctrl+1 through Ctrl+0.
JavaScript
12
star
27

shameless_plug

A novelty Elixir Plug to remove the word "shame" from the page body.
Elixir
11
star
28

etsy-rss

RSS feeds for Etsy searches.
Ruby
10
star
29

jquery.schmarkitup.js

jQuery plugin for non-WYSIWYG textarea markup editing. Simple and hackable.
JavaScript
9
star
30

livejournal_tasks

Ruby lib for common LiveJournal tasks, geared towards cross-posting.
Ruby
8
star
31

autho

A many-stop shop for authentication.
Ruby
7
star
32

atomica

Ruby CGI script to provide an Atom feed of your ICA-banken account activity.
Ruby
6
star
33

delishlist.com

Medium-sized Sinatra site (rewritten from Merb) deployed with Capistrano.
Ruby
6
star
34

action_mailer_tls

Use Gmail SMTP server to send emails in Rails ActionMailer
Ruby
6
star
35

delpin

Quick-and-dirty Heroku+Sequel app to map Delicious users/networks to Pinboard.
Ruby
5
star
36

rails-footnotes

Every Rails page has footnotes that link give request information and link back to your source via TextMate [extracted from Rails TextMate bundle project]
Ruby
5
star
37

i18n_utils

I18n utilities for Ruby on Rails.
Ruby
4
star
38

limechat-log.tmbundle

TextMate bundle for viewing LimeChat IRC logs in glorious Technicolor.
4
star
39

hond.ae

Honda e knowledge base site.
Slim
4
star
40

raising_finders

Enables find_by_name!(โ€ฆ) etc that raise if nothing was found, in ActiveRecord.
Ruby
4
star
41

thepugautomatic.com-eleventy

My blog, powered by Eleventy.
Ruby
3
star
42

icpenses

Performs simple expense analysis of bank transactions. Uses JSON exported by another script, ICporter.
Ruby
3
star
43

adventofcode2015

http://adventofcode.com/ solutions. All 25 days. Mix of Ruby and Elixir. Not refactoredโ€ฆ
Ruby
3
star
44

file_transfer_sender_subdirectories_plugin

Adium plugin that puts incoming files in per-sender subdirectories of the default directory.
Objective-C
3
star
45

route_speedup

Plugin version of Oleg Andreev's route recognition speedups, for Rails <2.1.
Ruby
3
star
46

man_or_markov

Tiny Sinatra quiz app on Heroku: guess which are real (Swedish) headlines and which are generated by Markov chains.
Ruby
3
star
47

image_scrapers

A collection of instructions, bookmarklets, user scripts and other scripts to download images from sites that try to prevent it.
Ruby
3
star
48

xkcdsvpw

Swedish XKCD password/passphrase generator site.
Ruby
2
star
49

henrik.github.io

My GitHub page.
HTML
2
star
50

gitwatch

GitHub webhook to notify people when certain parts of the code are touched.
Ruby
2
star
51

tintinnabulum

WiP
2
star
52

popularmedia_contact_importer

PopularMedia Contact Importer class for Ruby.
2
star
53

icporter

Exports ICA-banken bank transactions as JSON files on disk. Another script, ICpenses, does expense analysis.
Ruby
2
star
54

pairicon

Web app to get a combined avatar for pair programming commits.
Ruby
2
star
55

ica-banken_autologin

Chrome/Chromium extension. Automatically logs in to ICA-banken (simplified login without token).
JavaScript
2
star
56

trivialize

10-foot UI web app for IMDb trivia.
Ruby
1
star
57

heroku-template

A template for Sinatra apps on Heroku as I like to set them up.
Ruby
1
star
58

scss

Placeholder gem to tell you that you wanted "sass".
Ruby
1
star
59

bidtoy-react

Playing with React.
JavaScript
1
star
60

bidtoy

Playing around with AngularJS.
JavaScript
1
star
61

xkcdsvpw-js

Swedish XKCD password generator.
JavaScript
1
star
62

adventofcode2016

Advent of Code 2016 solutions in Elixir.
Elixir
1
star