Source

xplor / xplor.go

Full commit
mpl 96a7857 
mpl 8d664fb 
mpl 35c329b 


mpl 8568a32 

mpl 0dcafdc 
mpl 8568a32 
mpl 35c329b 
mpl 8568a32 
mpl 22b5940 
mpl 35c329b 
Fazlul Shahriar 1489daf 
mpl 8568a32 

mpl 35c329b 

mpl 78a3406 
mpl 0dcafdc 


mpl a4368fc 
mpl 78a3406 
mpl 22b5940 
mpl 78a3406 
mpl a4368fc 



mpl 78a3406 
mpl 96a7857 
mpl 8568a32 
mpl 78a3406 
mpl 8568a32 
mpl 78a3406 
mpl 8568a32 
mpl 8d664fb 
mpl 96a7857 


mpl 8d664fb 
mpl 22b5940 
mpl 35c329b 
mpl 8d664fb 
mpl 96a7857 

mpl 8d664fb 
mpl 35c329b 
mpl 8d664fb 
mpl 96a7857 


mpl beadcf3 






mpl 96a7857 


mpl 22b5940 
mpl a38b0d7 
mpl 8d664fb 
mpl 8568a32 
Fazlul Shahriar 3116301 
mpl 8d664fb 
mpl 96a7857 
mpl 35c329b 
mpl a4368fc 
mpl a38b0d7 


mpl a4368fc 



mpl 08102ca 






mpl 8568a32 
mpl 08102ca 


mpl 8568a32 
mpl 08102ca 




mpl 79594d1 



mpl 96a7857 
mpl 35c329b 


mpl 8568a32 

mpl 35c329b 

mpl a38b0d7 
mpl 35c329b 

mpl 8d664fb 
mpl 35c329b 
mpl a4368fc 
mpl a38b0d7 
mpl a4368fc 
mpl 78a3406 
mpl 35c329b 

mpl a4368fc 
soul9 f05eb8b 
mpl a4368fc 
mpl 35c329b 
mpl a4368fc 
mpl 35c329b 


mpl a4368fc 
mpl 35c329b 
mpl 96a7857 

mpl 8568a32 
mpl 22b5940 
mpl 35c329b 
mpl 22b5940 
mpl 35c329b 
mpl d0f8700 
mpl 8568a32 
mpl 96a7857 
mpl a4368fc 
mpl 93e4ec3 
mpl a4368fc 
mpl d0f8700 

mpl 9d21cea 
mpl 3b522ce 









mpl 9d21cea 
mpl 8568a32 
mpl a4368fc 
mpl 9d21cea 
mpl a4368fc 
mpl 9d21cea 
mpl 35c329b 
mpl 96a7857 
mpl 78a3406 
mpl 8568a32 
mpl 8d664fb 

mpl 96a7857 
mpl 8568a32 
mpl 78a3406 
mpl a4368fc 
mpl 35c329b 

mpl 8568a32 
mpl 35c329b 
mpl 8568a32 
mpl 35c329b 




mpl 96a7857 
mpl 9d21cea 




mpl 35c329b 

mpl 22b5940 





mpl 8568a32 

mpl 8d664fb 










mpl 96a7857 
mpl 8d664fb 
mpl 96a7857 
mpl 8d664fb 

mpl 22b5940 







mpl 96a7857 
mpl 22b5940 
mpl 8d664fb 
mpl 22b5940 

mpl 8568a32 
mpl a38b0d7 
mpl 22b5940 


mpl a38b0d7 
mpl 22b5940 


mpl 96a7857 
mpl 22b5940 



mpl 0dcafdc 
mpl 8d664fb 


mpl 22b5940 
mpl 35c329b 
mpl 8568a32 
mpl 35c329b 

mpl 22b5940 
mpl a38b0d7 
mpl d0f8700 
mpl 35c329b 
mpl 8568a32 
mpl 35c329b 

mpl 96a7857 
mpl 8568a32 
mpl a38b0d7 
Fazlul Shahriar 1489daf 

mpl 8568a32 
mpl 8d664fb 

Fazlul Shahriar 1489daf 








mpl 35c329b 

mpl 96a7857 
mpl 8d664fb 
mpl 35c329b 
mpl 8568a32 
mpl 22b5940 
mpl 8d664fb 





mpl 8568a32 
mpl 8d664fb 
mpl 96a7857 
mpl 3b522ce 



mpl 8d664fb 
mpl 78a3406 
mpl 8d664fb 





mpl 8568a32 
mpl 8d664fb 



mpl 8568a32 
mpl 8d664fb 


mpl 96a7857 
mpl 8d664fb 




mpl 96a7857 
mpl 8568a32 
mpl 96a7857 

mpl 8d664fb 

mpl 35c329b 

mpl 8568a32 
mpl 78a3406 










mpl 79594d1 



mpl 8568a32 
mpl 79594d1 


Fazlul Shahriar 3116301 
mpl 79594d1 



mpl a4368fc 
mpl 79594d1 
mpl 8568a32 
mpl 79594d1 



mpl 08102ca 




mpl 8568a32 

mpl 08102ca 

mpl 8568a32 
mpl 08102ca 

mpl d0f8700 
mpl 08102ca 
mpl 8568a32 
mpl 08102ca 

mpl 8568a32 
mpl 08102ca 





soul9 f05eb8b 
mpl 08102ca 
mpl 8568a32 

mpl 08102ca 
mpl 78a3406 


mpl 79594d1 






mpl 78a3406 
mpl 79594d1 
mpl 8568a32 
mpl 79594d1 

mpl beadcf3 
mpl 79594d1 

mpl a4368fc 



mpl 35c329b 




mpl beadcf3 
mpl a38b0d7 
Fazlul Shahriar 3116301 
mpl 35c329b 
mpl a4368fc 

mpl a38b0d7 
Fazlul Shahriar 3116301 
mpl 78a3406 





mpl 08102ca 




mpl 8568a32 
mpl a38b0d7 

mpl 35c329b 
mpl beadcf3 
Fazlul Shahriar 3116301 


mpl 35c329b 
Fazlul Shahriar 3116301 
mpl 35c329b 
mpl 8d664fb 
mpl 35c329b 


Fazlul Shahriar 3116301 
mpl 35c329b 





  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// 2010 - Mathieu Lonjaret

package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"path"
	"sort"
	"strings"

	"bitbucket.org/fhs/goplumb/plumb"
	"code.google.com/p/goplan9/plan9"
	"code.google.com/p/goplan9/plan9/acme"
)

var (
	root       string
	w          *acme.Win
	PLAN9      = os.Getenv("PLAN9")
	showHidden bool
)

const (
	NBUF      = 512
	INDENT    = "	"
	dirflag   = "+ "
	nodirflag = "  "
)

type dir struct {
	charaddr string
	depth    int
}

func usage() {
	fmt.Fprintf(os.Stderr, "usage: xplor [path] \n")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()

	switch len(args) {
	case 0:
		root, _ = os.Getwd()
	case 1:
		temp := path.Clean(args[0])
		if temp[0] != '/' {
			cwd, _ := os.Getwd()
			root = path.Join(cwd, temp)
		} else {
			root = temp
		}
	default:
		usage()
	}

	err := initWindow()
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		return
	}

	for word := range events() {
		if len(word) >= 6 && word[0:6] == "DotDot" {
			doDotDot()
			continue
		}
		if len(word) >= 6 && word[0:6] == "Hidden" {
			toggleHidden()
			continue
		}
		if len(word) >= 3 && word[0:3] == "Win" {
			if PLAN9 != "" {
				cmd := path.Join(PLAN9, "bin/win")
				doExec(word[3:len(word)], cmd)
			}
			continue
		}
		// yes, this does not cover all possible cases. I'll do better if anyone needs it.
		if len(word) >= 5 && word[0:5] == "Xplor" {
			cmd, err := exec.LookPath("xplor")
			if err != nil {
				fmt.Fprintf(os.Stderr, err.Error())
				continue
			}
			doExec(word[5:len(word)], cmd)
			continue
		}
		if word[0] == 'X' {
			onExec(word[1:len(word)])
			continue
		}
		onLook(word)
	}
}

func initWindow() error {
	var err error = nil
	w, err = acme.New()
	if err != nil {
		return err
	}

	title := "xplor-" + root
	w.Name(title)
	tag := "DotDot Win Xplor Hidden"
	w.Write("tag", []byte(tag))
	err = printDirContents(root, 0)
	return err
}

func printDirContents(dirpath string, depth int) (err error) {
	currentDir, err := os.OpenFile(dirpath, os.O_RDONLY, 0644)
	line := ""
	if err != nil {
		return err
	}
	names, err := currentDir.Readdirnames(-1)
	if err != nil {
		return err
	}
	currentDir.Close()

	sort.Strings(names)
	indents := ""
	for i := 0; i < depth; i++ {
		indents = indents + INDENT
	}
	fullpath := ""
	var fi os.FileInfo
	for _, v := range names {
		line = nodirflag + indents + v + "\n"
		isNotHidden := !strings.HasPrefix(v, ".")
		if isNotHidden || showHidden {
			fullpath = path.Join(dirpath, v)
			fi, err = os.Stat(fullpath)
			if err != nil {
				_, ok := err.(*os.PathError)
				if !ok {
					panic("Not a *os.PathError")
				}
				if !os.IsNotExist(err) {
					return err
				}
				// Skip (most likely) broken symlinks
				fmt.Fprintf(os.Stderr, "%v\n", err.Error())
				continue
			}
			if fi.IsDir() {
				line = dirflag + indents + v + "\n"
			}
			w.Write("data", []byte(line))
		}
	}

	if depth == 0 {
		//lame trick for now to dodge the out of range issue, until my address-foo gets better
		w.Write("body", []byte("\n"))
		w.Write("body", []byte("\n"))
		w.Write("body", []byte("\n"))
	}

	return err
}

func readLine(addr string) ([]byte, error) {
	var b []byte = make([]byte, NBUF)
	var err error = nil
	err = w.Addr("%s", addr)
	if err != nil {
		return b, err
	}
	n, err := w.Read("xdata", b)

	// remove dirflag, if any
	if n < 2 {
		return b[0 : n-1], err
	}
	return b[2 : 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 isFolded(charaddr string) (bool, error) {
	var err error = nil
	var b []byte
	addr := "#" + charaddr + "+1-"
	b, err = readLine(addr)
	if err != nil {
		return true, err
	}
	depth, _ := getDepth(b)
	addr = "#" + charaddr + "+-"
	b, err = readLine(addr)
	if err != nil {
		return true, err
	}
	nextdepth, _ := getDepth(b)
	return (nextdepth <= depth), err
}

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 {
			fmt.Fprintf(os.Stderr, err.Error())
			os.Exit(1)
		}
		newdepth, line := getDepth(b)
		if newdepth < depth {
			fullpath := path.Join(getParents(charaddr, newdepth, prevline), line)
			return fullpath
		}
		prevline++
		addr = "#" + charaddr + "-" + fmt.Sprint(prevline-1)
	}
	return ""
}

// 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()
		port.Send(&plumb.Msg{
			Src:  "xplor",
			Dst:  "",
			WDir: "/",
			Kind: "text",
			Attr: map[string]string{},
			Data: []byte(fullpath),
		})
		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(""))
	}
}

func getFullPath(charaddr string) (fullpath string, err error) {
	// reconstruct full path and print it to Stdout
	addr := "#" + charaddr + "+1-"
	b, err := readLine(addr)
	if err != nil {
		return fullpath, err
	}
	depth, line := getDepth(b)
	fullpath = path.Join(root, getParents(charaddr, depth, 1), line)
	return fullpath, err
}

func doDotDot() {
	// blank the window
	err := w.Addr("0,$")
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}
	w.Write("data", []byte(""))

	// restart from ..
	root = path.Clean(root + "/../")
	title := "xplor-" + root
	w.Name(title)
	err = printDirContents(root, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}
}

func doExec(loc string, cmd string) {
	var fullpath string
	if loc == "" {
		fullpath = root
	} else {
		var err error
		charaddr := strings.SplitAfterN(loc, ",#", 2)
		fullpath, err = getFullPath(charaddr[1])
		if err != nil {
			fmt.Fprintf(os.Stderr, err.Error())
			return
		}
		fi, err := os.Stat(fullpath)
		if err != nil {
			fmt.Fprintf(os.Stderr, err.Error())
			return
		}
		if !fi.IsDir() {
			fullpath, _ = path.Split(fullpath)
		}
	}
	var args []string = make([]string, 1)
	args[0] = cmd
	fds := []*os.File{os.Stdin, os.Stdout, os.Stderr}
	_, err := os.StartProcess(args[0], args, &os.ProcAttr{Env: os.Environ(), Dir: fullpath, Files: fds})
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		return
	}
	return
}

// on a B2 click event we print the fullpath of the file to Stdout.
// This can come in handy for paths with spaces in it, because the
// plumber will fail to open them.  Printing it to Stdout allows us to do
// whatever we want with it when that happens.
// Also usefull with a dir path: once printed to stdout, a B3 click on
// the path to open it the "classic" acme way.
func onExec(charaddr string) {
	fullpath, err := getFullPath(charaddr)
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		return
	}
	fmt.Fprintf(os.Stderr, fullpath+"\n")
}

func toggleHidden() {
	showHidden = !showHidden
}

func events() <-chan string {
	c := make(chan string, 10)
	go func() {
		for e := range w.EventChan() {
			switch e.C2 {
			case 'x': // execute in tag
				switch string(e.Text) {
				case "Del":
					w.Ctl("delete")
				case "Hidden":
					c <- "Hidden"
				case "DotDot":
					c <- "DotDot"
				case "Win":
					tmp := ""
					if e.Flag != 0 {
						tmp = string(e.Loc)
					}
					c <- ("Win" + tmp)
				case "Xplor":
					tmp := ""
					if e.Flag != 0 {
						tmp = string(e.Loc)
					}
					c <- ("Xplor" + tmp)
				default:
					w.WriteEvent(e)
				}
			case 'X': // execute in body
				c <- ("X" + fmt.Sprint(e.OrigQ0))
			case 'l': // button 3 in tag
				// let the plumber deal with it
				w.WriteEvent(e)
			case 'L': // button 3 in body
				w.Ctl("clean")
				//ignore expansions
				if e.OrigQ0 != e.OrigQ1 {
					continue
				}
				c <- fmt.Sprint(e.OrigQ0)
			}
		}
		w.CloseFiles()
		close(c)
	}()
	return c
}