From 6a5d09b8731414c6fd76213bd31f7b29b847a235 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 18 Dec 2009 01:20:43 +0100 Subject: added provisional filesystem operations (cp/cut/paste/delete/mkdir) --- TODO | 11 +++++--- ranger/actions.py | 59 +++++++++++++++++++++++++++++++++++++++++ ranger/commands.py | 14 ++++++++++ ranger/container/environment.py | 25 ++++++++++++----- ranger/defaults/keys.py | 4 +++ 5 files changed, 103 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index bd078d54..45d35acb 100644 --- a/TODO +++ b/TODO @@ -1,10 +1,10 @@ Console - ( ) #0 09/12/06 console commands - ( ) #1 09/12/06 quick find + (X) #0 09/12/06 console commands + (X) #1 09/12/06 quick find ( ) #2 09/12/06 open with ( ) #3 09/12/06 MVC for widgets - ( ) #4 09/12/06 history for console + (X) #4 09/12/06 history for console General @@ -12,3 +12,8 @@ General (X) #5 09/12/06 move code from fm into objects (X) #6 09/12/06 move main to __init__ ( ) #7 09/12/06 cooler titlebar + + +Filesystem Modification Operations + + ( ) #8 09/12/17 Add operations to modify files/directories diff --git a/ranger/actions.py b/ranger/actions.py index 41c7a6eb..d571b2aa 100644 --- a/ranger/actions.py +++ b/ranger/actions.py @@ -1,3 +1,6 @@ +import os +import shutil + from ranger.shared import EnvironmentAware, SettingsAware class Actions(EnvironmentAware, SettingsAware): @@ -133,6 +136,62 @@ class Actions(EnvironmentAware, SettingsAware): if isinstance(self.env.settings[string], bool): self.env.settings[string] ^= True +# ------------------------------------ filesystem operations + + def copy(self): + """Copy the selected items""" + + selected = set([self.env.cf]) + self.env.copy = set(f for f in selected if f in self.env.pwd.files) + self.env.cut = False + + def cut(self): + self.copy() + self.env.cut = True + + def paste(self): + """Paste the selected items into the current directory""" + from os.path import join, isdir + copied_files = self.env.copy + + if self.env.cut: + for f in self.env.copy: + try: + shutil.move(f.path, self.env.pwd.path) + except shutil.Error: + pass + self.env.copy.clear() + self.env.cut = False + else: + for f in self.env.copy: + if isdir(f.path): + try: + shutil.copytree(f.path, join(self.env.pwd.path, f.basename)) + except shutil.Error: + pass + else: + try: + shutil.copy(f.path, self.env.pwd.path) + except shutil.Error: + pass + + self.env.pwd.load_content() + + def delete(self): + selected = set([self.env.cf]) + self.env.copy -= selected + if selected: + for f in selected: + if os.path.isdir(f.path): + shutil.rmtree(f.path) + else: + try: + os.remove(f.path) + except OSError: + pass + + def mkdir(self, name): + os.mkdir(os.path.join(self.env.pwd.path, name)) # aliases: cd = enter_dir diff --git a/ranger/commands.py b/ranger/commands.py index 5b1060b6..06a4321b 100644 --- a/ranger/commands.py +++ b/ranger/commands.py @@ -169,6 +169,20 @@ class quit(Command): raise SystemExit +class delete(Command): + def execute(self): + self.fm.delete() + + +class mkdir(Command): + def execute(self): + line = parse(self.line) + try: + self.fm.mkdir(line.chunks[1]) + except IndexError: + pass + + # -------------------------------- rest by_name = {} diff --git a/ranger/container/environment.py b/ranger/container/environment.py index 40378221..7da8a608 100644 --- a/ranger/container/environment.py +++ b/ranger/container/environment.py @@ -4,18 +4,29 @@ from ranger.container import KeyBuffer, History from ranger.shared import SettingsAware class Environment(SettingsAware): - # A collection of data which is relevant for more than - # one class. + """A collection of data which is relevant for more than + one class. + """ + + pwd = None # current directory + cf = None # current file + copy = None + selection = None + termsize = None + history = None + directories = None + last_search = None + pathway = None + path = None + keybuffer = None + def __init__(self, path): self.path = abspath(expanduser(path)) self.pathway = () - self.last_search = None self.directories = {} - self.pwd = None # current directory - self.cf = None # current file self.keybuffer = KeyBuffer() - self.copy = None - self.termsize = None + self.selection = set() + self.copy = set() self.history = History(self.settings.max_history_size) from ranger.shared import EnvironmentAware diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py index 706e36e8..b300c4ad 100644 --- a/ranger/defaults/keys.py +++ b/ranger/defaults/keys.py @@ -36,6 +36,10 @@ def initialize_commands(command_list): bind('G', do('move_pointer', absolute = -1)) bind('E', do('edit_file')) + bind('yy', 'cp', do('copy')) + bind('cut', do('cut')) + bind('p', do('paste')) + bind('th', do('toggle_boolean_option', 'show_hidden')) bind('tp', do('toggle_boolean_option', 'preview_files')) bind('td', do('toggle_boolean_option', 'directories_first')) -- cgit 1.4.1-2-gfad0