From caa1fb64c1dc76e291f81267358b6392bc1b4eeb Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 13 Jul 2020 10:03:10 +0800 Subject: [PATCH] opt inner --- base/nfs/nfs.go | 9 +++-- base/web/cache.go | 29 +++++++++------ core/code/c.go | 42 +++++++++++++++++---- core/code/go.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++ core/code/inner.go | 32 ++++++++++++++-- core/wiki/wiki.go | 12 +++--- core/wiki/word.go | 2 + 7 files changed, 188 insertions(+), 31 deletions(-) create mode 100644 core/code/go.go diff --git a/base/nfs/nfs.go b/base/nfs/nfs.go index a9a313a8..6a505107 100644 --- a/base/nfs/nfs.go +++ b/base/nfs/nfs.go @@ -24,6 +24,7 @@ func _file_ext(name string) string { } func _file_list(m *ice.Message, root string, name string, level int, deep bool, dir_type string, dir_reg *regexp.Regexp, fields []string) { + m.Debug("fuck %v %v", root, name) if fs, e := ioutil.ReadDir(path.Join(root, name)); e != nil { if f, e := os.Open(path.Join(root, name)); e == nil { defer f.Close() @@ -136,7 +137,7 @@ func _file_list(m *ice.Message, root string, name string, level int, deep bool, } } if f.IsDir() && deep { - _file_list(m, root, p, level+1, deep, dir_type, dir_reg, fields) + _file_list(m, root, path.Join(name, f.Name()), level+1, deep, dir_type, dir_reg, fields) } } } @@ -311,9 +312,9 @@ var Index = &ice.Context{Name: "nfs", Help: "存储模块", _file_list(m, "./", arg[1], 0, false, "both", nil, []string{"time", "size", "type", "path"}) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - rg, _ := regexp.Compile(m.Option("dir_reg")) - _file_list(m, kit.Select("./", m.Option("dir_root")), kit.Select("", arg, 0), - 0, m.Options("dir_deep"), kit.Select("both", m.Option("dir_type")), rg, + rg, _ := regexp.Compile(m.Option(DIR_REG)) + _file_list(m, kit.Select("./", m.Option(DIR_ROOT)), kit.Select("", arg, 0), + 0, m.Options(DIR_DEEP), kit.Select("both", m.Option(DIR_TYPE)), rg, strings.Split(kit.Select("time size line path", strings.Join(arg[1:], " ")), " ")) }}, CAT: {Name: "cat file", Help: "查看", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/base/web/cache.go b/base/web/cache.go index ece86e6e..4bb96c5e 100644 --- a/base/web/cache.go +++ b/base/web/cache.go @@ -6,7 +6,6 @@ import ( kit "github.com/shylinux/toolkits" "io" - "io/ioutil" "net/http" "os" "path" @@ -103,19 +102,27 @@ func _cache_upload(m *ice.Message, r *http.Request) (kind, name, file, size stri return "", "", "", "0" } func _cache_download(m *ice.Message, r *http.Response) (file, size string) { - if buf, e := ioutil.ReadAll(r.Body); m.Assert(e) { - defer r.Body.Close() + defer r.Body.Close() - // 创建文件 - if f, p, e := kit.Create(_cache_name(m, kit.Hashs(buf))); m.Assert(e) { - defer f.Close() - - // 导入数据 - if n, e := f.Write(buf); m.Assert(e) { - m.Log_IMPORT(kit.MDB_FILE, p, kit.MDB_SIZE, kit.FmtSize(int64(n))) - return p, kit.Format(n) + total := kit.Int(kit.Select("1", r.Header.Get("Content-Length"))) + if f, p, e := kit.Create(path.Join("var/tmp", kit.Hashs("uniq"))); m.Assert(e) { + size, buf := 0, make([]byte, 81920) + for { + if n, e := r.Body.Read(buf); e == nil { + f.Write(buf[0:n]) + size += n + m.Log_IMPORT(kit.MDB_FILE, p, "per", size*100/total, kit.MDB_SIZE, kit.FmtSize(int64(size)), "total", kit.FmtSize(int64(total))) + } else if e == io.EOF { + f.Close() + break + } else { + break } } + m.Log_IMPORT(kit.MDB_FILE, p, kit.MDB_SIZE, kit.FmtSize(int64(size))) + c := _cache_name(m, kit.Hashs(f)) + m.Cmd(nfs.LINK, c, p) + return c, kit.Format(size) } return "", "0" } diff --git a/core/code/c.go b/core/code/c.go index 58016d84..709911f0 100644 --- a/core/code/c.go +++ b/core/code/c.go @@ -13,18 +13,16 @@ import ( ) func _c_find(m *ice.Message, key string) { - m.Option(cli.CMD_DIR, "") - for _, p := range strings.Split(m.Cmdx(cli.SYSTEM, "find", m.Option("_path"), "-name", key), "\n") { + for _, p := range strings.Split(m.Cmdx(cli.SYSTEM, "find", ".", "-name", key), "\n") { if p == "" { continue } - m.Push("file", p) + m.Push("file", strings.TrimPrefix(p, "./")) m.Push("line", 1) m.Push("text", "") } } func _c_tags(m *ice.Message, key string) { - m.Option(cli.CMD_DIR, m.Option("_path")) if _, e := os.Stat(path.Join(m.Option("_path"), ".tags")); e != nil { m.Cmd(cli.SYSTEM, "ctags", "-R", "-f", ".tags", "./") } @@ -46,16 +44,41 @@ func _c_tags(m *ice.Message, key string) { bio := bufio.NewScanner(f) for i := 1; bio.Scan(); i++ { if i == line || bio.Text() == text { - m.Push("file", p) + m.Push("file", strings.TrimPrefix(file, "./")) m.Push("line", i) m.Push("text", bio.Text()) } } } + m.Sort("line", "int") } func _c_grep(m *ice.Message, key string) { - m.Option(cli.CMD_DIR, "") - m.Split(m.Cmdx(cli.SYSTEM, "grep", "--exclude=.*", "-rn", key, m.Option("_path")), "file:line:text", ":", "\n") + m.Split(m.Cmd(cli.SYSTEM, "grep", "--exclude=.[a-z]*", "-rn", key, ".").Append(cli.CMD_OUT), "file:line:text", ":", "\n") +} +func _c_help(m *ice.Message, section, key string) { + p := m.Cmd(cli.SYSTEM, "man", section, key).Append(cli.CMD_OUT) + if p == "" { + return + } + ls := strings.Split(p, "\n") + + if len(ls) > 20 { + p = strings.Join(ls[:20], "\n") + } + p = strings.Replace(p, "_\x08", "", -1) + res := make([]byte, 0, len(p)) + for i := 0; i < len(p); i++ { + switch p[i] { + case '\x08': + i++ + default: + res = append(res, p[i]) + } + } + + m.Push("file", key+".man"+section) + m.Push("line", 1) + m.Push("text", string(res)) } func init() { Index.Register(&ice.Context{Name: "c", Help: "c", @@ -63,10 +86,15 @@ func init() { ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmd(mdb.SEARCH, mdb.CREATE, "h", "c", c.Cap(ice.CTX_FOLLOW)) m.Cmd(mdb.SEARCH, mdb.CREATE, "c", "c", c.Cap(ice.CTX_FOLLOW)) + m.Cmd(mdb.SEARCH, mdb.CREATE, "man3", "c", c.Cap(ice.CTX_FOLLOW)) + m.Cmd(mdb.SEARCH, mdb.CREATE, "man2", "c", c.Cap(ice.CTX_FOLLOW)) }}, "c": {Name: "c", Help: "c", Action: map[string]*ice.Action{ mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) { + m.Option(cli.CMD_DIR, m.Option("_path")) _c_find(m, kit.Select("main", arg, 1)) + _c_help(m, "2", kit.Select("main", arg, 1)) + _c_help(m, "3", kit.Select("main", arg, 1)) _c_tags(m, kit.Select("main", arg, 1)) _c_grep(m, kit.Select("main", arg, 1)) }}, diff --git a/core/code/go.go b/core/code/go.go new file mode 100644 index 00000000..860cd899 --- /dev/null +++ b/core/code/go.go @@ -0,0 +1,93 @@ +package code + +import ( + "bufio" + "os" + "path" + "strings" + + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + "github.com/shylinux/icebergs/base/mdb" + kit "github.com/shylinux/toolkits" +) + +func _go_find(m *ice.Message, key string) { + for _, p := range strings.Split(m.Cmdx(cli.SYSTEM, "find", ".", "-name", key), "\n") { + if p == "" { + continue + } + m.Push("file", strings.TrimPrefix(p, "./")) + m.Push("line", 1) + m.Push("text", "") + } +} +func _go_tags(m *ice.Message, key string) { + if _, e := os.Stat(path.Join(m.Option("_path"), ".tags")); e != nil { + m.Cmd(cli.SYSTEM, "gotags", "-R", "-f", ".tags", "./") + } + for _, l := range strings.Split(m.Cmdx(cli.SYSTEM, "grep", "^"+key, ".tags"), "\n") { + ls := strings.SplitN(l, "\t", 2) + if len(ls) < 2 { + continue + } + + ls = strings.SplitN(ls[1], "\t", 2) + file := ls[0] + ls = strings.SplitN(ls[1], ";\"", 2) + text := strings.TrimSuffix(strings.TrimPrefix(ls[0], "/^"), "$/") + line := kit.Int(text) + + p := path.Join(m.Option("_path"), file) + f, e := os.Open(p) + m.Assert(e) + bio := bufio.NewScanner(f) + for i := 1; bio.Scan(); i++ { + if i == line || bio.Text() == text { + m.Push("file", strings.TrimPrefix(file, "./")) + m.Push("line", i) + m.Push("text", bio.Text()) + } + } + } + m.Sort("line", "int") +} +func _go_grep(m *ice.Message, key string) { + m.Split(m.Cmd(cli.SYSTEM, "grep", "--exclude-dir=.git", "--exclude=.[a-z]*", "-rn", key, ".").Append(cli.CMD_OUT), "file:line:text", ":", "\n") +} +func _go_help(m *ice.Message, key string) { + p := m.Cmd(cli.SYSTEM, "go", "doc", key).Append(cli.CMD_OUT) + if p == "" { + return + } + ls := strings.Split(p, "\n") + if len(ls) > 10 { + ls = ls[:10] + } + res := strings.Join(ls, "\n") + + m.Push("file", key+".godoc") + m.Push("line", 1) + m.Push("text", string(res)) +} +func init() { + Index.Register(&ice.Context{Name: "go", Help: "go", + Commands: map[string]*ice.Command{ + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(mdb.SEARCH, mdb.CREATE, "go", "go", c.Cap(ice.CTX_FOLLOW)) + m.Cmd(mdb.SEARCH, mdb.CREATE, "godoc", "go", c.Cap(ice.CTX_FOLLOW)) + }}, + "go": {Name: "go", Help: "go", Action: map[string]*ice.Action{ + mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) { + m.Option(cli.CMD_DIR, m.Option("_path")) + _go_find(m, kit.Select("main", arg, 1)) + _go_tags(m, kit.Select("main", arg, 1)) + _go_help(m, kit.Select("main", arg, 1)) + _go_grep(m, kit.Select("main", arg, 1)) + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + + }}, + }, + }, nil) +} diff --git a/core/code/inner.go b/core/code/inner.go index 35a5b16e..1b68f1db 100644 --- a/core/code/inner.go +++ b/core/code/inner.go @@ -4,6 +4,7 @@ import ( ice "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/icebergs/base/mdb" + "github.com/shylinux/icebergs/base/nfs" "github.com/shylinux/icebergs/base/web" kit "github.com/shylinux/toolkits" @@ -53,7 +54,8 @@ func _inner_list(m *ice.Message, name string) { } if m.Set(ice.MSG_RESULT); strings.HasSuffix(name, "/") || _inner_source(m, name) { - m.Cmdy("nfs.dir", name, "file size time") + m.Option(nfs.DIR_DEEP, "true") + m.Cmdy(nfs.DIR, name, "path size time") return } m.Echo(name) @@ -118,6 +120,29 @@ func _inner_main(m *ice.Message, arg ...string) { if len(arg) > 2 { arg = arg[:2] } + p := _inner_ext(arg[1]) + key := strings.TrimSuffix(path.Base(arg[1]), "."+p) + switch p { + case "godoc": + m.Option(cli.CMD_DIR, arg[0]) + m.Echo(m.Cmdx(cli.SYSTEM, "go", "doc", key)) + + case "man3", "man2", "man1": + p := m.Cmdx(cli.SYSTEM, "man", strings.TrimPrefix(p, "man"), key) + p = strings.Replace(p, "_\x08", "", -1) + res := make([]byte, 0, len(p)) + for i := 0; i < len(p); i++ { + switch p[i] { + case '\x08': + i++ + default: + res = append(res, p[i]) + } + } + + m.Echo(string(res)) + return + } _inner_list(m, path.Join(arg...)) } @@ -133,6 +158,8 @@ func init() { "go", "true", "js", "true", "c", "true", "h", "true", "makefile", "true", + "mod", "true", + "sum", "true", ), "plug", kit.Dict( "py", kit.Dict( @@ -150,7 +177,7 @@ func init() { )}, }, Commands: map[string]*ice.Command{ - INNER: {Name: "inner path=usr/demo file=hi.qrc line=1 auto", Help: "编辑器", Meta: kit.Dict( + INNER: {Name: "inner path=usr/demo file=hi.qrc line=1 查看:button=auto", Help: "编辑器", Meta: kit.Dict( "display", "/plugin/local/code/inner.js", "style", "editor", ), Action: map[string]*ice.Action{ "cmd": {Name: "cmd arg", Help: "命令", Hand: func(m *ice.Message, arg ...string) { @@ -215,7 +242,6 @@ func init() { m.Cmdy(web.CACHE, web.UPLOAD) m.Cmdy(web.CACHE, web.WATCH, m.Option(web.DATA), path.Join(m.Option("path"), m.Option("name"))) }}, - mdb.SEARCH: {Name: "search type name text arg...", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(mdb.SEARCH, arg) }}, diff --git a/core/wiki/wiki.go b/core/wiki/wiki.go index e3625d77..a8274f04 100644 --- a/core/wiki/wiki.go +++ b/core/wiki/wiki.go @@ -38,16 +38,16 @@ func _wiki_upload(m *ice.Message, cmd string) { func reply(m *ice.Message, cmd string, arg ...string) bool { // 文件列表 - m.Option("dir_root", m.Conf(cmd, "meta.path")) - m.Option("dir_reg", m.Conf(cmd, "meta.regs")) - m.Cmdy("nfs.dir", kit.Select("./", arg, 0)) + m.Option(nfs.DIR_ROOT, m.Conf(cmd, "meta.path")) + m.Option(nfs.DIR_REG, m.Conf(cmd, "meta.regs")) + m.Cmdy(nfs.DIR, kit.Select("./", arg, 0)) m.Sort("time", "time_r") if len(arg) == 0 || strings.HasSuffix(arg[0], "/") { // 目录列表 - m.Option("dir_reg", "") - m.Option("dir_type", "dir") - m.Cmdy("nfs.dir", kit.Select("./", arg, 0)) + m.Option(nfs.DIR_REG, "") + m.Option(nfs.DIR_TYPE, "dir") + m.Cmdy(nfs.DIR, kit.Select("./", arg, 0)) m.Option("_display", "table") return true } diff --git a/core/wiki/word.go b/core/wiki/word.go index aabd3eca..42ae2caa 100644 --- a/core/wiki/word.go +++ b/core/wiki/word.go @@ -4,6 +4,7 @@ import ( ice "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/icebergs/base/ctx" + "github.com/shylinux/icebergs/base/nfs" "github.com/shylinux/icebergs/base/ssh" kit "github.com/shylinux/toolkits" @@ -366,6 +367,7 @@ func init() { m.Cmdy(arg[0], "action", "run", arg[1:]) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Option(nfs.DIR_DEEP, "true") if reply(m, cmd, arg...) { // 目录列表 return