diff --git a/pkg/oscommand/oscommand.go b/pkg/oscommand/oscommand.go index b0d3d6a..01508ae 100644 --- a/pkg/oscommand/oscommand.go +++ b/pkg/oscommand/oscommand.go @@ -6,8 +6,6 @@ import ( "os/exec" "strings" - "github.com/k0kubun/pp" - "github.com/sirupsen/logrus" ) @@ -23,17 +21,20 @@ func NewOSCommand(openCommandEnv string, logger *logrus.Entry) *OSCommand { // OpenFileAtLineColumn opens a file at a specific line and column. func (o *OSCommand) OpenFileAtLineColumn(filename string, line, column int) error { command := specifyLineColumn(o.openCommand(), filename, line, column) - _, err := o.runCommand(command[0], command[1:]...) - return err + //_, err := o.runCommand(command[0], command[1:]...) + return o.runSubprocess(command[0], command[1:]...) + //return err } +// openCommand returns an executable editor command. +// Falling back to environment variable for golintui, EDITOR then vi. func (o *OSCommand) openCommand() string { executable := os.Getenv(o.OpenCommandEnv) if executable == "" { executable = os.Getenv("EDITOR") } if executable == "" { - vi, err := o.runCommand("which", "vi") + vi, err := exec.LookPath("vi") if err != nil { o.Logger.Error("failed to get path to vi", err) } @@ -53,20 +54,20 @@ func specifyLineColumn(command, filename string, line, column int) []string { res := strings.Split(command, " ") switch res[0] { case "vi": - s := fmt.Sprintf("\"+normal %dG%d|\" %s", line, column, filename) - res = append(res, strings.Split(s, " ")...) + args := fmt.Sprintf("+%d %s", line, filename) + res = append(res, strings.Split(args, " ")...) case "vim": - s := fmt.Sprintf("\"+normal %dG%d|\" %s", line, column, filename) - res = append(res, strings.Split(s, " ")...) + args := fmt.Sprintf("+%d %s", line, filename) + res = append(res, strings.Split(args, " ")...) case "nvim": - s := fmt.Sprintf("\"+normal %dG%d|\" %s", line, column, filename) - res = append(res, strings.Split(s, " ")...) + args := fmt.Sprintf("+%d %s", line, filename) + res = append(res, strings.Split(args, " ")...) case "emacs": - s := fmt.Sprintf("+%d:%d %s", line, column, filename) - res = append(res, strings.Split(s, " ")...) + args := fmt.Sprintf("+%d:%d %s", line, column, filename) + res = append(res, strings.Split(args, " ")...) case "code": - s := fmt.Sprintf("--goto %s:%d:%d", filename, line, column) - res = append(res, strings.Split(s, " ")...) + args := fmt.Sprintf("--goto %s:%d:%d", filename, line, column) + res = append(res, strings.Split(args, " ")...) default: // Don't specify when using unsupported editor. res = append(res, filename) @@ -75,7 +76,33 @@ func specifyLineColumn(command, filename string, line, column int) []string { } func (o *OSCommand) runCommand(executable string, args ...string) ([]byte, error) { - pp.Println("command:", executable, "args:", args) cmd := exec.Command(executable, args...) return cmd.CombinedOutput() } + +func (o *OSCommand) runSubprocess(executable string, args ...string) error { + cmd := exec.Command(executable, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stdout + cmd.Stdin = os.Stdin + cmd.Env = os.Environ() + + //fmt.Fprintf(os.Stdout, "\n%s\n\n", utils.ColoredString("+ "+strings.Join(cmd.Args, " "), color.FgBlue)) + + o.Logger.Info("now start running!") + if err := cmd.Run(); err != nil { + // not handling the error explicitly because usually we're going to see it in the output anyway + o.Logger.Error(err) + } + o.Logger.Info("now finish!") + + /* cmd.Stdout = ioutil.Discard + cmd.Stderr = ioutil.Discard + cmd.Stdin = nil + cmd = nil + + fmt.Fprintf(os.Stdout, "\n%s", utils.ColoredString("Press Enter", color.FgGreen)) + fmt.Scanln() // wait for enter press + */ + return nil +}