Anonymous avatar Anonymous committed 22b5940

only unfold missing

Comments (0)

Files changed (1)

 	"os"
 	"path"
 	"fmt"
-//	"strings"
+	"strings"
 	"sort"
 
 	"goplan9.googlecode.com/hg/plan9/acme"
 
 	var root string
 	var w *acme.Win
-	 
+	var INDENT string = "	"
+
+//TODO: send error messages to error win, not to xplor win.	 
 func main() {
 
 	initWindow()
 	
 	sort.SortStrings(names)
 
-	tabs := ""
+	indents := ""
 	for i := 0; i < depth; i++ {
-		tabs = tabs + "	"
+		indents = indents + INDENT
 	}
 	for _, v := range names {	
-		w.Write("data", []byte(tabs + v + "\n"))
+		w.Write("data", []byte(indents + v + "\n"))
 	}
 }
 
-//TODO: func getDepth
-//TODO: func isExpanded
+//TODO: func isUnfolded
 
-func readLine(charaddr string) ([]byte, os.Error) {
+func readLine(addr string) ([]byte, os.Error) {
 //TODO: do better about the buf of 512?
 	const NBUF = 512	
 	var b []byte = make([]byte, NBUF)
 	var err os.Error = nil
-	addr := "#" + charaddr + "+-"
+	fmt.Fprint(os.Stdout, "readLine addr: " + addr + "\n")
+//	w.Write("body", []byte("readLine addr: " + addr + "\n"))
 	err = w.Addr("%s", addr)
 	if err != nil {
-		w.Write("body", []byte(err.String() + ": " + addr))
 		return b, err
 	}
 	n, err := w.Read("xdata", b)
-	if err != nil {
-		w.Write("body", []byte(err.String()))
-	}	
 	
 	return b[0:n-1], err
 }
 
+func getDepth(line []byte) (depth int, trimedline string) {
+	trimedline = strings.TrimLeft(string(line), INDENT)
+	depth = (len(line) - len(trimedline)) / len(INDENT)
+	return depth, trimedline
+}
+
+func getParents(charaddr string, depth int, prevline int) string {
+	var addr string
+	if depth == 0 {
+		return ""
+	}
+	if prevline == 1 {
+		addr = "#" + charaddr + "-+"
+	} else {
+		addr = "#" + charaddr + "-" + fmt.Sprint(prevline - 1)
+	}
+	for ;; {
+		b, err := readLine(addr)
+		if err != nil {
+			w.Write("body", []byte(err.String()))
+			return ""
+		}
+		newdepth, line := getDepth(b)
+		fmt.Fprint(os.Stdout, fmt.Sprint(newdepth) + ", " + line + "\n")
+		if newdepth < depth {
+			fullpath := path.Join(getParents(charaddr, newdepth, prevline), "/", line)
+			fmt.Fprint(os.Stdout, fullpath + "\n")
+			return fullpath
+		}
+		prevline++
+		addr = "#" + charaddr + "-" + fmt.Sprint(prevline - 1)
+	}
+	return ""
+}
+
 func onLook(word string) {
-	b, err := readLine(word)
+	addr := "#" + word + "+1-"
+	b, err := readLine(addr)
 	if err != nil {
 		w.Write("body", []byte(err.String()))
 		return
 	}
-	fpath := path.Join(root, string(b))
-	fi, err := os.Lstat(fpath)
+	depth, line := getDepth(b)
+	fmt.Fprint(os.Stdout, fmt.Sprint(depth) + ", " + line + "\n")
+	fullpath := path.Join(root, "/", getParents(word, depth, 1), "/", line)
+	fi, err := os.Lstat(fullpath)
 	if err != nil {
 		w.Write("body", []byte(err.String()))
 		return
 	}
 	if !fi.IsDirectory() {
 //TODO: send back a Look event?
+		fmt.Fprint(os.Stdout, "Not a dir \n")
 		return
 	}
-	addr := "#" + word + "+-#0"
+	addr = "#" + word + "+2-1-#0"
+	fmt.Fprint(os.Stdout, "onLook addr: " + addr + "\n")
 	err = w.Addr("%s", addr)
 	if err != nil {
 		w.Write("body", []byte(err.String() + addr))
 		return
 	}	
-	printDirContents(fpath, 1)
+	printDirContents(fullpath, depth + 1)
 }
 
 func events() <-chan string {
 				w.WriteEvent(e)
 			case 'L':	// look
 				w.Ctl("clean")
+			/*
+				msg := fmt.Sprint(e.Q0) + "," + fmt.Sprint(e.Q1) + "," + fmt.Sprint(e.OrigQ0) + "," + fmt.Sprint(e.OrigQ1)
+				w.Write("body", []byte(msg))
+				continue
+			*/
 				//disallow expansions to avoid weird addresses for now
 				if e.OrigQ0 != e.OrigQ1 {
 					continue
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.