about summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorMilan Svoboda <msvoboda@ra.rockwell.com>2014-11-01 12:50:42 +0100
committerMilan Svoboda <msvoboda@ra.rockwell.com>2014-11-01 18:13:58 +0100
commit1c2a52c0ca265dcd1ef22b0b9f52c3f1bc8e9d80 (patch)
tree809acc69d8d8f6eaa6dfd43e7ce2d78d727a4218 /ranger
parent855ab51414f595a7398674e26973f803c7b56936 (diff)
downloadranger-1c2a52c0ca265dcd1ef22b0b9f52c3f1bc8e9d80.tar.gz
implement flat command
Diffstat (limited to 'ranger')
-rw-r--r--ranger/config/commands.py21
-rw-r--r--ranger/container/directory.py41
-rw-r--r--ranger/container/fsobject.py14
-rw-r--r--ranger/core/fm.py10
4 files changed, 70 insertions, 16 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 51bff937..2c5babba 100644
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -1261,3 +1261,24 @@ class log(Command):
 
         pager = os.environ.get('PAGER', ranger.DEFAULT_PAGER)
         self.fm.run([pager, tmp.name])
+
+class flat(Command):
+    """
+    :flat <level>
+
+    Flattens the directory view up to level specified.
+        -1 fully flattened
+        0  remove flattened view
+    """
+
+    def execute(self):
+        try:
+            level = self.rest(1)
+            level = int(level)
+        except ValueError:
+            self.fm.notify("Need an integer number (-1, 0, 1, ...)", bad=True)
+            return
+        self.fm.thisdir.unload()
+        self.fm.thisdir.flat = level
+        self.fm.thisdir.load_content()
+
diff --git a/ranger/container/directory.py b/ranger/container/directory.py
index de69c467..c6987a76 100644
--- a/ranger/container/directory.py
+++ b/ranger/container/directory.py
@@ -52,6 +52,7 @@ class Directory(FileSystemObject, Accumulator, Loadable):
     cycle_list = None
     loading = False
     progressbar_supported = True
+    flat = 0
 
     filenames = None
     files = None
@@ -182,7 +183,7 @@ class Directory(FileSystemObject, Accumulator, Loadable):
         self.move_to_obj(self.pointed_obj)
 
     # XXX: Check for possible race conditions
-    def load_bit_by_bit(self):
+    def load_bit_by_bit(self, flat=0):
         """An iterator that loads a part on every next() call
 
         Returns a generator which load a part of the directory
@@ -193,6 +194,18 @@ class Directory(FileSystemObject, Accumulator, Loadable):
         self.percent = 0
         self.load_if_outdated()
 
+        basename_is_rel = True if flat else False
+
+        def walklevel(some_dir, level):
+            some_dir = some_dir.rstrip(os.path.sep)
+            assert os.path.isdir(some_dir)
+            num_sep = some_dir.count(os.path.sep)
+            for root, dirs, files in os.walk(some_dir):
+                yield root, dirs, files
+                num_sep_this = root.count(os.path.sep)
+                if level != -1 and num_sep + level <= num_sep_this:
+                    del dirs[:]
+
         try:
             if self.runnable:
                 yield
@@ -200,7 +213,17 @@ class Directory(FileSystemObject, Accumulator, Loadable):
 
                 self.mount_path = mount_path(mypath)
 
-                filelist = os.listdir(mypath)
+                if flat:
+                    filelist = []
+                    for dirpath, dirnames, filenames in walklevel(mypath, flat):
+                        filelist += [os.path.join("/", dirpath, d) for d in dirnames
+                                if dirpath.count(os.path.sep) - mypath.count(os.path.sep) == flat]
+                        filelist += [os.path.join("/", dirpath, f) for f in filenames]
+                    filenames = [os.path.relpath(name, mypath) for name in filelist]
+                else:
+                    filelist = os.listdir(mypath)
+                    filenames = [mypath + (mypath == '/' and fname or '/' + fname)
+                            for fname in filelist]
 
                 if self._cumulative_size_calculated:
                     # If self.content_loaded is true, this is not the first
@@ -220,8 +243,6 @@ class Directory(FileSystemObject, Accumulator, Loadable):
                 if self.is_link:
                     self.infostring = '->' + self.infostring
 
-                filenames = [mypath + (mypath == '/' and fname or '/' + fname)
-                        for fname in filelist]
                 yield
 
                 self.load_content_mtime = os.stat(mypath).st_mtime
@@ -249,14 +270,16 @@ class Directory(FileSystemObject, Accumulator, Loadable):
                         is_a_dir = False
                     if is_a_dir:
                         try:
-                            item = self.fm.get_directory(name)
+                            item = self.fm.get_directory(name,
+                                    basename_is_rel=basename_is_rel)
                             item.load_if_outdated()
                         except:
-                            item = Directory(name, preload=stats,
-                                    path_is_abs=True)
+                            item = Directory(name, preload=stats, path_is_abs=True,
+                                    basename_is_rel=basename_is_rel)
                             item.load()
                     else:
-                        item = File(name, preload=stats, path_is_abs=True)
+                        item = File(name, preload=stats, path_is_abs=True,
+                                    basename_is_rel=basename_is_rel)
                         item.load()
                         disk_usage += item.size
 
@@ -327,7 +350,7 @@ class Directory(FileSystemObject, Accumulator, Loadable):
                 schedule = True   # was: self.size > 30
 
             if self.load_generator is None:
-                self.load_generator = self.load_bit_by_bit()
+                self.load_generator = self.load_bit_by_bit(flat=self.flat)
 
                 if schedule and self.fm:
                     self.fm.loader.add(self)
diff --git a/ranger/container/fsobject.py b/ranger/container/fsobject.py
index 86730fae..5acf9f5f 100644
--- a/ranger/container/fsobject.py
+++ b/ranger/container/fsobject.py
@@ -13,8 +13,8 @@ DOCUMENT_BASENAMES = ('bugs', 'bugs', 'changelog', 'copying', 'credits',
 BAD_INFO = '?'
 
 import re
-from os import lstat, stat
-from os.path import abspath, basename, dirname, realpath, splitext, extsep
+from os import lstat, stat, getcwd
+from os.path import abspath, basename, dirname, realpath, splitext, extsep, relpath
 from ranger.core.shared import FileManagerAware, SettingsAware
 from ranger.ext.shell_escape import shell_escape
 from ranger.ext.spawn import spawn
@@ -78,11 +78,17 @@ class FileSystemObject(FileManagerAware, SettingsAware):
     vcs_outdated = False
     vcs_enabled = False
 
-    def __init__(self, path, preload=None, path_is_abs=False):
+    basename_is_rel = False
+
+    def __init__(self, path, preload=None, path_is_abs=False, basename_is_rel=False):
         if not path_is_abs:
             path = abspath(path)
         self.path = path
-        self.basename = basename(path)
+        self.basename_is_rel = basename_is_rel
+        if not basename_is_rel:
+            self.basename = basename(path)
+        else:
+            self.basename = relpath(path, getcwd())
         self.basename_lower = self.basename.lower()
         self.extension = splitext(self.basename)[1].lstrip(extsep) or None
         self.dirname = dirname(path)
diff --git a/ranger/core/fm.py b/ranger/core/fm.py
index 2a9ce315..d567bf24 100644
--- a/ranger/core/fm.py
+++ b/ranger/core/fm.py
@@ -252,13 +252,17 @@ class FM(Actions, SignalDispatcher):
         """returns the path relative to rangers library directory"""
         return os.path.join(ranger.RANGERDIR, *paths)
 
-    def get_directory(self, path):
+    def get_directory(self, path, basename_is_rel = False):
         """Get the directory object at the given path"""
         path = os.path.abspath(path)
         try:
-            return self.directories[path]
+            directory = self.directories[path]
+            if directory.basename_is_rel != basename_is_rel:
+                del self.directories[path]
+                raise KeyError
+            return directory
         except KeyError:
-            obj = Directory(path)
+            obj = Directory(path, basename_is_rel = basename_is_rel)
             self.directories[path] = obj
             return obj
 
6 11:47:10 -0700 committer Kartik K. Agaram <vc@akkartik.com> 2016-08-26 11:47:10 -0700 3257' href='/akkartik/mu/commit/038new_text.cc?h=main&id=ad2604e893edbb3cde3d5e14cf4418acd3ef7d65'>ad2604e8 ^
b0bf5321 ^

6c96a437 ^
b0bf5321 ^


a89c1bed ^
ad2604e8 ^
b0bf5321 ^
ad2604e8 ^
b0bf5321 ^
9a81d746 ^
b0bf5321 ^


4b424bb2 ^

23d3a022 ^
4b424bb2 ^



2b37bbea ^
4b424bb2 ^
23d3a022 ^
b0bf5321 ^
23d3a022 ^
b0bf5321 ^
7c9def3c ^

b0bf5321 ^


e41e8e09 ^
78c50205 ^
23d3a022 ^
b0bf5321 ^

17401c38 ^

23d3a022 ^
17401c38 ^

23d3a022 ^
b0bf5321 ^
7c9def3c ^

b0bf5321 ^


23d3a022 ^
b0bf5321 ^
7c9def3c ^

b0bf5321 ^


23d3a022 ^
d9630e06 ^
7c9def3c ^

d9630e06 ^



78c50205 ^
d9630e06 ^

23d3a022 ^


b0bf5321 ^
23d3a022 ^

b0bf5321 ^
23d3a022 ^

b0bf5321 ^














78c50205 ^
b0bf5321 ^
23d3a022 ^
b56c564c ^
23d3a022 ^
b56c564c ^


b0bf5321 ^
b56c564c ^
b0bf5321 ^
b0bf5321 ^

aa3d29a5 ^
65151923 ^



23d3a022 ^
0edd9b9f ^

ce9b2b05 ^
0edd9b9f ^


23d3a022 ^







65151923 ^






2b250717 ^
65151923 ^

23d3a022 ^

65151923 ^









23d3a022 ^
65151923 ^


23d3a022 ^
909adb27 ^
65151923 ^



65151923 ^
aa3d29a5 ^



























1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235