File: client.go

package info (click to toggle)
lazygit 0.50.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,808 kB
  • sloc: sh: 128; makefile: 76
file content (121 lines) | stat: -rw-r--r-- 3,796 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
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
package custom_commands

import (
	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/config"
	"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
	"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
	"github.com/jesseduffield/lazygit/pkg/i18n"
	"github.com/samber/lo"
)

// Client is the entry point to this package. It returns a list of keybindings based on the config's user-defined custom commands.
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_Command_Keybindings.md for more info.
type Client struct {
	c                 *helpers.HelperCommon
	handlerCreator    *HandlerCreator
	keybindingCreator *KeybindingCreator
}

func NewClient(
	c *helpers.HelperCommon,
	helpers *helpers.Helpers,
) *Client {
	sessionStateLoader := NewSessionStateLoader(c, helpers.Refs)
	handlerCreator := NewHandlerCreator(
		c,
		sessionStateLoader,
		helpers.Suggestions,
		helpers.MergeAndRebase,
	)
	keybindingCreator := NewKeybindingCreator(c)

	return &Client{
		c:                 c,
		keybindingCreator: keybindingCreator,
		handlerCreator:    handlerCreator,
	}
}

func (self *Client) GetCustomCommandKeybindings() ([]*types.Binding, error) {
	bindings := []*types.Binding{}
	for _, customCommand := range self.c.UserConfig().CustomCommands {
		if len(customCommand.CommandMenu) > 0 {
			handler := func() error {
				return self.showCustomCommandsMenu(customCommand)
			}
			bindings = append(bindings, &types.Binding{
				ViewName:    "", // custom commands menus are global; we filter the commands inside by context
				Key:         keybindings.GetKey(customCommand.Key),
				Modifier:    gocui.ModNone,
				Handler:     handler,
				Description: getCustomCommandsMenuDescription(customCommand, self.c.Tr),
				OpensMenu:   true,
			})
		} else {
			handler := self.handlerCreator.call(customCommand)
			compoundBindings, err := self.keybindingCreator.call(customCommand, handler)
			if err != nil {
				return nil, err
			}
			bindings = append(bindings, compoundBindings...)
		}
	}

	return bindings, nil
}

func (self *Client) showCustomCommandsMenu(customCommand config.CustomCommand) error {
	menuItems := make([]*types.MenuItem, 0, len(customCommand.CommandMenu))
	for _, subCommand := range customCommand.CommandMenu {
		if len(subCommand.CommandMenu) > 0 {
			handler := func() error {
				return self.showCustomCommandsMenu(subCommand)
			}
			menuItems = append(menuItems, &types.MenuItem{
				Label:     subCommand.GetDescription(),
				Key:       keybindings.GetKey(subCommand.Key),
				OnPress:   handler,
				OpensMenu: true,
			})
		} else {
			if subCommand.Context != "" && subCommand.Context != "global" {
				viewNames, err := self.keybindingCreator.getViewNamesAndContexts(subCommand)
				if err != nil {
					return err
				}

				currentView := self.c.GocuiGui().CurrentView()
				enabled := currentView != nil && lo.Contains(viewNames, currentView.Name())
				if !enabled {
					continue
				}
			}

			menuItems = append(menuItems, &types.MenuItem{
				Label:   subCommand.GetDescription(),
				Key:     keybindings.GetKey(subCommand.Key),
				OnPress: self.handlerCreator.call(subCommand),
			})
		}
	}

	if len(menuItems) == 0 {
		menuItems = append(menuItems, &types.MenuItem{
			Label:   self.c.Tr.NoApplicableCommandsInThisContext,
			OnPress: func() error { return nil },
		})
	}

	title := getCustomCommandsMenuDescription(customCommand, self.c.Tr)
	return self.c.Menu(types.CreateMenuOptions{Title: title, Items: menuItems, HideCancel: true})
}

func getCustomCommandsMenuDescription(customCommand config.CustomCommand, tr *i18n.TranslationSet) string {
	if customCommand.Description != "" {
		return customCommand.Description
	}

	return tr.CustomCommands
}