1package gui
2
3import (
4	"io"
5
6	"github.com/jesseduffield/lazygit/pkg/gui/style"
7)
8
9func (gui *Gui) handleCreateExtrasMenuPanel() error {
10	menuItems := []*menuItem{
11		{
12			displayString: gui.Tr.ToggleShowCommandLog,
13			onPress: func() error {
14				currentContext := gui.currentStaticContext()
15				if gui.ShowExtrasWindow && currentContext.GetKey() == COMMAND_LOG_CONTEXT_KEY {
16					if err := gui.returnFromContext(); err != nil {
17						return err
18					}
19				}
20				show := !gui.ShowExtrasWindow
21				gui.ShowExtrasWindow = show
22				gui.Config.GetAppState().HideCommandLog = !show
23				_ = gui.Config.SaveAppState()
24				return nil
25			},
26		},
27		{
28			displayString: gui.Tr.FocusCommandLog,
29			onPress:       gui.handleFocusCommandLog,
30		},
31	}
32
33	return gui.createMenu(gui.Tr.CommandLog, menuItems, createMenuOptions{showCancel: true})
34}
35
36func (gui *Gui) handleFocusCommandLog() error {
37	gui.ShowExtrasWindow = true
38	gui.State.Contexts.CommandLog.SetParentContext(gui.currentSideContext())
39	return gui.pushContext(gui.State.Contexts.CommandLog)
40}
41
42func (gui *Gui) scrollUpExtra() error {
43	gui.Views.Extras.Autoscroll = false
44
45	return gui.scrollUpView(gui.Views.Extras)
46}
47
48func (gui *Gui) scrollDownExtra() error {
49	gui.Views.Extras.Autoscroll = false
50
51	if err := gui.scrollDownView(gui.Views.Extras); err != nil {
52		return err
53	}
54
55	return nil
56}
57
58func (gui *Gui) getCmdWriter() io.Writer {
59	return &prefixWriter{writer: gui.Views.Extras, prefix: style.FgMagenta.Sprintf("\n\n%s\n", gui.Tr.GitOutput)}
60}
61
62// Ensures that the first write is preceded by writing a prefix.
63// This allows us to say 'Git output:' before writing the actual git output.
64// 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.
65type prefixWriter struct {
66	prefix        string
67	prefixWritten bool
68	writer        io.Writer
69}
70
71func (self *prefixWriter) Write(p []byte) (n int, err error) {
72	if !self.prefixWritten {
73		self.prefixWritten = true
74		// assuming we can write this prefix in one go
75		_, err = self.writer.Write([]byte(self.prefix))
76		if err != nil {
77			return
78		}
79	}
80	return self.writer.Write(p)
81}
82