From be090694e21fa541fb2fb1b499ca93ed8f8b3429 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Tue, 9 Nov 2010 11:13:45 +0100 Subject: [PATCH] xtmjoin: Progressbar & new naming pattern. * Added progressbar display using ANSI sequences ; * Handle a new naming pattern (.xtm.*) . --- bin/xtmjoin | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/bin/xtmjoin b/bin/xtmjoin index bde3f02..9f20c10 100755 --- a/bin/xtmjoin +++ b/bin/xtmjoin @@ -13,6 +13,7 @@ class XtmJoin attr_reader :opts + BUFFER_MAX_SIZE = 4096 * 4096 def initialize args @args = [] @@ -74,6 +75,7 @@ class XtmJoin cur_xtm = @input_filename is_first = true cur_size = header.filesize + print "\x1b[s" while cur_size > 0 do unless is_first then cur_xtm = _nextfile cur_xtm @@ -81,8 +83,15 @@ class XtmJoin in_xtm = File.open cur_xtm, "rb" end while cur_size > 0 and (not in_xtm.eof?) do - puts "Remaining : %s bytes" % cur_size - buffer = in_xtm.read cur_size + pcent = ((header.filesize - cur_size) * 100 / header.filesize) + + STDOUT.print "\x1b[uProgress : %s %" % pcent + STDOUT.flush + + read_size = if cur_size > BUFFER_MAX_SIZE then BUFFER_MAX_SIZE + else cur_size + end + buffer = in_xtm.read read_size cur_size = cur_size - buffer.length out_xtm.write buffer.slice(0, buffer.length) end @@ -90,21 +99,30 @@ class XtmJoin in_xtm.close end out_xtm.close + STDOUT.puts "" # remaining files end def _nextfile curfile + result = nil cur_idx = 0 cur_len = 0 - if curfile =~ /\.([0-9]+)\.xtm$/ then + case curfile + when /\.([0-9]+)\.xtm$/ then cur_idx = $1.to_i cur_len = $1.length + next_idx = cur_idx + 1 + result = curfile.gsub(/\.([0-9]+)\.xtm$/, ".%0#{cur_len}d.xtm" % next_idx) + when /\.xtm\.([0-9]+)$/ then + cur_idx = $1.to_i + cur_len = $1.length + next_idx = cur_idx + 1 + result = curfile.gsub(/\.xtm\.([0-9]+)$/, ".xtm.%0#{cur_len}d" % next_idx) + else + raise "Unable to detect a naming patterng for file sequence!" end - next_idx = cur_idx + 1 - - nf = curfile.gsub(/\.([0-9]+)\.xtm$/, ".%0#{cur_len}d.xtm" % next_idx) - return nf + return result end def self.main args