12 "github.com/zyedidia/micro/cmd/micro/screen"
13 "github.com/zyedidia/micro/cmd/micro/shellwords"
16 // ExecCommand executes a command using exec
17 // It returns any output/errors
18 func ExecCommand(name string, arg ...string) (string, error) {
20 cmd := exec.Command(name, arg...)
21 outputBytes := &bytes.Buffer{}
22 cmd.Stdout = outputBytes
23 cmd.Stderr = outputBytes
28 err = cmd.Wait() // wait for command to finish
29 outstring := outputBytes.String()
33 // RunCommand executes a shell command and returns the output/error
34 func RunCommand(input string) (string, error) {
35 args, err := shellwords.Split(input)
41 return ExecCommand(inputCmd, args[1:]...)
44 // RunBackgroundShell runs a shell command in the background
45 // It returns a function which will run the command and returns a string
47 func RunBackgroundShell(input string) (func() string, error) {
48 args, err := shellwords.Split(input)
53 return func() string {
54 output, err := RunCommand(input)
55 totalLines := strings.Split(output, "\n")
58 if len(totalLines) < 3 {
60 str = fmt.Sprint(inputCmd, " exited without error")
62 str = fmt.Sprint(inputCmd, " exited with error: ", err, ": ", output)
69 // RunInteractiveShell runs a shellcommand interactively
70 func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error) {
71 args, err := shellwords.Split(input)
77 // Shut down the screen because we're going to interact directly with the shell
78 screenb := screen.TempFini()
82 // Set up everything for the command
83 outputBytes := &bytes.Buffer{}
84 cmd := exec.Command(inputCmd, args...)
87 cmd.Stdout = io.MultiWriter(os.Stdout, outputBytes)
89 cmd.Stdout = os.Stdout
91 cmd.Stderr = os.Stderr
93 // This is a trap for Ctrl-C so that it doesn't kill micro
94 // Instead we trap Ctrl-C to kill the program we're running
95 c := make(chan os.Signal, 1)
96 signal.Notify(c, os.Interrupt)
106 output := outputBytes.String()
109 // This is just so we don't return right away and let the user press enter to return
110 screen.TermMessage("")
113 // Start the screen back up
114 screen.TempStart(screenb)
119 // UserCommand runs the shell command
120 // The openTerm argument specifies whether a terminal should be opened (for viewing output
121 // or interacting with stdin)
122 // func UserCommand(input string, openTerm bool, waitToFinish bool) string {
124 // RunBackgroundShell(input)
127 // output, _ := RunInteractiveShell(input, waitToFinish, false)