File: common.py

package info (click to toggle)
git-cola 4.14.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,812 kB
  • sloc: python: 37,625; sh: 298; makefile: 223; xml: 102; tcl: 62
file content (85 lines) | stat: -rw-r--r-- 2,474 bytes parent folder | download | duplicates (2)
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
import functools

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


def cmd_action(parent, cmd, context, func, *keys):
    """Wrap a standard Command object in a QAction

    This function assumes that :func:`func()` takes no arguments,
    that `cmd` has a :func:`name()` method, and that the `cmd`
    constructor takes a single argument, as returned by `func`.

    """
    return qtutils.add_action(
        parent, cmd.name(), lambda: cmds.do(cmd, context, func()), *keys
    )


def default_app_action(context, parent, func):
    """Open paths with the OS-default app -> QAction"""
    action = cmd_action(
        parent, cmds.OpenDefaultApp, context, func, hotkeys.PRIMARY_ACTION
    )
    action.setIcon(icons.default_app())
    return action


def edit_action(context, parent, *keys):
    """Launch an editor -> QAction"""
    action = qtutils.add_action_with_tooltip(
        parent,
        cmds.LaunchEditor.name(),
        N_('Edit selected paths'),
        cmds.run(cmds.LaunchEditor, context),
        hotkeys.EDIT,
        *keys
    )
    action.setIcon(icons.edit())
    return action


def parent_dir_action(context, parent, func):
    """Open the parent directory of paths -> QAction"""
    hotkey = hotkeys.SECONDARY_ACTION
    action = cmd_action(parent, cmds.OpenParentDir, context, func, hotkey)
    action.setIcon(icons.folder())
    return action


def worktree_dir_action(context, parent, *keys):
    """Open the repository worktree -> QAction"""
    # lambda: None is a no-op.
    action = cmd_action(parent, cmds.OpenWorktree, context, lambda: None, *keys)
    action.setIcon(icons.folder())
    return action


def refresh_action(context, parent):
    """Refresh the repository state -> QAction"""
    return qtutils.add_action(
        parent, cmds.Refresh.name(), cmds.run(cmds.Refresh, context), hotkeys.REFRESH
    )


def terminal_action(context, parent, func=None, hotkey=None):
    """Launch a terminal -> QAction"""
    action = None
    if cmds.LaunchTerminal.is_available(context):
        if func is None:
            func = functools.partial(lambda: None)
        action = cmd_action(
            parent,
            cmds.LaunchTerminal,
            context,
            lambda: utils.select_directory(func()),
        )
        action.setIcon(icons.terminal())
        if hotkey is not None:
            action.setShortcut(hotkey)
    return action