diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-03-03 22:41:35 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-03-04 00:12:23 +1100 |
commit | b3522c48d9377fec9397581cf2ab439c92590c20 (patch) | |
tree | b6d6be1d6eee091cc4cb6c7a4dc3cc96699a04fb /pkg/gui/pty.go | |
parent | 0fc58a79867ae91ce8c0374ecfe95407b621ce55 (diff) |
refactor
Diffstat (limited to 'pkg/gui/pty.go')
-rw-r--r-- | pkg/gui/pty.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/pkg/gui/pty.go b/pkg/gui/pty.go index 18fb98fda..39642b7f0 100644 --- a/pkg/gui/pty.go +++ b/pkg/gui/pty.go @@ -1,6 +1,10 @@ +// +build !windows + package gui import ( + "os/exec" + "github.com/jesseduffield/pty" ) @@ -19,3 +23,52 @@ func (gui *Gui) onResize() error { return nil } + +// 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. +func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd) error { + width, _ := gui.getMainView().Size() + pager := gui.GitCommand.GetPager(width) + + if pager == "" { + // if we're not using a custom pager we don't need to use a pty + return gui.newCmdTask(viewName, cmd) + } + + cmd.Env = append(cmd.Env, "GIT_PAGER="+pager) + + view, err := gui.g.View(viewName) + if err != nil { + return nil // swallowing for now + } + + _, height := view.Size() + _, oy := view.Origin() + + manager := gui.getManager(view) + + ptmx, err := pty.Start(cmd) + if err != nil { + return err + } + + gui.State.Ptmx = ptmx + onClose := func() { + ptmx.Close() + gui.State.Ptmx = nil + } + + if err := gui.onResize(); err != nil { + return err + } + + if err := manager.NewTask(manager.NewCmdTask(ptmx, cmd, height+oy+10, onClose)); err != nil { + return err + } + + return nil +} |