func handleWinEvents(win *acme.Win) { for ev := range win.EventChan() { if ev.C2 == 'x' || ev.C2 == 'X' { if ev.Flag&2 != 0 { ev.Q0 = ev.OrigQ0 ev.Q1 = ev.OrigQ1 } win.WriteEvent(ev) if fs := strings.Fields(string(ev.Text)); len(fs) > 0 && fs[0] == "Del" { os.Exit(0) } } } }
func events(w *acme.Win) <-chan string { c := make(chan string, 10) go func() { for e := range w.EventChan() { switch e.C2 { case 'x', 'X': // execute if string(e.Text) == "Del" { w.Ctl("delete") } w.WriteEvent(e) case 'l', 'L': // look w.Ctl("clean") c <- string(e.Text) } } w.CloseFiles() close(c) }() return c }
func main() { log.SetFlags(0) log.SetPrefix("Run: ") file := os.Getenv("samfile") if file == "" { log.Fatal("not running in acme") } id, _ := strconv.Atoi(os.Getenv("winid")) wfile, err := acme.Open(id, nil) if err != nil { log.Fatal(err) } wfile.Ctl("put") wfile.CloseFiles() wname := "/go/run/" + strings.TrimSuffix(path.Base(file), ".go") windows, _ := acme.Windows() var w *acme.Win for _, info := range windows { if info.Name == wname { ww, err := acme.Open(info.ID, nil) if err != nil { log.Fatal(err) } ww.Addr(",") ww.Write("data", nil) w = ww break } } if w == nil { ww, err := acme.New() if err != nil { log.Fatal(err) } ww.Name(wname) w = ww } w.Ctl("clean") defer w.Ctl("clean") cmd := exec.Command("go", append([]string{"run", os.Getenv("samfile")}, os.Args[1:]...)...) cmd.Stdout = bodyWriter{w} cmd.Stderr = cmd.Stdout cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} err = cmd.Start() if err != nil { w.Fprintf("body", "error starting command: %v\n", err) return } //stop := blinker(w) w.Ctl("cleartag") w.Fprintf("tag", " Kill Stack") done := make(chan bool) go func() { err := cmd.Wait() if err != nil { w.Fprintf("body", "\nerror running command: %v\n", err) } //stop <- true done <- true }() deleted := make(chan bool, 1) go func() { for e := range w.EventChan() { if e.C2 == 'x' || e.C2 == 'X' { switch string(e.Text) { case "Del": select { case deleted <- true: default: } syscall.Kill(-cmd.Process.Pid, 2) continue case "Kill": syscall.Kill(-cmd.Process.Pid, 2) continue case "Stack": syscall.Kill(-cmd.Process.Pid, 3) continue } w.WriteEvent(e) } } }() <-done w.Ctl("cleartag") select { case <-deleted: w.Ctl("delete") default: } }