summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2012-08-15 03:31:34 +0200
committerhut <hut@lavabit.com>2012-08-15 03:36:14 +0200
commit3168005e169b64633f8142ff2ccbbae15d44da2a (patch)
tree2aba61dd83c53184274d7e49faad034f0e8f350b /ranger
parent23fed83c9afe1938261360cf10efc1bb60f9e5d5 (diff)
downloadranger-3168005e169b64633f8142ff2ccbbae15d44da2a.tar.gz
core.loader: implemented progressbar for CopyLoader
Diffstat (limited to 'ranger')
-rw-r--r--ranger/core/loader.py26
-rw-r--r--ranger/ext/shutil_generatorized.py5
2 files changed, 28 insertions, 3 deletions
diff --git a/ranger/core/loader.py b/ranger/core/loader.py
index 58f23821..f1be697a 100644
--- a/ranger/core/loader.py
+++ b/ranger/core/loader.py
@@ -6,6 +6,7 @@ from time import time, sleep
 from subprocess import Popen, PIPE
 from ranger.core.shared import FileManagerAware
 from ranger.ext.signals import SignalDispatcher
+import math
 import os.path
 import sys
 import select
@@ -50,11 +51,31 @@ class CopyLoader(Loadable, FileManagerAware):
 		self.percent = 0
 		if self.copy_buffer:
 			self.one_file = self.copy_buffer[0]
-		Loadable.__init__(self, self.generate(), 'copying/moving...')
+		Loadable.__init__(self, self.generate(), 'Calculating size...')
+
+	def _calculate_size(self, step):
+		from os.path import join
+		size = 0
+		stack = [f.path for f in self.copy_buffer]
+		while stack:
+			fname = stack.pop()
+			if os.path.isdir(fname):
+				stack.extend([join(fname, item) for item in os.listdir(fname)])
+			else:
+				try:
+					fstat = os.stat(fname)
+				except:
+					continue
+				size += max(step, math.ceil(fstat.st_size / step) * step)
+		return size
 
 	def generate(self):
 		from ranger.ext import shutil_generatorized as shutil_g
 		if self.copy_buffer:
+			# TODO: Don't calculate size when renaming (needs detection)
+			bytes_per_tick = shutil_g.BLOCK_SIZE
+			size = max(1, self._calculate_size(bytes_per_tick))
+			bar_tick = 100.0 / (float(size) / bytes_per_tick)
 			if self.do_cut:
 				self.original_copy_buffer.clear()
 				if len(self.copy_buffer) == 1:
@@ -65,6 +86,7 @@ class CopyLoader(Loadable, FileManagerAware):
 					for _ in shutil_g.move(src=f.path,
 							dst=self.original_path,
 							overwrite=self.overwrite):
+						self.percent += bar_tick
 						yield
 			else:
 				if len(self.copy_buffer) == 1:
@@ -77,11 +99,13 @@ class CopyLoader(Loadable, FileManagerAware):
 								dst=os.path.join(self.original_path, f.basename),
 								symlinks=True,
 								overwrite=self.overwrite):
+							self.percent += bar_tick
 							yield
 					else:
 						for _ in shutil_g.copy2(f.path, self.original_path,
 								symlinks=True,
 								overwrite=self.overwrite):
+							self.percent += bar_tick
 							yield
 			cwd = self.fm.get_directory(self.original_path)
 			cwd.load_content()
diff --git a/ranger/ext/shutil_generatorized.py b/ranger/ext/shutil_generatorized.py
index 436d2cb7..72b8dc26 100644
--- a/ranger/ext/shutil_generatorized.py
+++ b/ranger/ext/shutil_generatorized.py
@@ -11,10 +11,11 @@ import sys
 import stat
 from os.path import abspath
 
-__all__ = ["copyfileobj","copyfile","copystat","copy2",
+__all__ = ["copyfileobj","copyfile","copystat","copy2","BLOCK_SIZE",
            "copytree","move","rmtree","Error", "SpecialFileError"]
 
 APPENDIX = '_'
+BLOCK_SIZE = 16 * 1024
 
 class Error(EnvironmentError):
     pass
@@ -28,7 +29,7 @@ try:
 except NameError:
     WindowsError = None
 
-def copyfileobj(fsrc, fdst, length=16*1024):
+def copyfileobj(fsrc, fdst, length=BLOCK_SIZE):
     """copy data from file-like object fsrc to file-like object fdst"""
     while 1:
         buf = fsrc.read(length)
='n291' href='#n291'>291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670