File: actions.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 (96 lines) | stat: -rw-r--r-- 2,922 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
90
91
92
93
94
95
96
"""QAction creator functions"""
from __future__ import annotations
from typing import TYPE_CHECKING

from . import cmds
from . import difftool
from . import hotkeys
from . import icons
from . import qtutils
from .i18n import N_

if TYPE_CHECKING:
    from qtpy.QtGui import QIcon
    from qtpy.QtWidgets import QAction

    from .app import ApplicationContext
    from .cmds import LaunchEditor, LaunchEditorAtLine, StageOrUnstage
    from .widgets.commitmsg import CommitMessageEditor
    from .widgets.diff import DiffEditor
    from .widgets.status import StatusTreeWidget


def cmd_action(
    widget: DiffEditor | CommitMessageEditor | StatusTreeWidget,
    cmd: type[LaunchEditor] | type[LaunchEditorAtLine] | type[StageOrUnstage],
    context: ApplicationContext,
    icon: QIcon,
    *shortcuts,
) -> QAction:
    """Wrap a generic ContextCommand in a QAction"""
    action = qtutils.add_action(widget, cmd.name(), cmds.run(cmd, context), *shortcuts)
    action.setIcon(icon)
    return action


def launch_editor(
    context: ApplicationContext, widget: DiffEditor | CommitMessageEditor, *shortcuts
) -> QAction:
    """Create a QAction to launch an editor"""
    icon = icons.edit()
    return cmd_action(
        widget, cmds.LaunchEditor, context, icon, hotkeys.EDIT, *shortcuts
    )


def launch_editor_at_line(
    context: ApplicationContext,
    widget: DiffEditor | CommitMessageEditor | StatusTreeWidget,
    *shortcuts,
) -> QAction:
    """Create a QAction to launch an editor at the current line"""
    icon = icons.edit()
    return cmd_action(
        widget, cmds.LaunchEditorAtLine, context, icon, hotkeys.EDIT, *shortcuts
    )


def launch_difftool(
    context: ApplicationContext, widget: DiffEditor | CommitMessageEditor
) -> QAction:
    """Create a QAction to launch git-difftool(1)"""
    icon = icons.diff()
    cmd = difftool.LaunchDifftool
    action = qtutils.add_action(
        widget, cmd.name(), cmds.run(cmd, context), hotkeys.DIFF
    )
    action.setIcon(icon)
    return action


def stage_or_unstage(
    context: ApplicationContext, widget: DiffEditor | CommitMessageEditor
) -> QAction:
    """Create a QAction to stage or unstage the selection"""
    icon = icons.add()
    return cmd_action(
        widget, cmds.StageOrUnstage, context, icon, hotkeys.STAGE_SELECTION
    )


def move_down(widget: DiffEditor | CommitMessageEditor) -> QAction:
    """Create a QAction to select the next item"""
    action = qtutils.add_action(
        widget, N_('Next File'), widget.down.emit, hotkeys.MOVE_DOWN_SECONDARY
    )
    action.setIcon(icons.move_down())
    return action


def move_up(widget: DiffEditor | CommitMessageEditor) -> QAction:
    """Create a QAction to select the previous/above item"""
    action = qtutils.add_action(
        widget, N_('Previous File'), widget.up.emit, hotkeys.MOVE_UP_SECONDARY
    )
    action.setIcon(icons.move_up())
    return action