diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index 00fac86a..24920313 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -99,6 +99,7 @@ func (cli *CLI) parse() bool { // {{{ ls := strings.Split(line, " ") msg := &ctx.Message{Wait: make(chan bool)} + msg.Message = cli.Resource[0] msg.Context = cli.Context msg.Target = cli.target @@ -149,6 +150,10 @@ func (cli *CLI) deal(msg *ctx.Message) bool { // {{{ }() detail := msg.Meta["detail"] + if len(detail) == 0 { + msg.End(true) + return true + } if a, ok := cli.alias[detail[0]]; ok { detail[0] = a } @@ -352,6 +357,23 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", // }}} }}, "message": &ctx.Command{"message detail...", "查看上下文", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { + m := ctx.Pulse + switch len(arg) { + case 1: + fmt.Println(msg.Code, msg.Time.Format("2006/01/02 15:04:05")) + case 2: + switch arg[1] { + case "home": + m = msg.Message + case "root": + } + fmt.Println(m.Code, m.Time.Format("2006/01/02 15:04:05")) + for i, v := range m.Messages { + fmt.Println(i, v.Code, v.Time.Format("2006/01/02 15:04:05")) + } + } + + return "" msg.Meta["detail"] = arg[1:] // {{{ if c == msg.Target { go msg.Target.Post(msg) diff --git a/src/context/ctx.go b/src/context/ctx.go index 81b4ffe2..3c8f877d 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -40,6 +40,7 @@ type Command struct { // {{{ type Message struct { // {{{ Time time.Time Code int + User string Meta map[string][]string Data map[string]interface{} @@ -50,8 +51,29 @@ type Message struct { // {{{ Name string *Context + + Messages []*Message + Message *Message + Root *Message } +func (m *Message) Spawn(c *Context, key string) *Message { // {{{ + msg := &Message{Time: time.Now(), Code: m.Capi("nmessage", 1)} + + msg.Context = m.Target + msg.Target = c + + if m.Messages == nil { + m.Messages = make([]*Message, 0, 10) + } + m.Messages = append(m.Messages, msg) + msg.Message = m + msg.Root = m.Root + return m + +} + +// }}} func (m *Message) Add(key string, value ...string) string { // {{{ if m.Meta == nil { m.Meta = make(map[string][]string) @@ -211,6 +233,9 @@ func (c *Context) Init(arg ...string) { // {{{ } } + Pulse.Context = Index + Pulse.Root = Pulse + if len(arg) > 2 { for _, v := range arg[2:] { cs = root.Search(v) @@ -219,7 +244,10 @@ func (c *Context) Init(arg ...string) { // {{{ } } } else { - go root.Find(strings.Split(root.Conf("default"), ".")).Start() + s := root.Find(strings.Split(root.Conf("default"), ".")) + go s.Start() + Pulse.Target = s + s.Post(Pulse) } <-make(chan bool) @@ -426,24 +454,6 @@ func (c *Context) Del(arg ...string) { // {{{ // }}} -func (c *Context) Request(s *Context, key string) *Message { // {{{ - if c.Session == nil { - c.Session = make(map[string]*Message) - } - if s.Resource == nil { - s.Resource = make([]*Message, 0, c.Confi("MessageListSize")) - } - - m := &Message{Index: len(s.Resource), Name: key} - - m.Target = s - m.Context = c - - return m -} - -// }}} - func (c *Context) Begin() bool { // {{{ c.Root.Capi("ncontext", 1) for k, v := range c.Configs { @@ -491,9 +501,10 @@ func (c *Context) Post(m *Message) bool { // {{{ if c.Messages == nil { c.Messages = make(chan *Message, c.Confi("MessageQueueSize")) } - - m.Time = time.Now() - m.Code = c.Root.Capi("nmessage", 1) + if c.Resource == nil { + c.Resource = make([]*Message, 0, c.Confi("MessageQueueSize")) + } + c.Resource = append(c.Resource, m) c.Messages <- m if m.Wait != nil { @@ -630,8 +641,7 @@ func (c *Context) Capi(key string, value int) int { // {{{ // }}} // }}} -type CTX struct { -} +type CTX struct{} var Ctx = &CTX{} @@ -664,8 +674,11 @@ var Index = &Context{Name: "ctx", Help: "根上下文", }}, }, Commands: map[string]*Command{}, + Session: map[string]*Message{"root": Pulse}, } +var Pulse = &Message{Time: time.Now(), Code: 0} + func Start() { Index.Init(os.Args[1:]...) } diff --git a/src/toolkit/bench.go b/src/example/bench.go similarity index 100% rename from src/toolkit/bench.go rename to src/example/bench.go diff --git a/src/example/findgrep.go b/src/example/findgrep.go new file mode 100644 index 00000000..286f9f20 --- /dev/null +++ b/src/example/findgrep.go @@ -0,0 +1,83 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "regexp" + "toolkit" +) + +var files = ".*\\.(xml|html|css|js)$" +var words = "[[:^ascii:]]+" + +func main() { + if len(os.Args) == 1 { + fmt.Println("usage", os.Args[0], "dirs [files [words]]") + fmt.Println("在目录dirs中,查找匹配files的文件,并查找匹配words的单词") + os.Exit(1) + } + + if len(os.Args) > 2 { + files = os.Args[2] + } + if len(os.Args) > 3 { + words = os.Args[3] + } + + word, e := regexp.Compile(words) + kit.Check(e) + // out, e := os.Create(os.Args[2]) + // kit.Check(e) + out := os.Stdout + + total := 0 + count := 0 + chars := 0 + kit.DirWalk(os.Args[1], func(file string) { + s, _ := os.Stat(file) + if s.IsDir() { + return + } + if m, e := regexp.MatchString(files, file); !kit.Check(e) || !m { + return + } + + f, e := os.Open(file) + kit.Check(e) + bio := bufio.NewReader(f) + + fmt.Fprintln(out, kit.FmtSize(s.Size()), file) + line := 0 + + cs := 0 + for i := 1; true; i++ { + l, e := bio.ReadString('\n') + if e == io.EOF { + break + } + kit.Check(e) + if i == 1 { + continue + } + + a := word.FindAllString(l, 20) + for _, v := range a { + n := len([]rune(v)) + fmt.Fprintf(out, "l:%d c:%d %s\n", i, n, v) + total++ + line++ + chars += n + cs += n + } + } + fmt.Fprintln(out, "lines:", line, "chars:", cs, file) + fmt.Fprintln(out) + if line > 0 { + count++ + } + }) + fmt.Fprintln(out, "files:", count, "lines:", total, "chars:", chars, os.Args[1]) + return +} diff --git a/src/toolkit/kit.go b/src/toolkit/kit.go new file mode 100644 index 00000000..6d38b17e --- /dev/null +++ b/src/toolkit/kit.go @@ -0,0 +1,47 @@ +package kit + +import ( + "fmt" + "io/ioutil" + // "log" + "os" + "path" +) + +func FmtSize(size int64) string { + if size > 1000000000 { + return fmt.Sprintf("%d.%dG", size/1000000000, size/100000000%100) + } + + if size > 1000000 { + return fmt.Sprintf("%d.%dM", size/100000, size/100000%100) + } + + if size > 1000 { + return fmt.Sprintf("%d.%dK", size/1000, size/100%100) + } + + return fmt.Sprintf("%dB", size) +} + +func Check(e error) bool { + if e != nil { + panic(e) + } + return true +} + +func DirWalk(file string, hand func(file string)) { + s, e := os.Stat(file) + Check(e) + hand(file) + + if s.IsDir() { + fs, e := ioutil.ReadDir(file) + Check(e) + + for _, v := range fs { + DirWalk(path.Join(file, v.Name()), hand) + } + } +}