PageRenderTime 170ms CodeModel.GetById 121ms app.highlight 8ms RepoModel.GetById 39ms app.codeStats 0ms

/plan9/acme/Dict/Dict.go

https://code.google.com/p/goplan9/
Go | 87 lines | 80 code | 7 blank | 0 comment | 18 complexity | f69387069a10c68b9a48ec460dbe32ae MD5 | raw file
 1package main
 2
 3import (
 4	"code.google.com/p/go.net/dict"
 5	"flag"
 6	"log"
 7
 8	"code.google.com/p/goplan9/plan9/acme"
 9)
10
11var dictx = flag.String("d", "", "dictionary")
12var server = flag.String("s", "dict.org:dict", "server")
13var lookc = make(chan string)
14var d *dict.Client
15var dicts []dict.Dict
16
17func main() {
18	w, err := acme.New()
19	if err != nil {
20		log.Fatal(err)
21	}
22	w.Name("/dict/")
23	d, err = dict.Dial("tcp", "216.93.242.2:dict")
24	if err != nil {
25		w.Write("body", []byte(err.Error()))
26		return
27	}
28	w.Ctl("clean")
29	go func() {
30		dicts, err = d.Dicts()
31		if err != nil {
32			w.Write("body", []byte(err.Error()))
33			return
34		}
35		for _, dict := range dicts {
36			w.Fprintf("body", "%s\t%s\n", dict.Name, dict.Desc)
37		}
38	}()
39	for word := range events(w) {
40		go lookup(word)
41	}
42}
43
44func lookup(word string) {
45	defs, err := d.Define("!", word)
46	if err != nil {
47		log.Print(err)
48		return
49	}
50	for _, def := range defs {
51		go wordwin(def)
52	}
53}
54
55func wordwin(def *dict.Defn) {
56	w, err := acme.New()
57	if err != nil {
58		log.Fatal(err)
59	}
60	w.Name("/dict/%s/%s", def.Dict.Name, def.Word)
61	w.Write("body", def.Text)
62	w.Ctl("clean")
63	for word := range events(w) {
64		go lookup(word)
65	}
66}
67
68func events(w *acme.Win) <-chan string {
69	c := make(chan string, 10)
70	go func() {
71		for e := range w.EventChan() {
72			switch e.C2 {
73			case 'x', 'X': // execute
74				if string(e.Text) == "Del" {
75					w.Ctl("delete")
76				}
77				w.WriteEvent(e)
78			case 'l', 'L': // look
79				w.Ctl("clean")
80				c <- string(e.Text)
81			}
82		}
83		w.CloseFiles()
84		close(c)
85	}()
86	return c
87}