12 // RunShellCommand executes a shell command and returns the output/error
13 func RunShellCommand(input string) (string, error) {
14 inputCmd := strings.Split(input, " ")[0]
15 args := strings.Split(input, " ")[1:]
17 cmd := exec.Command(inputCmd, args...)
18 outputBytes := &bytes.Buffer{}
19 cmd.Stdout = outputBytes
20 cmd.Stderr = outputBytes
22 err := cmd.Wait() // wait for command to finish
23 outstring := outputBytes.String()
27 // HandleShellCommand runs the shell command and outputs to DisplayBlock
28 func HandleShellCommand(input string, view *View, openTerm bool) {
29 inputCmd := strings.Split(input, " ")[0]
31 messenger.Message("Running...")
33 output, err := RunShellCommand(input)
34 totalLines := strings.Split(output, "\n")
36 if len(totalLines) < 3 {
38 messenger.Message(inputCmd, " exited without error")
40 messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
43 messenger.Message(output)
51 args := strings.Split(input, " ")[1:]
53 cmd := exec.Command(inputCmd, args...)
55 cmd.Stdout = os.Stdout
56 cmd.Stderr = os.Stderr
58 c := make(chan os.Signal, 1)
59 signal.Notify(c, os.Interrupt)
75 // HandleCommand handles input from the user
76 func HandleCommand(input string, view *View) {
77 inputCmd := strings.Split(input, " ")[0]
78 args := strings.Split(input, " ")[1:]
80 commands := []string{"set", "quit", "save", "replace", "run"}
85 for _, c := range commands {
86 if strings.HasPrefix(c, inputCmd) {
99 HandleShellCommand(strings.Join(args, " "), view, false)
101 if view.CanClose("Quit anyway? (yes, no, save) ") {
108 r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`)
109 replaceCmd := r.FindAllString(strings.Join(args, " "), -1)
110 if len(replaceCmd) < 2 {
111 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
116 if len(replaceCmd) == 3 {
117 // The user included some flags
118 flags = replaceCmd[2]
121 search := string(replaceCmd[0])
122 replace := string(replaceCmd[1])
124 if strings.HasPrefix(search, `"`) && strings.HasSuffix(search, `"`) {
125 search = search[1 : len(search)-1]
127 if strings.HasPrefix(replace, `"`) && strings.HasSuffix(replace, `"`) {
128 replace = replace[1 : len(replace)-1]
131 search = strings.Replace(search, `\"`, `"`, -1)
132 replace = strings.Replace(replace, `\"`, `"`, -1)
134 // messenger.Error(search + " -> " + replace)
136 regex, err := regexp.Compile(search)
138 messenger.Error(err.Error())
144 match := regex.FindStringIndex(view.Buf.String())
149 if strings.Contains(flags, "c") {
150 // The 'check' flag was used
151 Search(search, view, true)
154 choice, canceled := messenger.YesNoPrompt("Perform replacement? (y,n)")
156 if view.Cursor.HasSelection() {
157 view.Cursor.SetLoc(view.Cursor.curSelection[0])
158 view.Cursor.ResetSelection()
164 view.Cursor.DeleteSelection()
165 view.eh.Insert(match[0], replace)
166 view.Cursor.ResetSelection()
169 if view.Cursor.HasSelection() {
170 searchStart = view.Cursor.curSelection[1]
172 searchStart = ToCharPos(view.Cursor.x, view.Cursor.y, view.Buf)
177 view.eh.Replace(match[0], match[1], replace)
181 messenger.Message("Nothing matched " + search)
184 messenger.Error("Unknown command: " + inputCmd)