mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-21 04:46:29 +03:00
281 lines
9.8 KiB
Go
281 lines
9.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"io"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
"github.com/jesseduffield/lazygit/pkg/tasks"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (gui *Gui) newCmdTask(view *gocui.View, cmd *exec.Cmd, prefix types.Prefix, contentTag func() any) error {
|
|
cmdStr := strings.Join(cmd.Args, " ")
|
|
gui.c.Log.WithField(
|
|
"command",
|
|
cmdStr,
|
|
).Debug("RunCommand")
|
|
|
|
manager := gui.getManager(view)
|
|
// The task is only created after the layout (see below), but it has to take
|
|
// its place among the view's tasks now. Otherwise a task asked for after this
|
|
// one, but created before the layout, would be replaced by it.
|
|
reservation := manager.ReserveTask()
|
|
|
|
// Mark the view as loading synchronously (before the task's goroutine runs
|
|
// and before the next layout pass) so the layout doesn't clamp the scroll
|
|
// position to the not-yet-loaded content.
|
|
manager.StartLoading()
|
|
// Hold the scrollbar at the height the view has now (the previous render),
|
|
// while it still shows that render: once the re-render swaps in its first
|
|
// partial paint the displayed buffer is briefly short, and we don't want the
|
|
// thumb to shrink and snap back as the rest loads.
|
|
view.FreezeScrollbarHeight()
|
|
|
|
// The command lays its output out to the width of the view (git's diffstat
|
|
// graph does), and only the layout settles that, so the task is created after
|
|
// it, as a diff renderer's is. Taking the width there, on the UI thread, also
|
|
// keeps the task goroutine from reading the view's live dimensions while it
|
|
// streams output.
|
|
gui.afterLayout(func() error {
|
|
if manager.IsSuperseded(reservation) {
|
|
return nil
|
|
}
|
|
|
|
spec := renderSpec{view: view, cmd: cmd, width: gui.renderWidth(view)}
|
|
return gui.newTaskForRender(reservation, spec, prefix, contentTag, cmdStr, gui.plainRender)
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
// plainRender runs the command as it is, with its output going straight into
|
|
// a pipe.
|
|
func (gui *Gui) plainRender(spec renderSpec) (startRender, onCloseRender) {
|
|
var r io.ReadCloser
|
|
start := func() (tasks.Cmd, io.Reader) {
|
|
// The view wraps to this width; apply it here, on the task's goroutine
|
|
// once the previous task has stopped, so it doesn't race that task's
|
|
// still-running writes (see View.SetContentWidth).
|
|
spec.view.SetContentWidth(spec.width)
|
|
|
|
execCmd, pipe := startCmdWithPipe(spec.cmd, gui.c.Log)
|
|
r = pipe
|
|
return execCmd, pipe
|
|
}
|
|
|
|
onClose := func() {
|
|
if r != nil {
|
|
r.Close()
|
|
r = nil
|
|
}
|
|
}
|
|
|
|
return start, onClose
|
|
}
|
|
|
|
// startCmdWithPipe starts cmd with its stdout and stderr going to a single
|
|
// pipe, and returns the command along with the pipe's read end, in the shape
|
|
// that NewCmdTask expects from its start func. It never returns a nil reader,
|
|
// because NewCmdTask's scanner panics on one: when the pipe can't be created
|
|
// the command isn't started at all, and an empty reader is returned so that
|
|
// the task shuts down cleanly with the error in the log.
|
|
func startCmdWithPipe(cmd *exec.Cmd, log *logrus.Entry) (tasks.Cmd, io.ReadCloser) {
|
|
r, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
log.Error(err)
|
|
return tasks.ExecCmd{Cmd: cmd}, io.NopCloser(strings.NewReader(""))
|
|
}
|
|
cmd.Stderr = cmd.Stdout
|
|
// A render's command writes to the view, not to lazygit's terminal. A diff
|
|
// renderer that found that terminal would lay itself out to the width of the
|
|
// whole window rather than to the width of the view, which it is told in
|
|
// COLUMNS.
|
|
oscommands.DetachFromTerminal(cmd)
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
log.Error(err)
|
|
}
|
|
|
|
return tasks.ExecCmd{Cmd: cmd}, r
|
|
}
|
|
|
|
func (gui *Gui) newStringTask(view *gocui.View, str string) error {
|
|
// using str so that if rendering the exact same thing we don't reset the origin
|
|
return gui.newStringTaskWithKey(view, str, str)
|
|
}
|
|
|
|
func (gui *Gui) newStringTaskWithoutScroll(view *gocui.View, str string) error {
|
|
manager := gui.getManager(view)
|
|
// Whatever the view was going to be put back to belonged to a re-render of its
|
|
// content; this is a message instead, so there is nothing to put back.
|
|
manager.DropRestoreForNextTask()
|
|
|
|
f := func(tasks.TaskOpts) error {
|
|
return gui.g.OnUIThreadAndWaitBackground(func() {
|
|
gui.c.SetViewContent(view, str)
|
|
gui.updateDiffPaneDecorations(view, true)
|
|
gui.reApplySearch(view)
|
|
})
|
|
}
|
|
|
|
if err := manager.NewTask(f, manager.GetTaskKey()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (gui *Gui) newStringTaskWithScroll(view *gocui.View, str string, originX int, originY int) error {
|
|
manager := gui.getManager(view)
|
|
// Whatever the view was going to be put back to belonged to a re-render of its
|
|
// content; this is a message instead, so there is nothing to put back.
|
|
manager.DropRestoreForNextTask()
|
|
|
|
f := func(tasks.TaskOpts) error {
|
|
return gui.g.OnUIThreadAndWaitBackground(func() {
|
|
gui.c.SetViewContent(view, str)
|
|
view.SetOrigin(originX, originY)
|
|
gui.updateDiffPaneDecorations(view, true)
|
|
gui.reApplySearch(view)
|
|
})
|
|
}
|
|
|
|
if err := manager.NewTask(f, manager.GetTaskKey()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (gui *Gui) newStringTaskWithKey(view *gocui.View, str string, key string) error {
|
|
manager := gui.getManager(view)
|
|
// Whatever the view was going to be put back to belonged to a re-render of its
|
|
// content; this is a message instead, so there is nothing to put back.
|
|
manager.DropRestoreForNextTask()
|
|
|
|
f := func(tasks.TaskOpts) error {
|
|
return gui.g.OnUIThreadAndWaitBackground(func() {
|
|
gui.c.ResetViewOrigin(view)
|
|
gui.c.SetViewContent(view, str)
|
|
gui.updateDiffPaneDecorations(view, true)
|
|
gui.reApplySearch(view)
|
|
})
|
|
}
|
|
|
|
if err := manager.NewTask(f, key); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// contentWriter returns what a render of the given view writes its content to: the
|
|
// view itself, or, for a pane of the main section, the writer that links the files
|
|
// named in the diffstat on its way there (see DiffStatLinkWriter).
|
|
func (gui *Gui) contentWriter(view *gocui.View) io.Writer {
|
|
if gui.mainContextForView(view) == nil {
|
|
return view
|
|
}
|
|
return gui.diffStatLinkWriter(view)
|
|
}
|
|
|
|
// diffStatLinkWriter returns the writer that links the diffstat of the given pane,
|
|
// making it if the pane hasn't rendered yet. It lasts as long as the view does, and
|
|
// each render tells it what to make of that render (see DiffStatLinkWriter.BeginRender).
|
|
func (gui *Gui) diffStatLinkWriter(view *gocui.View) *helpers.DiffStatLinkWriter {
|
|
writer, ok := gui.diffStatLinkWriterMap[view.Name()]
|
|
if !ok {
|
|
writer = helpers.NewDiffStatLinkWriter(view)
|
|
gui.diffStatLinkWriterMap[view.Name()] = writer
|
|
}
|
|
return writer
|
|
}
|
|
|
|
func (gui *Gui) getManager(view *gocui.View) *tasks.ViewBufferManager {
|
|
manager, ok := gui.viewBufferManagerMap[view.Name()]
|
|
if !ok {
|
|
manager = tasks.NewViewBufferManager(
|
|
gui.Log,
|
|
gui.contentWriter(view),
|
|
func() {
|
|
// Called before showing the "loading..." indicator: clear the
|
|
// displayed buffer so only "loading..." is shown. The actual content
|
|
// is rendered off-screen (beginRender below) and swapped in, so it
|
|
// never overwrites the displayed buffer incrementally.
|
|
view.Reset()
|
|
},
|
|
func() {
|
|
// As the task reads more lines, the only thing that changes is the
|
|
// view's content (and its scrollbar); the window layout doesn't. So a
|
|
// content-only render is enough — it skips the layout pass and redraws
|
|
// only the cells that differ — and it's much cheaper than a full
|
|
// layout-and-redraw on every read, which matters a lot when reading a
|
|
// long diff, where reads happen repeatedly as the user scrolls.
|
|
//
|
|
// What this draws is more of the content than the pane held a moment
|
|
// ago, so it is also where what is drawn over that content is worked
|
|
// out again. The screenful the first paint reveals may not be enough
|
|
// to say whether there is anything to select, and for a diff that
|
|
// opens with a long diffstat it isn't.
|
|
gui.c.OnUIThreadContentOnly(func() error {
|
|
gui.updateDiffPaneDecorations(view, false)
|
|
return nil
|
|
})
|
|
},
|
|
func() {
|
|
// The content is fully loaded now, so let the scrollbar track it
|
|
// directly again (it was held at the previous render's height while
|
|
// loading, see FreezeScrollbarHeight).
|
|
view.UnfreezeScrollbarHeight()
|
|
|
|
// Need to check if the content of the view is well past the origin.
|
|
linesHeight := view.ViewLinesHeight()
|
|
_, originY := view.Origin()
|
|
if linesHeight < originY {
|
|
newOriginY := linesHeight
|
|
|
|
view.SetOriginY(newOriginY)
|
|
}
|
|
view.ClampOriginXToContent()
|
|
|
|
gui.updateDiffPaneDecorations(view, true)
|
|
gui.clampDiffSelectionToContent(view)
|
|
gui.reApplySearch(view)
|
|
},
|
|
func() {
|
|
view.SetOrigin(0, 0)
|
|
},
|
|
view.BeginOffscreenRender,
|
|
func() {
|
|
view.SwapInOffscreenRender()
|
|
|
|
// The content the pane is being given is on display from here on, so
|
|
// what is drawn over it is settled against that content rather than
|
|
// against the render before it.
|
|
gui.updateDiffPaneDecorations(view, false)
|
|
},
|
|
func() gocui.Task {
|
|
// A background task: rendering content into a view is display
|
|
// work, not lazygit driving a git operation, so it must not
|
|
// count towards being busy and block a repo switch. These
|
|
// renders fire on nearly every focus/selection change, including
|
|
// the context activation that happens right before a menu/prompt
|
|
// handler runs (e.g. confirming worktree creation), which would
|
|
// otherwise make the switch that handler triggers refuse itself.
|
|
return gui.c.GocuiGui().NewBackgroundTask()
|
|
},
|
|
// Rendering is background work too (see above), so the view mutations
|
|
// it bounces onto the UI thread mustn't count towards being busy.
|
|
gui.g.OnUIThreadAndWaitBackground,
|
|
)
|
|
gui.viewBufferManagerMap[view.Name()] = manager
|
|
}
|
|
|
|
return manager
|
|
}
|