func serve(idx *index.Index) error { fid, err := plumb.Open("editclass", plan9.OREAD) if err != nil { return err } defer fid.Close() r := bufio.NewReader(fid) w, err := plumb.Open("send", plan9.OWRITE) if err != nil { return err } defer w.Close() for { m := plumb.Message{} err := m.Recv(r) if err != nil { return err } log.Printf("Received from plumber: %s\n", m) name := string(m.Data) var get *index.GetResult for _, c := range candidatesOf(name) { if get = idx.Get(c); get != nil { break } } if get == nil { log.Printf("Found no results for: %s\n", name) continue } if get.Path != "" { if err := plumbFile(&m, w, name, get.Path); err != nil { log.Printf("%s: %s\n", get.Path, err) } } else if get.Children != nil { if err := plumbDir(idx, get.Children, w); err != nil { log.Printf("error opening dir: %s\n", err) } } else { log.Printf("Result was empty: %s\n", name) } } return nil }
// TODO(mpl): maybe break this one in a fold and unfold functions func onLook(charaddr string) { // reconstruct full path and check if file or dir addr := "#" + charaddr + "+1-" b, err := readLine(addr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) return } depth, line := getDepth(b) fullpath := path.Join(root, getParents(charaddr, depth, 1), line) fi, err := os.Stat(fullpath) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) return } if !fi.IsDir() { // not a dir -> send that file to the plumber port, err := plumb.Open("send", plan9.OWRITE) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) return } defer port.Close() msg := &plumb.Message{ Src: "xplor", Dst: "", Dir: "/", Type: "text", Data: []byte(fullpath), } if err := msg.Send(port); err != nil { fmt.Fprintf(os.Stderr, err.Error()) } return } folded, err := isFolded(charaddr) if err != nil { fmt.Fprint(os.Stderr, err.Error()) return } if folded { // print dir contents addr = "#" + charaddr + "+2-1-#0" err = w.Addr("%s", addr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()+addr) return } err = printDirContents(fullpath, depth+1) if err != nil { fmt.Fprint(os.Stderr, err.Error()) } } else { // fold, ie delete lines below dir until we hit a dir of the same depth addr = "#" + charaddr + "+-" nextdepth := depth + 1 nextline := 1 for nextdepth > depth { err = w.Addr("%s", addr) if err != nil { fmt.Fprint(os.Stderr, err.Error()) return } b, err = readLine(addr) if err != nil { fmt.Fprint(os.Stderr, err.Error()) return } nextdepth, _ = getDepth(b) nextline++ addr = "#" + charaddr + "+" + fmt.Sprint(nextline-1) } nextline-- addr = "#" + charaddr + "+-#0,#" + charaddr + "+" + fmt.Sprint(nextline-2) err = w.Addr("%s", addr) if err != nil { fmt.Fprint(os.Stderr, err.Error()) return } w.Write("data", []byte("")) } }