summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-15 15:01:07 +0100
committerhut <hut@lavabit.com>2009-12-15 15:01:07 +0100
commit284af717cada11848ea6c82c0485f761adbab655 (patch)
tree34bf9621274e6bf6d7aa0f58cefd77c9c4458f2a
parente65cebe473fccf7af0ef646164d8c1dfa62e3ac3 (diff)
downloadranger-284af717cada11848ea6c82c0485f761adbab655.tar.gz
turned different console types into seperate subclasses
-rw-r--r--ranger/gui/widgets/console.py82
1 files changed, 46 insertions, 36 deletions
diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py
index f4b278c9..cf0cc717 100644
--- a/ranger/gui/widgets/console.py
+++ b/ranger/gui/widgets/console.py
@@ -2,11 +2,18 @@
 commands, searching and executing files."""
 from . import Widget
 import curses
+from ranger import log
 
 CONSOLE_MODES = tuple(':@/?>!')
 CONSOLE_PROMPTS = { '@': 'open with: ' }
 
 class Console(Widget):
+	mode = None
+	visible = False
+	commandlist = None
+	last_cursor_mode = 1
+	prompt = ':'
+
 	def __init__(self, win):
 		from ranger.container import CommandList
 		Widget.__init__(self, win)
@@ -16,15 +23,7 @@ class Console(Widget):
 		self.settings.keys.initialize_console_commands(self.commandlist)
 		self.last_cursor_mode = 1
 		self.clear()
-		self.prompt = None
-		self.execute_funcs = {
-				':': Console.execute_command,
-				'@': Console.execute_openwith_quick,
-				'/': Console.execute_search,
-				'?': Console.execute_search,
-				'>': Console.execute_noreturn,
-				'!': Console.execute_openwith }
-	
+
 	def feed_env(self, env):
 		self.cf = env.cf
 
@@ -45,10 +44,9 @@ class Console(Widget):
 
 		self.last_cursor_mode = curses.curs_set(1)
 		self.mode = mode
-		try:
-			self.prompt = CONSOLE_PROMPTS[self.mode]
-		except KeyError:
-			self.prompt = self.mode
+		log(self.__class__)
+		self.__class__ = self.mode_classes[mode]
+		log(self.__class__)
 		self.focused = True
 		self.visible = True
 		return True
@@ -56,6 +54,7 @@ class Console(Widget):
 	def close(self):
 		curses.curs_set(self.last_cursor_mode)
 		self.clear()
+		self.__class__ = Console
 		self.focused = False
 		self.visible = False
 		if hasattr(self, 'on_close'):
@@ -125,33 +124,42 @@ class Console(Widget):
 		self.move(relative = mod)
 
 	def execute(self):
-		try:
-			self.execute_funcs[self.mode] (self)
-		except KeyError:
-			pass
+		log("aww")
 		self.line = ''
 		self.pos = 0
 		self.close()
 
-	def execute_search(self):
+class CommandConsole(Console):
+	prompt = ':'
+
+class QuickCommandConsole(Console):
+	prompt = '>'
+
+class SearchConsole(Console):
+	prompt = '/'
+	def execute(self):
+		log("yay")
 		import re
 		if self.fm.env.pwd:
-#			try:
-				regexp = re.compile(self.line, re.L | re.U | re.I)
-				self.fm.env.last_search = regexp
-				if self.fm.env.pwd.search(regexp):
-					self.fm.env.cf = self.fm.env.pwd.pointed_file
-#			except:
-#				pass
-
-	def execute_openwith(self):
+			regexp = re.compile(self.line, re.L | re.U | re.I)
+			self.fm.env.last_search = regexp
+			if self.fm.env.pwd.search(regexp):
+				self.fm.env.cf = self.fm.env.pwd.pointed_file
+		Console.execute(self)
+
+class OpenConsole(Console):
+	prompt = '!'
+	def execute(self):
 		line = self.line
 		if line[0] == '!':
 			self.fm.execute_file(tuple(line[1:].split()) + (self.fm.env.cf.path, ))
 		else:
 			self.fm.execute_file(tuple(line.split()) + (self.fm.env.cf.path, ), background = True)
+		Console.execute(self)
 
-	def execute_openwith_quick(self):
+class QuickOpenConsole(Console):
+	prompt = 'open with: '
+	def execute(self):
 		split = self.line.split()
 		app, flags, mode = get_app_flags_mode(self.line, self.fm)
 		self.fm.execute_file(
@@ -159,12 +167,7 @@ class Console(Widget):
 				app = app,
 				flags = flags,
 				mode = mode )
-
-	def execute_noreturn(self):
-		pass
-
-	def execute_command(self):
-		pass
+		Console.execute(self)
 
 def get_app_flags_mode(line, fm):
 	""" extracts the application, flags and mode from a string entered into the "openwith_quick" console. """
@@ -236,6 +239,15 @@ def get_app_flags_mode(line, fm):
 
 	return app, flags, int(mode)
 
+Console.mode_classes = {
+		':': CommandConsole,
+		'>': QuickCommandConsole,
+		'!': OpenConsole,
+		'@': QuickOpenConsole,
+		'/': SearchConsole,
+		'?': SearchConsole,
+}
+
 def is_app(arg, fm):
 	return fm.apps.has(arg)
 
@@ -245,5 +257,3 @@ def is_flags(arg):
 
 def is_mode(arg):
 	return all(x in '0123456789' for x in arg)
-
-
a id='n343' href='#n343'>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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880