├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── lib └── tqdm.ex ├── mix.exs ├── mix.lock └── test ├── test_helper.exs └── tqdm_test.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | /doc 5 | /docs 6 | erl_crash.dump 7 | *.ez 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: elixir 2 | matrix: 3 | include: 4 | - otp_release: 18.3 5 | elixir: 1.3.2 6 | env: MIX_ENV=test ELIXIR_ERL_OPTIONS="+T 9" 7 | - otp_release: 20.0 8 | elixir: 1.5 9 | env: MIX_ENV=test ELIXIR_ERL_OPTIONS="+T 9" 10 | before_script: 11 | - mix compile --warnings-as-errors 12 | script: 13 | - mix coveralls.travis 14 | - travis_wait 50 mix dialyzer --halt-exit-status 15 | - mix credo --strict 16 | after_script: 17 | - mix deps.get --only docs 18 | - MIX_ENV=docs mix inch.report 19 | cache: 20 | directories: 21 | - _build 22 | - deps 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/antipax/tqdm_elixir.svg?branch=master)](https://travis-ci.org/antipax/tqdm_elixir) [![Coverage Status](https://coveralls.io/repos/github/antipax/tqdm_elixir/badge.svg?branch=master)](https://coveralls.io/github/antipax/tqdm_elixir?branch=master) [![Inline docs](http://inch-ci.org/github/antipax/tqdm_elixir.svg?branch=master)](http://inch-ci.org/github/antipax/tqdm_elixir) [![Hex.pm package version](https://img.shields.io/hexpm/v/tqdm.svg)](https://hex.pm/packages/tqdm) [![Hex.pm package license](https://img.shields.io/hexpm/l/tqdm.svg)](https://github.com/antipax/tqdm_elixir/blob/master/LICENSE) 2 | 3 | # Tqdm 4 | 5 | Tqdm easily adds a CLI progress bar to any enumerable. 6 | 7 | ![tqdm](http://i.imgur.com/D4ZILgE.gif) 8 | 9 | A (partial) port of Python's [tqdm](https://github.com/tqdm/tqdm) to Elixir. Thanks noamraph and all other contributors for the original library! 10 | 11 | Just wrap Lists, Maps, Streams, or anything else that implements Enumerable with `Tqdm.tqdm`: 12 | 13 | ```elixir 14 | for _ <- Tqdm.tqdm(1..1000) do 15 | :timer.sleep(10) 16 | end 17 | 18 | # or 19 | 20 | 1..1000 21 | |> Tqdm.tqdm() 22 | |> Enum.map(fn _ -> :timer.sleep(10) end) 23 | 24 | # or even... 25 | 26 | 1..1000 27 | |> Stream.map(fn _ -> :timer.sleep(10) end) 28 | |> Tqdm.tqdm(total: 1000) 29 | |> Stream.run() 30 | 31 | # |###-------| 392/1000 39.0% [elapsed: 00:00:04.627479 left: 00:00:07, 84.71 iters/sec] 32 | ``` 33 | 34 | Full documentation can be found [here](https://hexdocs.pm/tqdm/0.0.2). 35 | 36 | ## Installation 37 | 38 | 1. Add tqdm to your list of dependencies in `mix.exs`: 39 | 40 | ```elixir 41 | def deps do 42 | [{:tqdm, "~> 0.0.2"}] 43 | end 44 | ``` 45 | 46 | 2. Ensure tqdm is added to your list of applications: 47 | 48 | ```elixir 49 | def application do 50 | [applications: [:tqdm]] 51 | end 52 | ``` 53 | -------------------------------------------------------------------------------- /lib/tqdm.ex: -------------------------------------------------------------------------------- 1 | defmodule Tqdm do 2 | @moduledoc """ 3 | Tqdm easily adds a CLI progress bar to any enumerable. 4 | 5 | Just wrap Lists, Maps, Streams, or anything else that implements Enumerable 6 | with `Tqdm.tqdm`: 7 | 8 | for _ <- Tqdm.tqdm(1..1000) do 9 | :timer.sleep(10) 10 | end 11 | 12 | # or 13 | 14 | 1..1000 15 | |> Tqdm.tqdm() 16 | |> Enum.map(fn _ -> :timer.sleep(10) end) 17 | 18 | # or even... 19 | 20 | 1..1000 21 | |> Stream.map(fn _ -> :timer.sleep(10) end) 22 | |> Tqdm.tqdm(total: 1000) 23 | |> Stream.run() 24 | 25 | # |###-------| 392/1000 39.0% [elapsed: 00:00:04.627479 \ 26 | left: 00:00:07, 84.71 iters/sec] 27 | """ 28 | 29 | @type option :: 30 | {:description, String.t} | 31 | {:total, non_neg_integer} | 32 | {:clear, boolean} | 33 | {:device, IO.device} | 34 | {:min_interval, non_neg_integer} | 35 | {:min_iterations, non_neg_integer} | 36 | {:total_segments, non_neg_integer} 37 | 38 | @type options :: [option] 39 | 40 | @doc """ 41 | Wrap the given `enumerable` and print a CLI progress bar. 42 | 43 | `options` may be provided: 44 | 45 | * `:description` - a short string that is displayed on the progress bar. 46 | For example, if the string `"Processing values"` is provided for this 47 | option: 48 | 49 | # Processing values: |###-------| 349/1000 35.0% [elapsed: \ 50 | 00:00:06.501472 left: 00:00:12, 53.68 iters/sec] 51 | 52 | * `:total` - by default, `Tdqm` will use `Enum.count` to count how many 53 | elements are in the given `enumerable`. For large amounts of data, or 54 | streams, this may not be appropriate. You can provide your own total with 55 | this option. You may provide an estimate, and if the actual count 56 | exceeds this value, the progress bar will change to an indeterminate mode: 57 | 58 | # 296 [elapsed: 00:00:03.500038, 84.57 iters/sec] 59 | 60 | You can also force the indeterminate mode by passing `0`. 61 | 62 | * `:clear` - by default, `Tqdm` will clear the progress bar after the 63 | enumeration is complete. If you pass `false` for this option, the progress 64 | bar will persist, instead. 65 | 66 | * `:device` - by default, `Tqdm` writes to `:stderr`. You can provide any 67 | `IO.device` to this option to use it instead of the default. 68 | 69 | * `:min_interval` - by default, `Tqdm` will only print progress updates 70 | every 100ms. You can increase or decrease this value using this option. 71 | 72 | * `:min_iterations` - by default, `Tqdm` will check if the `:min_interval` 73 | has passed for every iteration. Passing a value for this option will skip 74 | this check until at least `:min_iterations` iterations have passed. 75 | 76 | * `:total_segments` - by default, `Tqdm` will split its progress bar into 10 77 | segments. You can customize this by passing a different value for this 78 | option. 79 | """ 80 | @spec tqdm(Enumerable.t, options) :: Enumerable.t 81 | def tqdm(enumerable, options \\ []) do 82 | start_fun = fn -> 83 | now = System.monotonic_time() 84 | 85 | get_total = fn -> Enum.count(enumerable) end 86 | 87 | %{ 88 | n: 0, 89 | last_print_n: 0, 90 | start_time: now, 91 | last_print_time: now, 92 | last_printed_length: 0, 93 | prefix: options |> Keyword.get(:description, "") |> prefix(), 94 | total: Keyword.get_lazy(options, :total, get_total), 95 | clear: Keyword.get(options, :clear, true), 96 | device: Keyword.get(options, :device, :stderr), 97 | min_interval: 98 | options 99 | |> Keyword.get(:min_interval, 100) 100 | |> System.convert_time_unit(:milliseconds, :native), 101 | min_iterations: Keyword.get(options, :min_iterations, 1), 102 | total_segments: Keyword.get(options, :total_segments, 10) 103 | } 104 | end 105 | 106 | Stream.transform(enumerable, start_fun, &do_tqdm/2, &do_tqdm_after/1) 107 | end 108 | 109 | defp prefix(""), do: "" 110 | defp prefix(description), do: description <> ": " 111 | 112 | defp do_tqdm(element, %{n: 0} = state) do 113 | {[element], %{print_status(state, System.monotonic_time()) | n: 1}} 114 | end 115 | 116 | defp do_tqdm( 117 | element, 118 | %{n: n, last_print_n: last_print_n, min_iterations: min_iterations} = state 119 | ) when n - last_print_n < min_iterations, 120 | do: {[element], %{state | n: n + 1}} 121 | 122 | defp do_tqdm(element, state) do 123 | now = System.monotonic_time() 124 | 125 | time_diff = 126 | now - state.last_print_time 127 | 128 | state = 129 | if time_diff >= state.min_interval do 130 | Map.merge(print_status(state, now), %{ 131 | last_print_n: state.n, 132 | last_print_time: System.monotonic_time() 133 | }) 134 | else 135 | state 136 | end 137 | 138 | {[element], %{state | n: state.n + 1}} 139 | end 140 | 141 | defp do_tqdm_after(state) do 142 | state = print_status(state, System.monotonic_time()) 143 | 144 | finish = 145 | if state.clear do 146 | prefix_length = String.length(state.prefix) 147 | total_bar_chars = prefix_length + state.last_printed_length 148 | 149 | "\r" <> String.duplicate(" ", total_bar_chars) <> "\r" 150 | else 151 | "\n" 152 | end 153 | 154 | IO.write(state.device, finish) 155 | end 156 | 157 | defp print_status(state, now) do 158 | status = format_status(state, now) 159 | status_length = String.length(status) 160 | 161 | num_padding_chars = max(state.last_printed_length - status_length, 0) 162 | padding = String.duplicate(" ", num_padding_chars) 163 | 164 | IO.write(state.device, "\r#{state.prefix}#{status}#{padding}") 165 | 166 | %{state | last_printed_length: status_length} 167 | end 168 | 169 | defp format_status(state, now) do 170 | elapsed = 171 | System.convert_time_unit(now - state.start_time, :native, :microseconds) 172 | 173 | elapsed_str = format_interval(elapsed, false) 174 | 175 | rate = format_rate(elapsed, state.n) 176 | 177 | format_status(state, elapsed, rate, elapsed_str) 178 | end 179 | 180 | defp format_status(state, elapsed, rate, elapsed_str) do 181 | n = state.n 182 | total = state.total 183 | total_segments = state.total_segments 184 | 185 | if n <= total and total != 0 do 186 | progress = n / total 187 | 188 | num_segments = trunc(progress * total_segments) 189 | 190 | bar = format_bar(num_segments, total_segments) 191 | 192 | percentage = "#{Float.round(progress * 100)}%" 193 | 194 | left = format_left(n, elapsed, total) 195 | 196 | "|#{bar}| #{n}/#{total} #{percentage} " <> 197 | "[elapsed: #{elapsed_str} left: #{left}, #{rate} iters/sec]" 198 | else 199 | "#{n} [elapsed: #{elapsed_str}, #{rate} iters/sec]" 200 | end 201 | end 202 | 203 | defp format_rate(elapsed, n) when elapsed > 0, 204 | do: Float.round(n / (elapsed / 1_000_000), 2) 205 | defp format_rate(_elapsed, _n), 206 | do: "?" 207 | 208 | defp format_bar(num_segments, total_segments) do 209 | String.duplicate("#", num_segments) <> 210 | String.duplicate("-", total_segments - num_segments) 211 | end 212 | 213 | defp format_left(n, elapsed, total) when n > 0, 214 | do: format_interval(elapsed / n * (total - n), true) 215 | defp format_left(_n, _elapsed, _total), 216 | do: "?" 217 | 218 | defp format_interval(elapsed, trunc_seconds) do 219 | minutes = trunc(elapsed / 60_000_000) 220 | hours = div(minutes, 60) 221 | rem_minutes = minutes - hours * 60 222 | micro_seconds = elapsed - minutes * 60_000_000 223 | seconds = micro_seconds / 1_000_000 224 | 225 | seconds = if trunc_seconds, do: trunc(seconds), else: seconds 226 | 227 | hours_str = format_time_component(hours) 228 | minutes_str = format_time_component(rem_minutes) 229 | seconds_str = format_time_component(seconds) 230 | 231 | "#{hours_str}:#{minutes_str}:#{seconds_str}" 232 | end 233 | 234 | defp format_time_component(time) when time < 10, 235 | do: "0#{time}" 236 | defp format_time_component(time), 237 | do: to_string(time) 238 | end 239 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Tqdm.Mixfile do 2 | use Mix.Project 3 | 4 | @version "0.0.3" 5 | 6 | def project do 7 | [ 8 | app: :tqdm, 9 | version: @version, 10 | elixir: "~> 1.3", 11 | build_embedded: Mix.env == :prod, 12 | start_permanent: Mix.env == :prod, 13 | deps: deps(), 14 | description: description(), 15 | package: package(), 16 | source_url: "https://github.com/antipax/tqdm_elixir", 17 | docs: [ 18 | main: "Tqdm", 19 | extras: ["README.md"], 20 | source_ref: "v#{@version}" 21 | ], 22 | test_coverage: [tool: ExCoveralls], 23 | preferred_cli_env: ["coveralls": :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test], 24 | ] 25 | end 26 | 27 | defp deps do 28 | [ 29 | {:credo, "~> 0.3", only: [:dev, :test]}, 30 | {:dialyxir, "~> 0.3", only: [:dev, :test]}, 31 | {:earmark, "~> 1.2", only: :dev}, 32 | {:ex_doc, "~> 0.18", only: :dev}, 33 | {:excoveralls, "~> 0.4", only: :test}, 34 | {:inch_ex, ">= 0.0.0", only: :docs} 35 | ] 36 | end 37 | 38 | defp description do 39 | "Add a progress bar to your enumerables (Lists, Maps, Streams, Ranges, etc.) in a second." 40 | end 41 | 42 | defp package do 43 | [ 44 | files: ["lib", "mix.exs", "README.md", "LICENSE"], 45 | maintainers: ["Eric Entin"], 46 | licenses: ["Apache 2.0"], 47 | links: %{ 48 | "GitHub" => "https://github.com/antipax/tqdm_elixir" 49 | } 50 | ] 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, 2 | "certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], [], "hexpm"}, 3 | "credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"}, 4 | "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"}, 5 | "earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [:mix], [], "hexpm"}, 6 | "ex_doc": {:hex, :ex_doc, "0.18.1", "37c69d2ef62f24928c1f4fdc7c724ea04aecfdf500c4329185f8e3649c915baf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, 7 | "excoveralls": {:hex, :excoveralls, "0.8.0", "99d2691d3edf8612f128be3f9869c4d44b91c67cec92186ce49470ae7a7404cf", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, 8 | "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"}, 9 | "hackney": {:hex, :hackney, "1.10.1", "c38d0ca52ea80254936a32c45bb7eb414e7a96a521b4ce76d00a69753b157f21", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, 10 | "idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, 11 | "inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, 12 | "jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm"}, 13 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, 14 | "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"}, 15 | "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, 16 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"}, 17 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"}} 18 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /test/tqdm_test.exs: -------------------------------------------------------------------------------- 1 | defmodule TqdmTest do 2 | use ExUnit.Case 3 | import ExUnit.CaptureIO 4 | 5 | 6 | test "tqdm for" do 7 | assert_start_and_end "for: ", fn -> 8 | for _ <- Tqdm.tqdm(1..100, description: "for") do 9 | :timer.sleep(10) 10 | end 11 | end 12 | end 13 | 14 | test "tdqm enum" do 15 | assert_start_and_end ~S"Enum\.map: ", fn -> 16 | 1..100 17 | |> Tqdm.tqdm(description: "Enum.map") 18 | |> Enum.map(fn _ -> :timer.sleep(10) end) 19 | end 20 | end 21 | 22 | test "tqdm stream" do 23 | assert_start_and_end ~S"Stream\.map: ", fn -> 24 | 1..100 25 | |> Stream.map(fn _ -> :timer.sleep(10) end) 26 | |> Tqdm.tqdm(description: "Stream.map") 27 | |> Stream.run() 28 | end 29 | end 30 | 31 | test "tqdm optionless" do 32 | assert_start_and_end "", fn -> 33 | 1..100 34 | |> Stream.map(fn _ -> :timer.sleep(10) end) 35 | |> Tqdm.tqdm() 36 | |> Stream.run() 37 | end 38 | end 39 | 40 | test "tqdm indeterminate" do 41 | fun = fn -> 42 | 1..100 43 | |> Stream.map(fn _ -> :timer.sleep(10) end) 44 | |> Tqdm.tqdm(total: 0) 45 | |> Stream.run() 46 | end 47 | 48 | capture = capture_io(:stderr, fun) 49 | 50 | assert capture =~ ~r/0 \[elapsed: .*, 0\.0 iters\/sec]/ 51 | assert capture =~ ~r/100 \[elapsed: .*, .* iters\/sec]/ 52 | end 53 | 54 | defp assert_start_and_end(description, fun) do 55 | capture = capture_io(:stderr, fun) 56 | 57 | assert capture =~ start_regex_for_description(description) 58 | assert capture =~ end_regex_for_description(description) 59 | end 60 | 61 | defp start_regex_for_description(description) do 62 | ~r/#{description}\|----------\| 0\/100 0\.0% \[elapsed: .* left: \?, 0\.0 iters\/sec]/ 63 | end 64 | 65 | defp end_regex_for_description(description) do 66 | ~r/#{description}\|##########\| 100\/100 100\.0% \[elapsed: .* left: 00:00:00, .* iters\/sec]/ 67 | end 68 | end 69 | --------------------------------------------------------------------------------