From 0bebfe454e99264cfab7d97c8f861536e1365c6f Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 11 Apr 2021 19:17:18 +1000 Subject: [PATCH] pull out function --- pkg/gui/gui.go | 51 ++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 153c663f8..f6c273859 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -473,30 +473,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom gui.watchFilesForChanges() - onRunCommand := func() func(entry oscommands.CmdLogEntry) { - currentSpan := "" - - return func(entry oscommands.CmdLogEntry) { - if gui.Views.Extras == nil { - return - } - - gui.Views.Extras.Autoscroll = true - - if entry.GetSpan() != currentSpan { - fmt.Fprintln(gui.Views.Extras, utils.ColoredString(entry.GetSpan(), color.FgYellow)) - currentSpan = entry.GetSpan() - } - - clrAttr := theme.DefaultTextColor - if !entry.GetCommandLine() { - clrAttr = color.FgMagenta - } - gui.CmdLog = append(gui.CmdLog, entry.GetCmdStr()) - indentedCmdStr := " " + strings.Replace(entry.GetCmdStr(), "\n", "\n ", -1) - fmt.Fprintln(gui.Views.Extras, utils.ColoredString(indentedCmdStr, clrAttr)) - } - }() + onRunCommand := gui.GetOnRunCommand() oSCommand.SetOnRunCommand(onRunCommand) gui.OnRunCommand = onRunCommand @@ -504,6 +481,32 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom return gui, nil } +func (gui *Gui) GetOnRunCommand() func(entry oscommands.CmdLogEntry) { + // closing over this so that nobody else can modify it + currentSpan := "" + + return func(entry oscommands.CmdLogEntry) { + if gui.Views.Extras == nil { + return + } + + gui.Views.Extras.Autoscroll = true + + if entry.GetSpan() != currentSpan { + fmt.Fprintln(gui.Views.Extras, utils.ColoredString(entry.GetSpan(), color.FgYellow)) + currentSpan = entry.GetSpan() + } + + clrAttr := theme.DefaultTextColor + if !entry.GetCommandLine() { + clrAttr = color.FgMagenta + } + gui.CmdLog = append(gui.CmdLog, entry.GetCmdStr()) + indentedCmdStr := " " + strings.Replace(entry.GetCmdStr(), "\n", "\n ", -1) + fmt.Fprintln(gui.Views.Extras, utils.ColoredString(indentedCmdStr, clrAttr)) + } +} + // Run setup the gui with keybindings and start the mainloop func (gui *Gui) Run() error { recordEvents := recordingEvents()