Giter Site home page Giter Site logo

suru's Introduction

suru

A tqdm-style progress bar in Nim

asciicast

the demo above uses this code (note that the api has since changed, refer to tests/tests.nim for updated code):

import unittest, os, sequtils, random
randomize()

test "random time test":
  for a in suru(toSeq(0..<100)):
    sleep((rand(99) + 1))

test "long time test":
  for a in suru([1, 2, 3, 5]):
    sleep(1000)

test "alternate long time test":
  sleep 1000
  var bar: SuruBar = initSuruBar(25)

  bar.start(4)

  for a in toSeq(1..1000):
    sleep 4
    if a mod 250 == 0:
      inc bar
    bar.update(50_000_000)

  bar.finish()

test "constant time test":
  for a in suru(toSeq(0..<100)):
    sleep(25)

test "v-shaped time test":
  for a in suru(toSeq(1..100) & toSeq(countdown(100, 1))):
    sleep(a)

test "increasing time test":
  for a in suru(toSeq(1..100)):
    sleep(a)

test "sinusoidal time test":
  for a in suru(toSeq(1..100)):
    sleep(int(sin(a.float / 5) * 50 + 50))

test "multi-bar test":
  echo "check if this line is removed by the bars"
  sleep 1000
  var bar: SuruBar = initSuruBar(25, 25)

  bar.start(1000, 40)

  for a in toSeq(1..1000):
    sleep 25
    inc bar
    if a mod 25 == 0:
      inc bar, 1
    bar.update(50_000_000)
    bar.update(50_000_000, 1)

  bar.finish()

  echo "check if this line is removed by the bars"

Usage

suru can be used in two ways:

import suru

for a in suru([20, 90, 120]):
  # do something
  discard

or

import suru

var bar: SuruBar = initSuruBar()
# pass in a positive integer if you want to change how many bars there are

bar[0].total = 3 # number of iterations

bar.setup()

for a in [20, 90, 120]:
  # do something

  inc bar # can be changed to increment n amount at a time
  # will increment all bars
  # use inc bar[0] if you only want to increment the first bar

  bar.update(50_000_000) # in nanoseconds, so the delay is 50 ms
  # will be clamped to at least 1 ms

bar.finish()

API Reference

TODO :(

Major To-do

the order bears no meaning

  • thread-safe
  • multi-bar support
  • formatting support
    • ascii-only version
    • custom text
  • stable api
    • might come soon, i need more opinions on the current api
  • iterator support
  • unicode checks
  • echoing within the loop
  • recursive support for suru macro
    • ex: (this should work like manually making a two-bar SuruBar)
import suru

for a in suru(...):
  # do something
  for b in suru(...):
    # do another thing

Dependencies

suru has no external Nim dependencies

suru's People

Contributors

de-odex avatar disruptek avatar dxxb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

suru's Issues

Suru does not compile on nim 2.0

When trying to compile with nim 2.0 I get this error:

suru.nim(241, 14) Error: type mismatch
Expression: setup(sbc[].bar, iterableLengths)
  [1] sbc[].bar: SuruBar
  [2] iterableLengths: varargs[int]

Expected one of (first mismatch at [position]):
[1] proc setup(sbc: ptr SuruBarController; iterableLengths: varargs[int])
[2] proc setup(sb: var SuruBar)

Make SuruBarController available externally

Hi there!

I'm attempting to make use of the multithreaded features of this library, and I encountered an issue where I couldn't actually reference the type SuruBarController. That is, I'm passing in the following type to a thread I'm creating:

type ThreadData = ref object
    y*, xInc*, yInc*: float
    offsetY*: int
    opts*: GraphOpts
    imageAddr*: ptr Image
    bar*: ptr SuruBarController

Creating a thread like so:

    var bar = initSuruBarThreaded()
    bar[0].total = size

    if opts.showProgress:
        bar.setup()

...


        let data = ThreadData(
            offsetY: offsetY,
            y: y,
            xInc: xInc,
            yInc: yInc,
            opts: opts,
            imageAddr: imageAddr,
            bar: bar
        )
        threads[i].createThread(processRow, data)

But with the current iteration, this doesn't work because SuruBarController is inaccessible externally.

Was wondering if it were possible to change the definition from:
SuruBarController = object to SuruBarController* = object

Cheers!

Crashing randomly (nim 1.6.14)

Sometimes getting a RangeDefect exception raised in progress= with stack trace:

C:\Users\ABC123\.nimble\pkgs\suru-0.3.1\suru.nim(131) inc
C:\Users\ABC123\.nimble\pkgs\suru-0.3.1\suru.nim(73) inc
C:\Users\ABC123\.nimble\pkgs\suru-0.3.1\suru.nim(54) progress=
C:\Users\ABC123\.choosenim\toolchains\nim-1.6.14\lib\system\fatal.nim(54) sysFatal
Error: unhandled exception: value out of range: 3551325900 notin -2147483648 .. 2147483647 [RangeDefect]

I'm not overly familiar with this codebase so I will not suggest any change but this should be an easy fix (maybe?) if I understand the issue right.

More tests

Perhaps the test suite is somewhat anaemic

Humanize file sizes

First of all, thanks for your library!

I use it to show the progress of downloading some big files (see https://github.com/rominf/nim-downloader). Currently, the progress bar looks like this:
image

It would be great to be able to make sizes human-readable like:
26.2 MiB/76.0 MiB

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.