9 "github.com/gdamore/tcell"
12 func HandleShellCommand(input string, view *View) {
13 inputCmd := strings.Split(input, " ")[0]
14 args := strings.Split(input, " ")[1:]
17 cmdout := exec.Command(inputCmd, args...)
18 output, _ := cmdout.CombinedOutput()
19 outstring := string(output)
20 totalLines := strings.Split(outstring, "\n")
21 if len(totalLines) == 2 {
22 messenger.Message(outstring)
26 // Display nonblank output
27 DisplayBlock(outstring)
31 // DisplayBlock displays txt
32 // It blocks the main loop
33 func DisplayBlock(text string) {
35 _, height := screen.Size()
37 totalLines := strings.Split(text, "\n")
41 lineEnd := topline + height
42 if lineEnd > len(totalLines) {
43 lineEnd = len(totalLines)
45 lines := totalLines[topline:lineEnd]
46 for y, line := range lines {
47 for x, ch := range line {
49 screen.SetContent(x, y, ch, nil, st)
55 event := screen.PollEvent()
56 switch e := event.(type) {
57 case *tcell.EventResize:
66 if topline < len(totalLines)-height {
69 case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC:
76 // HandleCommand handles input from the user
77 func HandleCommand(input string, view *View) {
78 inputCmd := strings.Split(input, " ")[0]
79 args := strings.Split(input, " ")[1:]
81 commands := []string{"set", "quit", "save", "replace"}
86 for _, c := range commands {
87 if strings.HasPrefix(c, inputCmd) {
100 if view.CanClose("Quit anyway? ") {
107 r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`)
108 replaceCmd := r.FindAllString(strings.Join(args, " "), -1)
109 if len(replaceCmd) < 2 {
110 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
115 if len(replaceCmd) == 3 {
116 // The user included some flags
117 flags = replaceCmd[2]
120 search := string(replaceCmd[0])
121 replace := string(replaceCmd[1])
123 if strings.HasPrefix(search, `"`) && strings.HasSuffix(search, `"`) {
124 search = search[1 : len(search)-1]
126 if strings.HasPrefix(replace, `"`) && strings.HasSuffix(replace, `"`) {
127 replace = replace[1 : len(replace)-1]
130 search = strings.Replace(search, `\"`, `"`, -1)
131 replace = strings.Replace(replace, `\"`, `"`, -1)
133 // messenger.Error(search + " -> " + replace)
135 regex, err := regexp.Compile(search)
137 messenger.Error(err.Error())
143 match := regex.FindStringIndex(view.buf.text)
148 if strings.Contains(flags, "c") {
149 // // The 'check' flag was used
150 // if messenger.YesNoPrompt("Perform replacement?") {
151 // view.eh.Replace(match[0], match[1], replace)
156 view.eh.Replace(match[0], match[1], replace)
159 messenger.Message("Nothing matched " + search)
162 messenger.Error("Unknown command: " + inputCmd)