1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/pty.go

90 lines
2.3 KiB
Go
Raw Normal View History

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"
"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"
"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 {
gui.Mutexes.PtyMutex.Lock()
defer gui.Mutexes.PtyMutex.Unlock()
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()
pager := gui.git.Config.GetPager(width)
externalDiffCommand := gui.Config.GetUserConfig().Git.Paging.ExternalDiffCommand
2020-03-03 13:41:35 +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)
var ptmx *os.File
2021-11-02 07:39:15 +02:00
start := func() (*exec.Cmd, io.Reader) {
var err error
ptmx, err = pty.StartWithSize(cmd, gui.desiredPtySize())
2021-11-02 07:39:15 +02:00
if err != nil {
gui.c.Log.Error(err)
2021-11-02 07:39:15 +02:00
}
gui.Mutexes.PtyMutex.Lock()
gui.viewPtmxMap[view.Name()] = ptmx
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() {
gui.Mutexes.PtyMutex.Lock()
ptmx.Close()
delete(gui.viewPtmxMap, view.Name())
gui.Mutexes.PtyMutex.Unlock()
2020-03-03 13:41:35 +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
}