about summary refs log tree commit diff stats
path: root/ranger/gui/widgets/console.py
blob: 2d6df2a7570cfbb67f9c31c91ac5f33609cfdd5b (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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
"""The Console widget implements a vim-like console for entering
commands, searching and executing files."""
from . import Widget
from ranger import commands, log
from ranger.gui.widgets.console_mode import is_valid_mode, mode_to_class
import curses
from collections import deque

DEFAULT_HISTORY = 0
SEARCH_HISTORY = 1
QUICKOPEN_HISTORY = 2
OPEN_HISTORY = 3

class Console(Widget):
	mode = None
	visible = False
	commandlist = None
	last_cursor_mode = 1
	prompt = ':'
	copy = ''
	tab_deque = None
	original_line = None
	history = None
	histories = None
	override = None

	def __init__(self, win):
		from ranger.container import CommandList, History
		Widget.__init__(self, win)
		self.commandlist = CommandList()
		self.settings.keys.initialize_console_commands(self.commandlist)
		self.clear()
		self.histories = [None] * 4
		self.histories[DEFAULT_HISTORY] = History()
		self.histories[SEARCH_HISTORY] = History()
		self.histories[QUICKOPEN_HISTORY] = History()
		self.histories[OPEN_HISTORY] = History()
	
	def init(self):
		"""override this. Called directly after class change"""

	def draw(self):
		if self.mode is None:
			return
		self.win.addstr(self.y, self.x, self.prompt + self.line)

	def finalize(self):
		try:
			self.win.move(self.y, self.x + self.pos + len(self.prompt))
		except:
			pass

	def open(self, mode, string=''):
		if not is_valid_mode(mode):
			return False

		cls = mode_to_class(mode)

		self.last_cursor_mode = curses.curs_set(1)
		self.mode = mode
		self.__class__ = cls
		self.history = self.histories[DEFAULT_HISTORY]
		self.init()
		self.tab_deque = None
		self.focused = True
		self.visible = True
		self.line = string
		self.pos = len(string)
		self.history.add('')
		return True

	def close(self):
		curses.curs_set(self.last_cursor_mode)
		self.add_to_history()
		self.clear()
		self.__class__ = Console
		self.focused = False
		self.visible = False
		if hasattr(self, 'on_close'):
			self.on_close()
	
	def clear(self):
		self.pos = 0
		self.line = ''
	
	def press(self, key):
		from curses.ascii import ctrl, ESC

		try:
			cmd = self.commandlist[self.env.keybuffer.tuple_with_numbers()]
		except KeyError:
			self.env.key_clear()
			return

		if cmd == self.commandlist.dummy_object:
			return

		cmd.execute(self)
		self.env.key_clear()

	def type_key(self, key):
		self.tab_deque = None

		if isinstance(key, int):
			key = chr(key)

		if self.pos == len(self.line):
			self.line += key
		else:
			self.line = self.line[:self.pos] + key + self.line[self.pos:]

		self.pos += len(key)
		self.on_line_change()

	def history_move(self, n):
		from ranger.container.history import HistoryEmptyException
		try:
			current = self.history.current()
		except HistoryEmptyException:
			pass
		else:
			if self.line != current and self.line != self.history.top():
				self.history.modify(self.line)
			self.history.move(n)
			current = self.history.current()
			if self.line != current:
				self.line = self.history.current()
				self.pos = len(self.line)
	
	def add_to_history(self):
		self.history.fast_forward()
		self.history.modify(self.line)

	def move(self, relative = 0, absolute = None):
		if absolute is not None:
			if absolute < 0:
				self.pos = len(self.line) + 1 + absolute
			else:
				self.pos = absolute

		self.pos = min(max(0, self.pos + relative), len(self.line))

	def delete_rest(self, direction):
		self.tab_deque = None
		if direction > 0:
			self.copy = self.line[self.pos:]
			self.line = self.line[:self.pos]
		else:
			self.copy = self.line[:self.pos]
			self.line = self.line[self.pos:]
			self.pos = 0
		self.on_line_change()

	def paste(self):
		if self.pos == len(self.line):
			self.line += self.copy
		else:
			self.line = self.line[:self.pos] + self.copy + self.line[self.pos:]
		self.pos += len(self.copy)
		self.on_line_change()

	def delete_word(self):
		self.tab_deque = None
		try:
			i = self.line.rindex(' ', 0, self.pos - 1) + 1
			self.line = self.line[:i] + self.line[self.pos:]
			self.pos = len(self.line)
		except ValueError:
			self.line = ''
			self.pos = 0
		self.on_line_change()
	
	def delete(self, mod):
		self.tab_deque = None
		if mod == -1 and len(self.line) == 0:
			self.close()
		pos = self.pos + mod

		self.line = self.line[0:pos] + self.line[pos+1:]
		self.move(relative = mod)
		self.on_line_change()

	def execute(self):
		self.tab_deque = None
		self.close()

	def tab(self):
		pass

	def on_line_change(self):
		pass


class ConsoleWithTab(Console):
	def tab(self, n=1):
		if self.tab_deque is None:
			tab_result = self._get_tab()

			if isinstance(tab_result, str):
				self.line = tab_result
				self.pos = len(tab_result)
				self.on_line_change()

			elif tab_result == None:
				pass

			elif hasattr(tab_result, '__iter__'):
				self.tab_deque = deque(tab_result)
				self.tab_deque.appendleft(self.line)

		if self.tab_deque is not None:
			self.tab_deque.rotate(-n)
			self.line = self.tab_deque[0]
			self.pos = len(self.line)
			self.on_line_change()
	
	def _get_tab(self):
		"""
		Override this function in the subclass!

		It should return either a string, an iterable or None.
		If a string is returned, tabbing will result in the line turning
		into that string.
		If another iterable is returned, each tabbing will cycle through
		the elements of the iterable (which have to be strings).
		If None is returned, nothing will happen.
		"""

		return None


class CommandConsole(ConsoleWithTab):
	prompt = ':'

	def execute(self, cmd=None):
		if cmd is None:
			cmd = self._get_cmd()

		if cmd:
			cmd.execute()

		Console.execute(self)

	def _get_cmd(self):
		try:
			command_name = self.line.split()[0]
		except IndexError:
			return None

		try:
			command_class = commands.by_name[command_name]
		except KeyError:
			return None

		return command_class(self.line, self.mode)
	
	def _get_tab(self):
		if ' ' in self.line:
			cmd = self._get_cmd()
			if cmd:
				return cmd.tab()
			else:
				return None

		return commands.command_generator(self.line)


class QuickCommandConsole(CommandConsole):
	"""
	The QuickCommandConsole is essentially the same as the
	CommandConsole, and includes one additional feature:
	After each letter you type, it checks whether the command as it
	stands there could be executed in a meaningful way, and if it does,
	run it right away.

	Example:
	>cd ..
	As you type the last dot, The console will recognize what you mean
	and enter the parent directory saving you the time of pressing enter.
	"""
	prompt = '>'
	def on_line_change(self):
		cmd = self._get_cmd()
		if cmd and cmd.quick_open():
			self.execute(cmd)


class SearchConsole(Console):
	prompt = '/'

	def init(self):
		self.history = self.histories[SEARCH_HISTORY]

	def execute(self):
		import re
		if self.fm.env.pwd:
			regexp = re.compile(self.line, re.L | re.U | re.I)
			self.fm.env.last_search = regexp
			if self.fm.env.pwd.search(regexp):
				self.fm.env.cf = self.fm.env.pwd.pointed_obj
		Console.execute(self)


class OpenConsole(Console):
	"""
	The OpenConsole allows you to execute shell commands:
	!vim *         will run vim and open all files in the directory.

	There is a special syntax for more control:

	!d! mplayer    will run mplayer with flags (d means detached)
	!@ mplayer     will open the selected files with mplayer

	those two can be combinated:

	!d!@mplayer    will open the selection with a detached mplayer

	For a list of other flags than "d", look at the documentation
	of ranger.applications.
	"""
	prompt = '!'

	def init(self):
		self.history = self.histories[OPEN_HISTORY]
	
	def execute(self):
		from ranger.applications import run
		from subprocess import STDOUT, PIPE
		command, flags = self._parse()
		if command:
			run(command, flags=flags, fm=self.fm,
					mode=0, shell=True, stdin=None,
					apps=self.fm.apps, stderr=STDOUT)
		Console.execute(self)
	
	def _parse(self):
		if '!' in self.line:
			flags, cmd = self.line.split('!', 1)
		else:
			flags, cmd = '', self.line

		add_selection = False
		if cmd.startswith('@'):
			cmd = cmd[1:]
			add_selection = True
		elif flags.startswith('@'):
			flags = flags[1:]
			add_selection = True

		if add_selection:
			cmd += ' ' + ' '.join(tuple(map(self._shellify,
					self.env.get_selection())))

		return (cmd, flags)

	def _shellify(self, string):
		return "'" + str(string).replace("'","'\"'\"'") + "'"


class QuickOpenConsole(ConsoleWithTab):
	"""
	The QuickOpenConsole allows you to open files with
	pre-defined programs and modes very quickly. By adding flags
	to the command, you can specify precisely how the program is run,
	ie. the d-flag will run it detached from the filemanager.
	"""

	prompt = 'open with: '

	def init(self):
		self.history = self.histories[QUICKOPEN_HISTORY]

	def execute(self):
		split = self.line.split()
		app, flags, mode = self._get_app_flags_mode()
		self.fm.execute_file(
				files = [self.env.cf],
				app = app,
				flags = flags,
				mode = mode )
		Console.execute(self)

	def _get_app_flags_mode(self):
		"""
		Extracts the application, flags and mode from
		a string entered into the "openwith_quick" console.
		"""
		# examples:
		# "mplayer d 1" => ("mplayer", "d", 1)
		# "aunpack 4" => ("aunpack", "", 4)
		# "p" => ("", "p", 0)
		# "" => None

		app = ''
		flags = ''
		mode = 0
		split = self.line.split()

		if len(split) == 0:
			pass

		elif len(split) == 1:
			part = split[0]
			if self._is_app(part):
				app = part
			elif self._is_flags(part):
				flags = part
			elif self._is_mode(part):
				mode = part

		elif len(split) == 2:
			part0 = split[0]
			part1 = split[1]

			if self._is_app(part0):
				app = part0
				if self._is_flags(part1):
					flags = part1
				elif self._is_mode(part1):
					mode = part1
			elif self._is_flags(part0):
				flags = part0
				if self._is_mode(part1):
					mode = part1
			elif self._is_mode(part0):
				mode = part0
				if self._is_flags(part1):
					flags = part1

		elif len(split) >= 3:
			part0 = split[0]
			part1 = split[1]
			part2 = split[2]

			if self._is_app(part0):
				app = part0
				if self._is_flags(part1):
					flags = part1
					if self._is_mode(part2):
						mode = part2
				elif self._is_mode(part1):
					mode = part1
					if self._is_flags(part2):
						flags = part2
			elif self._is_flags(part0):
				flags = part0
				if self._is_mode(part1):
					mode = part1
			elif self._is_mode(part0):
				mode = part0
				if self._is_flags(part1):
					flags = part1

		return app, flags, int(mode)

	def _get_tab(self):
		if ' ' not in self.line:
			all_apps = self.fm.apps.all()
			log(all_apps)
			if all_apps:
				return (app for app in all_apps if app.startswith(self.line))

		return None


	def _is_app(self, arg):
		return self.fm.apps.has(arg)

	def _is_flags(self, arg):
		from ranger.applications import ALLOWED_FLAGS
		return all(x in ALLOWED_FLAGS for x in arg)

	def _is_mode(self, arg):
		return all(x in '0123456789' for x in arg)