File: cmd.py

package info (click to toggle)
git-cola 4.18.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,036 kB
  • sloc: python: 40,107; sh: 298; makefile: 223; xml: 121; tcl: 62
file content (89 lines) | stat: -rw-r--r-- 2,552 bytes parent folder | download
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
"""Base Command class"""
from __future__ import annotations
import time
from typing import Any, TYPE_CHECKING

from qtpy import QtCore
from qtpy.QtCore import Qt, Signal

if TYPE_CHECKING:
    from .app import ApplicationContext


class Command:
    """Mixin interface for commands"""

    UNDOABLE = False

    def __init__(self) -> None:
        """Initialize the base command"""
        self.can_undo = False

    @staticmethod
    def name() -> str:
        """Return the command's name"""
        return '(undefined)'

    @classmethod
    def is_undoable(cls) -> bool:
        """Can this be undone?"""
        return cls.UNDOABLE

    def do(self) -> Any:
        """Execute the command"""
        self.can_undo = True
        return True

    def undo(self) -> bool:
        """Undo the command"""
        return self.can_undo


class ContextCommand(Command):
    """Base class for commands that operate on a context"""

    def __init__(self, context: ApplicationContext) -> None:
        super().__init__()
        self.timestamp = time.time()
        self.context = context
        self.model = context.model
        self.cfg = context.cfg
        self.git = context.git
        self.selection = context.selection
        self.fsmonitor = context.fsmonitor
        self.old_timestamp = context.timestamp

    def do(self) -> Any:
        """Update the context"""
        # Commands can get executed in the background, and completion of one command may
        # happen *after* another Diff and similar commands have been fired. We prevent
        # the delayed background lookup from overwriting a newer command by checking the
        # context's timestamp.
        if self.context.timestamp > self.timestamp:
            return False
        super().do()
        self.context.timestamp = self.timestamp
        return True

    def undo(self) -> None:
        super().undo()
        self.context.timestamp = self.old_timestamp


class CommandBus(QtCore.QObject):
    do_command = Signal(object)
    undo_command = Signal(object)

    def __init__(self, parent: QtCore.QObject | None = None) -> None:
        super().__init__(parent)

        self.do_command.connect(lambda cmd: cmd.do(), type=Qt.QueuedConnection)
        self.undo_command.connect(lambda cmd: cmd.undo(), type=Qt.QueuedConnection)

    def do(self, cmd: str) -> None:
        """Run a command on the main thread"""
        self.do_command.emit(cmd)

    def undo(self, cmd: str) -> None:
        """Undo a command on the main thread"""
        self.undo_command.emit(cmd)