run subprocess cleanly

This commit is contained in:
Jesse Duffield 2018-08-07 18:05:43 +10:00
parent 9067c3be3e
commit f6a9c727fa
3 changed files with 33 additions and 20 deletions

View file

@ -349,15 +349,14 @@ func getOpenCommand() (string, string) {
}
func runSubProcess(g *gocui.Gui, cmdName string, commandArgs ...string) {
// TODO: find a way to wait for the subprocess without having to
// close and reinitialize the gui
// g.Close() // TODO: find a way to make close properly after uncommenting
cmd := exec.Command(cmdName, commandArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
run() // start another Gui
subprocess = exec.Command(cmdName, commandArgs...)
subprocess.Stdin = os.Stdin
subprocess.Stdout = os.Stdout
subprocess.Stderr = os.Stderr
g.Update(func(g *gocui.Gui) error {
return ErrSubprocess
})
}
func getBranchDiff(branch string, baseBranch string) (string, error) {

14
gui.go
View file

@ -5,7 +5,6 @@ import (
// "io"
// "io/ioutil"
"log"
"strings"
"time"
@ -321,10 +320,10 @@ func updateLoader(g *gocui.Gui) {
}
}
func run() {
func run() (err error) {
g, err := gocui.NewGui(gocui.OutputNormal, OverlappingEdges)
if err != nil {
log.Panicln(err)
return
}
defer g.Close()
@ -343,13 +342,12 @@ func run() {
g.SetManagerFunc(layout)
if err := keybindings(g); err != nil {
log.Panicln(err)
if err = keybindings(g); err != nil {
return
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
err = g.MainLoop()
return
}
func quit(g *gocui.Gui, v *gocui.View) error {

22
main.go
View file

@ -1,19 +1,25 @@
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/user"
"time"
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
)
// ErrSubProcess is raised when we are running a subprocess
var (
startTime time.Time
debugging bool
startTime time.Time
debugging bool
ErrSubprocess = errors.New("running subprocess")
subprocess *exec.Cmd
)
func homeDirectory() string {
@ -65,5 +71,15 @@ func main() {
startTime = time.Now()
verifyInGitRepo()
navigateToRepoRootDirectory()
run()
for {
if err := run(); err != nil {
if err == gocui.ErrQuit {
break
} else if err == ErrSubprocess {
subprocess.Run()
} else {
log.Panicln(err)
}
}
}
}