summary refs log tree commit diff stats
path: root/ranger/gui/widgets/titlebar.py
blob: ddb27f2ca84ccfb26aead59e1dddcf14055a41f4 (plain) (blame)
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, 2010, 2011  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
The titlebar is the widget at the top, giving you broad overview.

It displays the current path among other things.
"""

from os.path import basename

from . import Widget
from ranger.gui.bar import Bar

class TitleBar(Widget):
	old_cf = None
	old_keybuffer = None
	old_wid = None
	result = None
	throbber = ' '
	need_redraw = False
	tab_width = 0

	def __init__(self, *args, **keywords):
		Widget.__init__(self, *args, **keywords)
		self.fm.signal_bind('tab.change', self.request_redraw, weak=True)

	def request_redraw(self):
		self.need_redraw = True

	def draw(self):
		if self.need_redraw or \
				self.env.cf != self.old_cf or\
				str(self.env.keybuffer) != str(self.old_keybuffer) or\
				self.wid != self.old_wid:
			self.need_redraw = False
			self.old_wid = self.wid
			self.old_cf = self.env.cf
			self._calc_bar()
		self._print_result(self.result)
		if self.wid > 2:
			self.color('in_titlebar', 'throbber')
			self.addnstr(self.y, self.wid - 2 - self.tab_width,
					self.throbber, 1)

	def click(self, event):
		"""Handle a MouseEvent"""
		direction = event.mouse_wheel_direction()
		if direction:
			self.fm.tab_move(direction)
			self.need_redraw = True
			return True

		if not event.pressed(1) or not self.result:
			return False

		pos = self.wid - 1
		for tabname in reversed(self.fm._get_tab_list()):
			tabtext = self._get_tab_text(tabname)
			pos -= len(tabtext)
			if event.x > pos:
				self.fm.tab_open(tabname)
				self.need_redraw = True
				return True

		pos = 0
		for i, part in enumerate(self.result):
			pos += len(part)
			if event.x < pos:
				if i < 2:
					self.fm.enter_dir("~")
				elif i == 2:
					self.fm.enter_dir("/")
				else:
					try:
						self.fm.enter_dir(part.directory)
					except:
						pass
				return True
		return False

	def _calc_bar(self):
		bar = Bar('in_titlebar')
		self._get_left_part(bar)
		self._get_right_part(bar)
		try:
			bar.shrink_from_the_left(self.wid)
		except ValueError:
			bar.shrink_by_removing(self.wid)
		self.result = bar.combine()

	def _get_left_part(self, bar):
		if self.env.username == 'root':
			clr = 'bad'
		else:
			clr = 'good'

		bar.add(self.env.username, 'hostname', clr, fixed=True)
		bar.add('@', 'hostname', clr, fixed=True)
		bar.add(self.env.hostname, 'hostname', clr, fixed=True)
		bar.add(':', 'hostname', clr, fixed=True)

		pathway = self.env.pathway
		if self.settings.tilde_in_titlebar and \
				self.fm.env.cwd.path.startswith(self.env.home_path):
			pathway = pathway[self.env.home_path.count('/')+1:]
			bar.add('~/', 'directory', fixed=True)

		for path in pathway:
			if path.is_link:
				clr = 'link'
			else:
				clr = 'directory'

			bar.add(path.safe_basename, clr, directory=path)
			bar.add('/', clr, fixed=True, directory=path)

		if self.env.cf is not None:
			bar.add(self.env.cf.safe_basename, 'file')

	def _get_right_part(self, bar):
		# TODO: fix that pressed keys are cut off when chaining CTRL keys
		kb = str(self.env.keybuffer)
		self.old_keybuffer = kb
		bar.addright(kb, 'keybuffer', fixed=True)
		bar.addright('  ', 'space', fixed=True)
		self.tab_width = 0
		if len(self.fm.tabs) > 1:
			for tabname in self.fm._get_tab_list():
				tabtext = self._get_tab_text(tabname)
				self.tab_width += len(tabtext)
				clr = 'good' if tabname == self.fm.current_tab else 'bad'
				bar.addright(tabtext, 'tab', clr, fixed=True)

	def _get_tab_text(self, tabname):
		result = ' ' + str(tabname)
		if self.settings.dirname_in_tabs:
			dirname = basename(self.fm.tabs[tabname])
			if not dirname:
				result += ":/"
			elif len(dirname) > 15:
				result += ":" + dirname[:14] + "~"
			else:
				result += ":" + dirname
		return result

	def _print_result(self, result):
		self.win.move(0, 0)
		for part in result:
			self.color(*part.lst)
			y, x = self.win.getyx()
			self.addstr(y, x, str(part))
		self.color_reset()