2021-09-16 15:38:43 +02:00
|
|
|
//go:build !windows
|
2020-03-03 13:41:35 +02:00
|
|
|
// +build !windows
|
|
|
|
|
2020-03-01 03:30:48 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2021-11-02 07:39:15 +02:00
|
|
|
"io"
|
2022-06-13 03:01:26 +02:00
|
|
|
"os"
|
2020-03-03 13:41:35 +02:00
|
|
|
"os/exec"
|
2021-10-17 10:01:02 +02:00
|
|
|
"strings"
|
2020-03-03 13:41:35 +02:00
|
|
|
|
2020-03-25 11:37:10 +02:00
|
|
|
"github.com/creack/pty"
|
2021-04-04 16:31:52 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2022-08-06 03:48:08 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2020-03-01 03:30:48 +02:00
|
|
|
)
|
|
|
|
|
2021-11-22 11:58:40 +02:00
|
|
|
func (gui *Gui) desiredPtySize() *pty.Winsize {
|
|
|
|
width, height := gui.Views.Main.Size()
|
|
|
|
|
|
|
|
return &pty.Winsize{Cols: uint16(width), Rows: uint16(height)}
|
|
|
|
}
|
|
|
|
|
2020-03-01 03:30:48 +02:00
|
|
|
func (gui *Gui) onResize() error {
|
2022-06-13 03:01:26 +02:00
|
|
|
gui.Mutexes.PtyMutex.Lock()
|
|
|
|
defer gui.Mutexes.PtyMutex.Unlock()
|
|
|
|
|
2022-08-06 03:48:08 +02:00
|
|
|
for _, ptmx := range gui.viewPtmxMap {
|
|
|
|
// TODO: handle resizing properly: we need to actually clear the main view
|
|
|
|
// and re-read the output from our pty. Or we could just re-run the original
|
|
|
|
// command from scratch
|
|
|
|
if err := pty.Setsize(ptmx, gui.desiredPtySize()); err != nil {
|
|
|
|
return utils.WrapError(err)
|
|
|
|
}
|
2020-03-01 03:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-03 13:41:35 +02:00
|
|
|
|
|
|
|
// Some commands need to output for a terminal to active certain behaviour.
|
|
|
|
// For example, git won't invoke the GIT_PAGER env var unless it thinks it's
|
|
|
|
// talking to a terminal. We typically write cmd outputs straight to a view,
|
|
|
|
// which is just an io.Reader. the pty package lets us wrap a command in a
|
|
|
|
// pseudo-terminal meaning we'll get the behaviour we want from the underlying
|
|
|
|
// command.
|
2021-04-04 16:31:52 +02:00
|
|
|
func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error {
|
2021-04-04 15:51:59 +02:00
|
|
|
width, _ := gui.Views.Main.Size()
|
2022-01-16 05:46:53 +02:00
|
|
|
pager := gui.git.Config.GetPager(width)
|
2023-11-22 04:18:19 +02:00
|
|
|
externalDiffCommand := gui.Config.GetUserConfig().Git.Paging.ExternalDiffCommand
|
2020-03-03 13:41:35 +02:00
|
|
|
|
2023-11-22 04:18:19 +02:00
|
|
|
if pager == "" && externalDiffCommand == "" {
|
2020-03-03 13:41:35 +02:00
|
|
|
// if we're not using a custom pager we don't need to use a pty
|
2021-04-04 16:31:52 +02:00
|
|
|
return gui.newCmdTask(view, cmd, prefix)
|
2020-03-03 13:41:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:01:02 +02:00
|
|
|
cmdStr := strings.Join(cmd.Args, " ")
|
|
|
|
|
2020-03-03 13:41:35 +02:00
|
|
|
cmd.Env = append(cmd.Env, "GIT_PAGER="+pager)
|
|
|
|
|
|
|
|
manager := gui.getManager(view)
|
|
|
|
|
2022-06-13 03:01:26 +02:00
|
|
|
var ptmx *os.File
|
2021-11-02 07:39:15 +02:00
|
|
|
start := func() (*exec.Cmd, io.Reader) {
|
2022-06-13 03:01:26 +02:00
|
|
|
var err error
|
|
|
|
ptmx, err = pty.StartWithSize(cmd, gui.desiredPtySize())
|
2021-11-02 07:39:15 +02:00
|
|
|
if err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.c.Log.Error(err)
|
2021-11-02 07:39:15 +02:00
|
|
|
}
|
|
|
|
|
2022-06-13 03:01:26 +02:00
|
|
|
gui.Mutexes.PtyMutex.Lock()
|
2022-08-06 03:48:08 +02:00
|
|
|
gui.viewPtmxMap[view.Name()] = ptmx
|
2022-06-13 03:01:26 +02:00
|
|
|
gui.Mutexes.PtyMutex.Unlock()
|
2021-11-02 07:39:15 +02:00
|
|
|
|
|
|
|
return cmd, ptmx
|
2020-03-03 13:41:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onClose := func() {
|
2022-06-13 03:01:26 +02:00
|
|
|
gui.Mutexes.PtyMutex.Lock()
|
|
|
|
ptmx.Close()
|
2022-08-06 03:48:08 +02:00
|
|
|
delete(gui.viewPtmxMap, view.Name())
|
2022-06-13 03:01:26 +02:00
|
|
|
gui.Mutexes.PtyMutex.Unlock()
|
2020-03-03 13:41:35 +02:00
|
|
|
}
|
|
|
|
|
2023-03-10 14:35:19 +02:00
|
|
|
linesToRead := gui.linesToReadFromCmdTask(view)
|
|
|
|
if err := manager.NewTask(manager.NewCmdTask(start, prefix, linesToRead, onClose), cmdStr); err != nil {
|
2020-03-03 13:41:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|