summary refs log tree commit diff stats
path: root/ranger/gui/widgets/console.py
blob: b3fa7151482eecc9c421893675173f143fff3edc (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# This file is part of ranger, the console file manager.
# License: GNU GPL version 3, see the file "AUTHORS" for details.

"""The Console widget implements a vim-like console"""

from __future__ import (absolute_import, division, print_function)

import curses
import os
import re
from collections import deque

from ranger.gui.widgets import Widget
from ranger.ext.direction import Direction
from ranger.ext.widestring import uwid, WideString
from ranger.container.history import History, HistoryEmptyException
import ranger


class Console(Widget):  # pylint: disable=too-many-instance-attributes,too-many-public-methods
    visible = False
    last_cursor_mode = None
    history_search_pattern = None
    prompt = ':'
    copy = ''
    tab_deque = None
    original_line = None
    history = None
    history_backup = None
    override = None
    allow_close = False
    historypath = None
    wait_for_command_input = False
    unicode_buffer = ""

    def __init__(self, win):
        Widget.__init__(self, win)
        self.pos = 0
        self.line = ''
        self.history = History(self.settings.max_console_history_size)
        # load history from files
        if not ranger.args.clean:
            self.historypath = self.fm.datapath('history')
            if os.path.exists(self.historypath):
                try:
                    fobj = open(self.historypath, 'r')
                except OSError as ex:
                    self.fm.notify('Failed to read history file', bad=True, exception=ex)
                else:
                    for line in fobj:
                        self.history.add(line[:-1])
                    fobj.close()
        self.history_backup = History(self.history)

        # NOTE: the console is considered in the "question mode" when the
        # question_queue is non-empty.  In that case, the console will draw the
        # question instead of the regular console, and the input you give is
        # used to answer the question instead of typing in commands.
        #
        # A question is a tuple of (question_string, callback_func,
        # tuple_of_choices).  callback_func is a function that is called when
        # the question is answered which gets the answer as an argument.
        # tuple_of_choices looks like ('y', 'n').  Only one-letter-answers are
        # currently supported.  Pressing enter uses the first choice whereas
        # pressing ESC uses the second choice.
        self.question_queue = []

    def destroy(self):
        # save history to files
        if ranger.args.clean or not self.settings.save_console_history:
            return
        if self.historypath:
            try:
                fobj = open(self.historypath, 'w')
            except OSError as ex:
                self.fm.notify('Failed to write history file', bad=True, exception=ex)
            else:
                for entry in self.history_backup:
                    try:
                        fobj.write(entry + '\n')
                    except UnicodeEncodeError:
                        pass
                fobj.close()
        Widget.destroy(self)

    def draw(self):
        self.win.erase()
        if self.question_queue:
            assert isinstance(self.question_queue[0], tuple)
            assert len(self.question_queue[0]) == 3
            self.addstr(0, 0, self.question_queue[0][0])
            return

        self.addstr(0, 0, self.prompt)
        line = WideString(self.line)
        overflow = -self.wid + len(self.prompt) + len(line) + 1
        if overflow > 0:
            self.addstr(0, len(self.prompt), str(line[overflow:]))
        else:
            self.addstr(0, len(self.prompt), self.line)

    def finalize(self):
        move = self.fm.ui.win.move
        if self.question_queue:
            try:
                move(self.y, len(self.question_queue[0][0]))
            except curses.error:
                pass
        else:
            try:
                pos = uwid(self.line[0:self.pos]) + len(self.prompt)
                move(self.y, self.x + min(self.wid - 1, pos))
            except curses.error:
                pass

    def open(self, string='', prompt=None, position=None):
        if prompt is not None:
            assert isinstance(prompt, str)
            self.prompt = prompt
        elif 'prompt' in self.__dict__:
            del self.prompt

        if self.last_cursor_mode is None:
            try:
                self.last_cursor_mode = curses.curs_set(1)
            except curses.error:
                pass
        self.allow_close = False
        self.tab_deque = None
        self.unicode_buffer = ""
        self.line = string
        self.history_search_pattern = self.line
        self.pos = len(string)
        if position is not None:
            self.pos = min(self.pos, position)
        self.history_backup.fast_forward()
        self.history = History(self.history_backup)
        self.history.add('')
        self.wait_for_command_input = True
        return True

    def close(self, trigger_cancel_function=True):
        if self.question_queue:
            question = self.question_queue[0]
            answers = question[2]
            if len(answers) >= 2:
                self._answer_question(answers[1])
        else:
            self._close_command_prompt(trigger_cancel_function)

    def _close_command_prompt(self, trigger_cancel_function=True):
        if trigger_cancel_function:
            cmd = self._get_cmd(quiet=True)
            if cmd:
                cmd.cancel()
        if self.last_cursor_mode is not None:
            try:
                curses.curs_set(self.last_cursor_mode)
            except curses.error:
                pass
            self.last_cursor_mode = None
        self.fm.hide_console_info()
        self.add_to_history()
        self.tab_deque = None
        self.clear()
        self.__class__ = Console
        self.wait_for_command_input = False

    def clear(self):
        self.pos = 0
        self.line = ''

    def press(self, key):
        self.fm.ui.keymaps.use_keymap('console')
        if not self.fm.ui.press(key):
            self.type_key(key)

    def _answer_question(self, answer):
        if not self.question_queue:
            return False
        question = self.question_queue[0]
        _, callback, answers = question
        if answer in answers:
            self.question_queue.pop(0)
            callback(answer)
            return True
        return False

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

        line = "" if self.question_queue else self.line
        result = self._add_character(key, self.unicode_buffer, line, self.pos)
        if result[1] == line:
            # line didn't change, so we don't need to do anything, just update
            # the unicode _buffer.
            self.unicode_buffer = result[0]
            return

        if self.question_queue:
            self.unicode_buffer, answer, self.pos = result
            self._answer_question(answer)
        else:
            self.unicode_buffer, self.line, self.pos = result
            self.on_line_change()

    def _add_character(self, key, unicode_buffer, line, pos):
        # Takes the pressed key, a string "unicode_buffer" containing a
        # potentially incomplete unicode character, the current line and the
        # position of the cursor inside the line.
        # This function returns the new unicode buffer, the modified line and
        # position.
        if isinstance(key, int):
            try:
                key = chr(key)
            except ValueError:
                return unicode_buffer, line, pos

        if self.fm.py3:
            if len(unicode_buffer) >= 4:
                unicode_buffer = ""
            if ord(key) in range(0, 256):
                unicode_buffer += key
                try:
                    decoded = unicode_buffer.encode("latin-1").decode("utf-8")
                except UnicodeDecodeError:
                    return unicode_buffer, line, pos
                except UnicodeEncodeError:
                    return unicode_buffer, line, pos
                else:
                    unicode_buffer = ""
                    if pos == len(line):
                        line += decoded
                    else:
                        line = line[:pos] + decoded + line[pos:]
                    pos += len(decoded)
        else:
            if pos == len(line):
                line += key
            else:
                line = line[:pos] + key + line[pos:]
            pos += len(key)
        return unicode_buffer, line, pos

    def history_move(self, n):
        try:
            current = self.history.current()
        except HistoryEmptyException:
            pass
        else:
            if self.line != current and self.line != self.history.top():
                self.history.modify(self.line)
            if self.history_search_pattern:
                self.history.search(self.history_search_pattern, n)
            else:
                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_backup.fast_forward()
        self.history_backup.add(self.line)
        self.history = History(self.history_backup)

    def move(self, **keywords):
        direction = Direction(keywords)
        if direction.horizontal():
            # Ensure that the pointer is moved utf-char-wise
            if self.fm.py3:
                self.pos = direction.move(
                    direction=direction.right(),
                    minimum=0,
                    maximum=len(self.line) + 1,
                    current=self.pos)
            else:
                if self.fm.py3:
                    uchar = list(self.line)
                    upos = len(self.line[:self.pos])
                else:
                    uchar = list(self.line.decode('utf-8', 'ignore'))
                    upos = len(self.line[:self.pos].decode('utf-8', 'ignore'))
                newupos = direction.move(
                    direction=direction.right(),
                    minimum=0,
                    maximum=len(uchar) + 1,
                    current=upos)
                self.pos = len(''.join(uchar[:newupos]).encode('utf-8', 'ignore'))

    def move_word(self, **keywords):
        direction = Direction(keywords)
        if direction.horizontal():
            self.pos = self.move_by_word(self.line, self.pos, direction.right())
            self.on_line_change()

    @staticmethod
    def move_by_word(line, position, direction):
        """
        Returns a new position by moving word-wise in the line

        >>> import sys
        >>> if sys.version_info < (3, ):
        ...     # Didn't get the unicode test to work on python2, even though
        ...     # it works fine in ranger, even with unicode input...
        ...     line = "ohai world,  this is dog"
        ... else:
        ...     line = "\\u30AA\\u30CF\\u30E8\\u30A6 world,  this is dog"
        >>> Console.move_by_word(line, 0, -1)
        0
        >>> Console.move_by_word(line, 0, 1)
        5
        >>> Console.move_by_word(line, 2, -1)
        0
        >>> Console.move_by_word(line, 2, 1)
        5
        >>> Console.move_by_word(line, 15, -2)
        5
        >>> Console.move_by_word(line, 15, 2)
        21
        >>> Console.move_by_word(line, 24, -1)
        21
        >>> Console.move_by_word(line, 24, 1)
        24
        """
        word_beginnings = []
        seen_whitespace = True
        current_word = None
        cursor_inside_word = False

        # Scan the line for word boundaries and determine position of cursor
        for i, char in enumerate(line):
            if i == position:
                current_word = len(word_beginnings)
                if not seen_whitespace:
                    cursor_inside_word = True
            if char == " ":
                seen_whitespace = True
            elif seen_whitespace:
                seen_whitespace = False
                word_beginnings.append(i)
        word_beginnings.append(len(line))

        # Handle corner cases:
        if current_word is None:
            current_word = len(word_beginnings)
        if direction > 0 and cursor_inside_word:
            current_word -= 1
        if direction < 0 and position == len(line):
            current_word -= 1

        new_word = current_word + direction
        new_word = max(0, min(len(word_beginnings) - 1, new_word))

        return word_beginnings[new_word]

    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, backward=True):
        if self.line:
            self.tab_deque = None
            if backward:
                right_part = self.line[self.pos:]
                i = self.pos - 2
                while i >= 0 and re.match(
                        r'[\w\d]', self.line[i], re.UNICODE):  # pylint: disable=no-member
                    i -= 1
                self.copy = self.line[i + 1:self.pos]
                self.line = self.line[:i + 1] + right_part
                self.pos = i + 1
            else:
                left_part = self.line[:self.pos]
                i = self.pos + 1
                while i < len(self.line) and re.match(
                        r'[\w\d]', self.line[i], re.UNICODE):  # pylint: disable=no-member
                    i += 1
                self.copy = self.line[self.pos:i]
                if i >= len(self.line):
                    self.line = left_part
                    self.pos = len(self.line)
                else:
                    self.line = left_part + self.line[i:]
                    self.pos = len(left_part)
            self.on_line_change()

    def delete(self, mod):
        self.tab_deque = None
        if mod == -1 and self.pos == 0:
            if not self.line:
                self.close(trigger_cancel_function=False)
            return
        # Delete utf-char-wise
        if self.fm.py3:
            left_part = self.line[:self.pos + mod]
            self.pos = len(left_part)
            self.line = left_part + self.line[self.pos + 1:]
        else:
            uchar = list(self.line.decode('utf-8', 'ignore'))
            upos = len(self.line[:self.pos].decode('utf-8', 'ignore')) + mod
            left_part = ''.join(uchar[:upos]).encode('utf-8', 'ignore')
            self.pos = len(left_part)
            self.line = left_part + ''.join(uchar[upos + 1:]).encode('utf-8', 'ignore')
        self.on_line_change()

    def execute(self, cmd=None):
        if self.question_queue and cmd is None:
            question = self.question_queue[0]
            answers = question[2]
            if len(answers) >= 1:
                self._answer_question(answers[0])
            else:
                self.question_queue.pop(0)
            return

        self.allow_close = True
        if cmd:
            cmd.execute()
        else:
            self.fm.execute_console(self.line)

        if self.allow_close:
            self._close_command_prompt(trigger_cancel_function=False)

    def _get_cmd(self, quiet=False):
        try:
            command_class = self.get_cmd_class()
        except IndexError:
            return None
        except KeyError:
            if not quiet:
                self.fm.notify("Command not found: `%s'" % self.line.split()[0], bad=True)
            return None
        return command_class(self.line)

    def get_cmd_class(self):
        return self.fm.commands.get_command(self.line.split()[0])

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

        return self.fm.commands.command_generator(self.line)

    def tab(self, tabnum=1):
        if self.tab_deque is None:
            tab_result = self._get_tab(tabnum)

            if tab_result is None:
                pass
            elif isinstance(tab_result, str):
                self.line = tab_result
                self.pos = len(tab_result)
                self.on_line_change()
            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(-tabnum)
            self.line = self.tab_deque[0]
            self.pos = len(self.line)
            self.on_line_change()

    def on_line_change(self):
        self.history_search_pattern = self.line
        try:
            cls = self.get_cmd_class()
        except (KeyError, ValueError, IndexError):
            pass
        else:
            cmd = cls(self.line)
            if cmd and cmd.quick():
                cmd.quickly_executed = True
                self.execute(cmd)

    def ask(self, text, callback, choices=None):
        """Open a question prompt with predefined choices

        The "text" is displayed as the question text and should include a list
        of possible keys that the user can type.  The "callback" is a function
        that is called when the question is answered.  It only gets the answer
        as an argument.  "choices" is a tuple of one-letter strings that can be
        typed in by the user.  Every other input gets ignored, except <Enter>
        and <ESC>.

        The first choice is used when the user presses <Enter>, the second
        choice is used when the user presses <ESC>.
        """
        self.question_queue.append(
            (text, callback, choices if choices is not None else ['y', 'n']))


if __name__ == '__main__':
    import doctest
    doctest.testmod()