File: extras_panel.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 (116 lines) | stat: -rw-r--r-- 3,133 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
package gui

import (
	"io"

	"github.com/jesseduffield/lazygit/pkg/gui/context"
	"github.com/jesseduffield/lazygit/pkg/gui/style"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

func (gui *Gui) handleCreateExtrasMenuPanel() error {
	return gui.c.Menu(types.CreateMenuOptions{
		Title: gui.c.Tr.CommandLog,
		Items: []*types.MenuItem{
			{
				Label: gui.c.Tr.ToggleShowCommandLog,
				OnPress: func() error {
					currentContext := gui.c.Context().CurrentStatic()
					if gui.c.State().GetShowExtrasWindow() && currentContext.GetKey() == context.COMMAND_LOG_CONTEXT_KEY {
						gui.c.Context().Pop()
					}
					show := !gui.c.State().GetShowExtrasWindow()
					gui.c.State().SetShowExtrasWindow(show)
					gui.c.GetAppState().HideCommandLog = !show
					gui.c.SaveAppStateAndLogError()
					return nil
				},
			},
			{
				Label:   gui.c.Tr.FocusCommandLog,
				OnPress: gui.handleFocusCommandLog,
			},
		},
	})
}

func (gui *Gui) handleFocusCommandLog() error {
	gui.c.State().SetShowExtrasWindow(true)
	// TODO: is this necessary? Can't I just call 'return from context'?
	gui.State.Contexts.CommandLog.SetParentContext(gui.c.Context().CurrentSide())
	gui.c.Context().Push(gui.State.Contexts.CommandLog, types.OnFocusOpts{})
	return nil
}

func (gui *Gui) scrollUpExtra() error {
	gui.Views.Extras.Autoscroll = false

	gui.scrollUpView(gui.Views.Extras)

	return nil
}

func (gui *Gui) scrollDownExtra() error {
	gui.Views.Extras.Autoscroll = false

	gui.scrollDownView(gui.Views.Extras)

	return nil
}

func (gui *Gui) pageUpExtrasPanel() error {
	gui.Views.Extras.Autoscroll = false

	gui.Views.Extras.ScrollUp(gui.Contexts().CommandLog.GetViewTrait().PageDelta())

	return nil
}

func (gui *Gui) pageDownExtrasPanel() error {
	gui.Views.Extras.Autoscroll = false

	gui.Views.Extras.ScrollDown(gui.Contexts().CommandLog.GetViewTrait().PageDelta())

	return nil
}

func (gui *Gui) goToExtrasPanelTop() error {
	gui.Views.Extras.Autoscroll = false

	gui.Views.Extras.ScrollUp(gui.Views.Extras.ViewLinesHeight())

	return nil
}

func (gui *Gui) goToExtrasPanelBottom() error {
	gui.Views.Extras.Autoscroll = true

	gui.Views.Extras.ScrollDown(gui.Views.Extras.ViewLinesHeight())

	return nil
}

func (gui *Gui) getCmdWriter() io.Writer {
	return &prefixWriter{writer: gui.Views.Extras, prefix: style.FgMagenta.Sprintf("\n\n%s\n", gui.c.Tr.GitOutput)}
}

// Ensures that the first write is preceded by writing a prefix.
// This allows us to say 'Git output:' before writing the actual git output.
// We could just write directly to the view in this package before running the command but we already have code in the commands package that writes to the same view beforehand (with the command it's about to run) so things would be out of order.
type prefixWriter struct {
	prefix        string
	prefixWritten bool
	writer        io.Writer
}

func (self *prefixWriter) Write(p []byte) (int, error) {
	if !self.prefixWritten {
		self.prefixWritten = true
		// assuming we can write this prefix in one go
		n, err := self.writer.Write([]byte(self.prefix))
		if err != nil {
			return n, err
		}
	}
	return self.writer.Write(p)
}