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
|
# Copyright (C) 2009-2013 Roman Zimbelmann <hut@lavabit.com>
# This software is distributed under the terms of the GNU GPL version 3.
import os
import sys
from os.path import abspath, normpath, join, expanduser, isdir
from ranger.container.history import History
from ranger.core.shared import FileManagerAware, SettingsAware
from ranger.ext.signals import SignalDispatcher
class Tab(FileManagerAware, SettingsAware):
def __init__(self, path):
self.thisdir = None # Current Working Directory
self._thisfile = None # Current File
self.history = History(self.settings.max_history_size, unique=False)
self.last_search = None
self.pointer = 0
self.path = abspath(expanduser(path))
self.pathway = ()
# NOTE: in the line below, weak=True works only in python3. In python2,
# weak references are not equal to the original object when tested with
# "==", and this breaks _set_thisfile_from_signal and _on_tab_change.
self.fm.signal_bind('move', self._set_thisfile_from_signal, priority=0.1,
weak=(sys.version > '3'))
self.fm.signal_bind('tab.change', self._on_tab_change,
weak=(sys.version > '3'))
def _set_thisfile_from_signal(self, signal):
if self == signal.tab:
self._thisfile = signal.new
if self == self.fm.thistab:
self.pointer = self.thisdir.pointer
def _on_tab_change(self, signal):
if self == signal.new and self.thisdir:
# restore the pointer whenever this tab is reopened
self.thisdir.pointer = self.pointer
self.thisdir.correct_pointer()
def _set_thisfile(self, value):
if value is not self._thisfile:
previous = self._thisfile
self.fm.signal_emit('move', previous=previous, new=value, tab=self)
def _get_thisfile(self):
return self._thisfile
thisfile = property(_get_thisfile, _set_thisfile)
def at_level(self, level):
"""Returns the FileSystemObject at the given level.
level >0 => previews
level 0 => current file/directory
level <0 => parent directories
"""
if level <= 0:
try:
return self.pathway[level - 1]
except IndexError:
return None
else:
directory = self.thisdir
for i in range(level):
if directory is None:
return None
if directory.is_directory:
directory = directory.pointed_obj
else:
return None
return directory
def get_selection(self):
if self.thisdir:
if self.thisdir.marked_items:
return self.thisdir.get_selection()
elif self._thisfile:
return [self._thisfile]
else:
return []
return set()
def assign_cursor_positions_for_subdirs(self):
"""Assign correct cursor positions for subdirectories"""
last_path = None
for path in reversed(self.pathway):
if last_path is None:
last_path = path
continue
path.move_to_obj(last_path)
last_path = path
def ensure_correct_pointer(self):
if self.thisdir:
self.thisdir.correct_pointer()
def history_go(self, relative):
"""Move relative in history"""
if self.history:
self.history.move(relative).go(history=False)
def inherit_history(self, other_history):
self.history.rebase(other_history)
def enter_dir(self, path, history = True):
"""Enter given path"""
# TODO: Ensure that there is always a self.thisdir
if path is None: return
path = str(path)
previous = self.thisdir
# get the absolute path
path = normpath(join(self.path, expanduser(path)))
if not isdir(path):
return False
new_thisdir = self.fm.get_directory(path)
try:
os.chdir(path)
except:
return True
self.path = path
self.thisdir = new_thisdir
self.thisdir.load_content_if_outdated()
# build the pathway, a tuple of directory objects which lie
# on the path to the current directory.
if path == '/':
self.pathway = (self.fm.get_directory('/'), )
else:
pathway = []
currentpath = '/'
for dir in path.split('/'):
currentpath = join(currentpath, dir)
pathway.append(self.fm.get_directory(currentpath))
self.pathway = tuple(pathway)
self.assign_cursor_positions_for_subdirs()
# set the current file.
self.thisdir.sort_directories_first = self.fm.settings.sort_directories_first
self.thisdir.sort_reverse = self.fm.settings.sort_reverse
self.thisdir.sort_if_outdated()
if previous and previous.path != path:
self.thisfile = self.thisdir.pointed_obj
else:
# This avoids setting self.pointer (through the 'move' signal) and
# is required so that you can use enter_dir when switching tabs
# without messing up the pointer.
self._thisfile = self.thisdir.pointed_obj
if history:
self.history.add(new_thisdir)
self.fm.signal_emit('cd', previous=previous, new=self.thisdir)
return True
def __repr__(self):
return "<Tab '%s'>" % self.thisdir
|