summaryrefslogtreecommitdiffstats
path: root/pkg/gui/services/custom_commands/session_state_loader.go
blob: 42f3403ecbc4acf3d84cf5904a31ba6ddb3f11e0 (plain)
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
package custom_commands

import (
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/gui/context"
	"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
)

// loads the session state at the time that a custom command is invoked, for use
// in the custom command's template strings
type SessionStateLoader struct {
	contexts *context.ContextTree
	helpers  *helpers.Helpers
}

func NewSessionStateLoader(contexts *context.ContextTree, helpers *helpers.Helpers) *SessionStateLoader {
	return &SessionStateLoader{
		contexts: contexts,
		helpers:  helpers,
	}
}

// SessionState captures the current state of the application for use in custom commands
type SessionState struct {
	SelectedLocalCommit    *models.Commit
	SelectedReflogCommit   *models.Commit
	SelectedSubCommit      *models.Commit
	SelectedFile           *models.File
	SelectedPath           string
	SelectedLocalBranch    *models.Branch
	SelectedRemoteBranch   *models.RemoteBranch
	SelectedRemote         *models.Remote
	SelectedTag            *models.Tag
	SelectedStashEntry     *models.StashEntry
	SelectedCommitFile     *models.CommitFile
	SelectedCommitFilePath string
	CheckedOutBranch       *models.Branch
}

func (self *SessionStateLoader) call() *SessionState {
	return &SessionState{
		SelectedFile:           self.contexts.Files.GetSelectedFile(),
		SelectedPath:           self.contexts.Files.GetSelectedPath(),
		SelectedLocalCommit:    self.contexts.LocalCommits.GetSelected(),
		SelectedReflogCommit:   self.contexts.ReflogCommits.GetSelected(),
		SelectedLocalBranch:    self.contexts.Branches.GetSelected(),
		SelectedRemoteBranch:   self.contexts.RemoteBranches.GetSelected(),
		SelectedRemote:         self.contexts.Remotes.GetSelected(),
		SelectedTag:            self.contexts.Tags.GetSelected(),
		SelectedStashEntry:     self.contexts.Stash.GetSelected(),
		SelectedCommitFile:     self.contexts.CommitFiles.GetSelectedFile(),
		SelectedCommitFilePath: self.contexts.CommitFiles.GetSelectedPath(),
		SelectedSubCommit:      self.contexts.SubCommits.GetSelected(),
		CheckedOutBranch:       self.helpers.Refs.GetCheckedOutRef(),
	}
}