Skip to content

Commit

Permalink
Make it open file as sub process
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed May 3, 2020
1 parent 2a70a3f commit 5ad7394
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions pkg/oscommand/oscommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os/exec"
"strings"

"github.com/k0kubun/pp"

"github.com/sirupsen/logrus"
)

Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit 5ad7394

Please sign in to comment.