Okomikeruko / terminal-progress

Terminal Progress is a progress bar manager for Ruby applications. It provides a simple way to display progress updates in the terminal, making it ideal for tasks like seed data generation, file processing, or any operation where progress tracking is ideal.
MIT License
0 stars 0 forks source link

Interesting...but -- needs more documentation I think #1

Open notjames opened 4 months ago

notjames commented 4 months ago

Assuming that you want the public to use this tool...

I'm considering using it and I've already started a quick implementation. My use-case is to show a progress bar for downloading a file. My problem is the following:

(0..100).each do |i|
  progress.print_progress(...)
  sleep 0.1
end

my read of this is that there is a manual sequential counter starting at 0 and incrementing to 100, but it doesn't make sense to me what real-world application this has as it's not tied to any objective purpose. It's just going to count from 0 to 100 with a sleep in the middle of .1 seconds.

This is not a real-world applicable use-case. I take it that the 0 to 100 sequence might be arbitrary in terms of the example? I decided to try using the file size (0 to size-of-file) as the sequence, but that doesn't work. In fact, the following code in an attempt to get this to work as expected failed:

...
...
...
      @files.each_slice(2) do |slice|
        slice.each do |file|
          title     = file[:Name]
          filepath  = format('%s/%s', @options[:'dl-path'], title)
          fin_size  = file[:Size]
          now_size  = File.size?(filepath) || 0

          touch(filepath)
          progress[title] = TerminalProgress.new(fin_size)

          threads[title] = Thread.new do
            @dl_client.do_download(get_link, @options, format('%s.%s', title, v_ext))
          end

          puts format('Downloading: %s (%d/%d)', title, now_size, fin_size)
          loop do
            now_size = File.size?(filepath) || 0
            progress[title].print_progress
            break if now_size >= fin_size
            sleep 0.1
          end
        end

        threads_wait = ThreadsWait.new(*threads)
        threads_wait.all_waits do |thr|
          t_name = thr.to_s
          progress[t_name].print_complete
        end
      end

all this does is produce a counter starting from zero incrementing every .1 seconds despite the fact that I'm pulling the size of the file. I'm assuming due to lack of documentation, I'm just missing something. I've briefly looked at the code but I need to look at it more in depth I believe.

Okomikeruko commented 4 months ago

I specifically used this tool to help me track the progress of my rails db:seed command by creating a max element counter to replace the 100 and different incrementing commands throughout the db/seeds.rb to track how far along it had progressed.

I extracted it as this gem to use it in my other projects.

Yes, you're right about it needing more documentation and explaining.