diff --git a/etc/init.shy b/etc/init.shy index 1a3900b3..3c86f12f 100644 --- a/etc/init.shy +++ b/etc/init.shy @@ -1,24 +1,10 @@ +~file1 + history load etc/history.txt ~shell1 alias import nfs + source etc/local.shy -return - -var msg <- $(demo) -for index $msg append temp - for index $msg $temp h - echo $temp $h - end -end - -for index $msg hi temp - echo $temp -end - -var b = 1 -for b += 1; $b < 3 - echo $msg -end - +config debug on return return "hello" "world" diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index a42e6862..c8c6bb40 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -313,6 +313,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", if i, e := strconv.Atoi(arg[0]); e == nil { t = time.Unix(int64(i/m.Confi("time_unit")), 0) arg = arg[1:] + m.Option("time_format", m.Conf("time_format")) } } @@ -664,11 +665,17 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", detail = append(detail, arg...) } + if detail[0] != "context" { + target := cli.target + defer func() { + cli.target = target + m.Cap("ps_target", cli.target.Name) + }() + } + routes := strings.Split(detail[0], ".") msg := m if len(routes) > 1 { - target := cli.target - defer func() { cli.target = target }() route := strings.Join(routes[:len(routes)-1], ".") if msg = m.Find(route, false); msg == nil { diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 63cbc80d..e49c0989 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -1943,8 +1943,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心", "nmessage": &Cache{Name: "消息数量", Value: "0", Help: "显示模块启动时所创建消息的数量"}, }, Configs: map[string]*Config{ - "scan_path": &Config{Name: "调试模式(true/false)", Value: "", Help: "是否打印错误信息,off:不打印,on:打印)"}, - "debug": &Config{Name: "调试模式(true/false)", Value: "true", Help: "是否打印错误信息,off:不打印,on:打印)"}, "default": &Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点,root:从根模块,back:从父模块,home:从当前模块"}, "start": &Config{Name: "启动模块", Value: "cli", Help: "启动时自动运行的模块"}, @@ -3006,10 +3004,6 @@ func init() { } func Start(args ...string) { - if len(args) > 0 { - Pulse.Conf("scan_path", args[0]) - args = args[1:] - } if len(args) > 0 { Pulse.Conf("init.shy", args[0]) args = args[1:] diff --git a/src/contexts/mdb/mdb.go b/src/contexts/mdb/mdb.go index a1a99fb2..da045934 100644 --- a/src/contexts/mdb/mdb.go +++ b/src/contexts/mdb/mdb.go @@ -1,6 +1,7 @@ package mdb // {{{ // }}} import ( // {{{ + "bufio" "contexts" "database/sql" "encoding/json" @@ -395,10 +396,32 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", } // }}} }}, - "demo": &ctx.Command{Name: "get field offset table where [parse func field]", Help: "执行查询语句", - Form: map[string]int{"parse": 1}, + "csv": &ctx.Command{Name: "csv scv_file sql_file sql", Help: "执行查询语句", + Form: map[string]int{"csv_col_sep": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - m.Echo("%v", m.Meta) + f, e := os.Open(arg[0]) + m.Assert(e) + + s, e := os.Create(arg[1]) + m.Assert(e) + + head := false + bio := bufio.NewScanner(f) + for bio.Scan() { + line := bio.Text() + if !head { + head = true + continue + } + + args := []interface{}{} + for _, v := range strings.Split(line, m.Confx("csv_col_sep")) { + args = append(args, v) + } + + s.WriteString(fmt.Sprintf(arg[2], args...)) + s.WriteString(";\n") + } }}, }, Index: map[string]*ctx.Context{ diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index db80dc25..bd534e2e 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -3,6 +3,7 @@ package nfs // {{{ import ( // {{{ "contexts" "encoding/json" + "errors" "github.com/nsf/termbox-go" "github.com/skip2/go-qrcode" @@ -21,6 +22,8 @@ import ( // {{{ // }}} +var FileNotExist = errors.New("file not exist") + type NFS struct { in *os.File out *os.File @@ -28,6 +31,8 @@ type NFS struct { pages []string width, height int + paths []string + io io.ReadWriteCloser *bufio.Reader *bufio.Writer @@ -39,6 +44,22 @@ type NFS struct { *ctx.Context } +func (nfs *NFS) open(name string) (*os.File, error) { // {{{ + if path.IsAbs(name) { + nfs.Log("info", nil, "open %s", name) + return os.Open(name) + } + for i := len(nfs.paths) - 1; i >= 0; i-- { + if f, e := os.Open(path.Join(nfs.paths[i], name)); e == nil { + nfs.Log("info", nil, "open %s", path.Join(nfs.paths[i], name)) + return f, e + } + } + nfs.Log("info", nil, "open %s", name) + return os.Open(name) +} + +// }}} func dir(m *ctx.Message, name string, level int) { // {{{ back, e := os.Getwd() m.Assert(e) @@ -815,20 +836,49 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", "git_info": &ctx.Config{Name: "git_info", Value: "branch status diff", Help: "命令集合"}, }, Commands: map[string]*ctx.Command{ + "paths": &ctx.Command{ + Name: "paths [add path]|[del index]|[set index path]|[index]", + Help: "设置文件搜索路径, add: 添加目录, del: 删除目录, set: 修改目录,index: 目录序号, path: 目录名", + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { // {{{ + if len(arg) == 0 { + for i, v := range nfs.paths { + m.Echo("%d: %s\n", i, v) + } + return + } + switch arg[0] { + case "add": + nfs.paths = append(nfs.paths, arg[1]) + case "del": + if i, e := strconv.Atoi(arg[1]); e == nil && i < len(nfs.paths) { + for ; i < len(nfs.paths)-1; i++ { + nfs.paths[i] = nfs.paths[i+1] + } + nfs.paths = nfs.paths[:len(nfs.paths)-1] + } + case "set": + if i, e := strconv.Atoi(arg[1]); e == nil && i < len(nfs.paths) { + nfs.paths[i] = arg[2] + m.Echo("%d: %s\n", i, nfs.paths[i]) + } + default: + if i, e := strconv.Atoi(arg[0]); e == nil && i < len(nfs.paths) { + m.Echo("%d: %s\n", i, nfs.paths[i]) + } + } + } // }}} + }}, "scan": &ctx.Command{ Name: "scan filename [nfs_name [nfs_help]]", Help: "扫描文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { // {{{ + if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { // {{{ if arg[0] == "stdio" { m.Optionv("in", os.Stdin) m.Optionv("out", os.Stdout) } else { - p := arg[0] - if !path.IsAbs(arg[0]) { - p = path.Join(m.Conf("scan_path"), arg[0]) - } - if f, e := os.Open(p); m.Assert(e) { + if f, e := nfs.open(arg[0]); m.Assert(e) { m.Optionv("in", f) } } @@ -849,7 +899,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", } switch arg[0] { case "load": - f, e := os.Open(arg[1]) + f, e := nfs.open(arg[1]) m.Assert(e) defer f.Close()