summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-04 19:59:41 +0100
committerhut <hut@lavabit.com>2009-12-04 19:59:41 +0100
commitf69105521406e581344088103623652d23c51933 (patch)
treeba3b971a36b153f354028743e283c9b749974715
parent6874e0ea3059b0f394ca9cf5929cb82c3fe4a09d (diff)
downloadranger-f69105521406e581344088103623652d23c51933.tar.gz
improve bookmarks (delete, reload_if_outdated)
-rw-r--r--ranger/bookmark.py72
-rw-r--r--ranger/conf/keys.py1
-rw-r--r--ranger/fm.py4
3 files changed, 72 insertions, 5 deletions
diff --git a/ranger/bookmark.py b/ranger/bookmark.py
index e9bfcee1..4b931292 100644
--- a/ranger/bookmark.py
+++ b/ranger/bookmark.py
@@ -12,20 +12,81 @@ class Bookmarks(object):
 			self.path = os.path.expanduser("~/.ranger/bookmarks")
 		self.load_pattern = re.compile(r"^[\d\w`']:.")
 		self.enter_dir_function = None
+		self.last_mtime = None
 
-	def load(self):
+	def load_dict(self):
 		import os
-		self.dct.clear()
-
+		dct = {}
 		if os.access(self.path, os.R_OK):
 			f = open(self.path, 'r')
 			for line in f:
 				if self.load_pattern.match(line):
 					key, value = line[0], line[2:-1]
 					if key in ALLOWED_KEYS: 
-						self.dct[key] = value
-
+						dct[key] = value
 			f.close()
+			return dct
+		else:
+			raise OSError('Cannot read the given path')
+	
+	def set_dict(self, dct):
+		self.dct.clear()
+		self.dct.update(dct)
+		self.original_dict = self.dct.copy()
+		self.last_mtime = self.get_mtime()
+
+	def get_mtime(self):
+		import os
+		return os.stat(self.path).st_mtime
+
+	def load(self):
+		try:
+			new_dict = self.load_dict()
+		except OSError:
+			return
+
+		self.set_dict(new_dict)
+	
+	def delete(self, key):
+		if key in self.dct:
+			del self.dct[key]
+			self.save()
+
+	def update(self):
+		try:
+			real_dict = self.load_dict()
+		except OSError:
+			return
+
+		for key in set(self.dct.keys()) | set(real_dict.keys()):
+			if key in self.dct:
+				current = self.dct[key]
+			else:
+				current = None
+			
+			if key in self.original_dict:
+				original = self.original_dict[key]
+			else:
+				original = None
+				
+			if key in real_dict:
+				real = real_dict[key]
+			else:
+				real = None
+
+			if current == original and current != real:
+				continue
+
+			if key not in self.dct:
+				del real_dict[key]
+			else:
+				real_dict[key] = current
+
+		self.set_dict(real_dict)
+
+	def reload_if_outdated(self):
+		if self.last_mtime != self.get_mtime():
+			self.update()
 
 	def enter(self, key):
 		if self.enter_dir_function is not None:
@@ -53,6 +114,7 @@ class Bookmarks(object):
 
 	def save(self):
 		import os
+		self.update()
 		if os.access(self.path, os.W_OK):
 			f = open(self.path, 'w')
 
diff --git a/ranger/conf/keys.py b/ranger/conf/keys.py
index 9a9bcff3..a800253b 100644
--- a/ranger/conf/keys.py
+++ b/ranger/conf/keys.py
@@ -59,6 +59,7 @@ def initialize_commands(cl):
 	for key in ALLOWED_BOOKMARK_KEYS:
 		cl.bind(c(FM.enter_bookmark, key),   "`" + key, "'" + key)
 		cl.bind(c(FM.set_bookmark, key),     "m" + key)
+		cl.bind(c(FM.unset_bookmark, key),   "um" + key)
 
 	# system functions
 	cl.bind(FM.exit,         ctrl('D'), 'q', 'ZZ')
diff --git a/ranger/fm.py b/ranger/fm.py
index 3f36045d..0cbebea2 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -15,6 +15,7 @@ class FM():
 
 		while True:
 			try:
+				self.bookmarks.reload_if_outdated()
 				self.ui.draw()
 				key = self.ui.get_next_key()
 				self.ui.press(key, self)
@@ -54,6 +55,9 @@ class FM():
 	def set_bookmark(self, key):
 		self.bookmarks[key] = self.env.pwd.path
 
+	def unset_bookmark(self, key):
+		self.bookmarks.delete(key)
+
 	def move_left(self):
 		self.env.enter_dir('..')
 	
1' href='#n321'>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