Module: Dister::Utils
- Defined in:
- lib/dister/utils.rb
Overview
Shared utility methods
Constant Summary collapse
- GIGA_SIZE =
1073741824.0
- MEGA_SIZE =
1048576.0
- KILO_SIZE =
1024.0
Class Method Summary collapse
-
.execute_printing_progress(message)
Shows message and prints a dot per second until the block code terminates its execution.
-
.readable_file_size(size, precision) ⇒ String
Return the file size with a readable style.
Class Method Details
.execute_printing_progress(message)
Shows message and prints a dot per second until the block code terminates its execution. Exceptions raised by the block are displayed and program exists with error status 1.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dister/utils.rb', line 12 def execute_printing_progress t = Thread.new do print "#{}" while(true) do print "." STDOUT.flush sleep 1 end end shell = Thor::Shell::Color.new begin ret = yield t.kill if t.alive? shell.say_status "[DONE]", "", :GREEN return ret rescue t.kill if t.alive? shell.say_status "[ERROR]", $!, :RED exit 1 end end |
.readable_file_size(size, precision) ⇒ String
Return the file size with a readable style.
42 43 44 45 46 47 48 49 50 |
# File 'lib/dister/utils.rb', line 42 def readable_file_size(size, precision) case when size == 1 then "1 Byte" when size < KILO_SIZE then "%d Bytes" % size when size < MEGA_SIZE then "%.#{precision}f KB" % (size / KILO_SIZE) when size < GIGA_SIZE then "%.#{precision}f MB" % (size / MEGA_SIZE) else "%.#{precision}f GB" % (size / GIGA_SIZE) end end |