From b23fe02af530b67e6fdf75709c4b16136a76df33 Mon Sep 17 00:00:00 2001 From: shylinux Date: Thu, 20 Jan 2022 09:31:58 +0800 Subject: [PATCH] opt inner.go --- base/nfs/cat.go | 1 - conf.go | 7 ++-- core/code/c.go | 8 ++-- core/code/go.go | 94 ++++++++++++++++++---------------------------- core/code/inner.go | 77 ++++++++++++++++++------------------- core/code/js.go | 4 +- core/code/sh.go | 4 +- core/code/shy.go | 20 ++++++---- 8 files changed, 97 insertions(+), 118 deletions(-) diff --git a/base/nfs/cat.go b/base/nfs/cat.go index c85b3793..ba2e43d1 100644 --- a/base/nfs/cat.go +++ b/base/nfs/cat.go @@ -124,7 +124,6 @@ const ( REPOS = "repos" LOAD = "load" - FIND = "find" TAGS = "tags" ) const ( diff --git a/conf.go b/conf.go index 3c95b6c2..3c90f754 100644 --- a/conf.go +++ b/conf.go @@ -18,14 +18,15 @@ const ( OF = " of " INIT = "init" + EXIT = "exit" + SAVE = "save" LOAD = "load" - AUTO = "auto" SHOW = "show" + EXEC = "exec" + AUTO = "auto" PLAY = "play" HELP = "help" HTTP = "http" - SAVE = "save" - EXIT = "exit" BASE = "base" CORE = "core" diff --git a/core/code/c.go b/core/code/c.go index 3beb052f..b92ca8a2 100644 --- a/core/code/c.go +++ b/core/code/c.go @@ -13,8 +13,8 @@ import ( ) func _c_tags(m *ice.Message, key string) { - if _, e := os.Stat(path.Join(m.Option(cli.CMD_DIR), _TAGS)); e != nil { - m.Cmd(cli.SYSTEM, "ctags", "-R", "-f", _TAGS, ice.PWD) + if _, e := os.Stat(path.Join(m.Option(cli.CMD_DIR), TAGS)); e != nil { + m.Cmd(cli.SYSTEM, "ctags", "-R", "-f", TAGS, ice.PWD) } _go_tags(m, key) } @@ -82,8 +82,8 @@ func init() { m.Cmdy(mdb.SEARCH, MAN2, arg[1:]) m.Cmdy(mdb.SEARCH, MAN3, arg[1:]) _c_tags(m, kit.Select(MAIN, arg, 1)) - _go_find(m, kit.Select(MAIN, arg, 1)) - _go_grep(m, kit.Select(MAIN, arg, 1)) + _go_find(m, kit.Select(MAIN, arg, 1), arg[2]) + _go_grep(m, kit.Select(MAIN, arg, 1), arg[2]) }}, }, PlugAction())}, MAN: {Name: MAN, Help: "手册", Action: ice.MergeAction(map[string]*ice.Action{ diff --git a/core/code/go.go b/core/code/go.go index f54e07c5..231731d6 100644 --- a/core/code/go.go +++ b/core/code/go.go @@ -15,14 +15,14 @@ import ( ) func _go_tags(m *ice.Message, key string) { - if s, e := os.Stat(path.Join(m.Option(cli.CMD_DIR), _TAGS)); os.IsNotExist(e) || s.ModTime().Before(time.Now().Add(kit.Duration("-72h"))) { - m.Cmd(cli.SYSTEM, "gotags", "-R", "-f", _TAGS, ice.PWD) + if s, e := os.Stat(path.Join(m.Option(cli.CMD_DIR), TAGS)); os.IsNotExist(e) || s.ModTime().Before(time.Now().Add(kit.Duration("-72h"))) { + m.Cmd(cli.SYSTEM, "gotags", "-R", "-f", TAGS, ice.PWD) } ls := strings.Split(key, ice.PT) key = ls[len(ls)-1] - for _, l := range strings.Split(m.Cmdx(cli.SYSTEM, GREP, "^"+key+"\\>", _TAGS), ice.NL) { + for _, l := range strings.Split(m.Cmdx(cli.SYSTEM, nfs.GREP, "^"+key+"\\>", TAGS), ice.NL) { ls := strings.SplitN(l, ice.TB, 2) if len(ls) < 2 { continue @@ -41,37 +41,30 @@ func _go_tags(m *ice.Message, key string) { bio := bufio.NewScanner(f) for i := 1; bio.Scan(); i++ { if i == line || bio.Text() == text { - m.PushSearch(ice.CMD, "tags", nfs.FILE, strings.TrimPrefix(file, ice.PWD), nfs.LINE, kit.Format(i), mdb.TEXT, bio.Text()) + m.PushSearch(nfs.FILE, strings.TrimPrefix(file, ice.PWD), nfs.LINE, kit.Format(i), mdb.TEXT, bio.Text()) } } } } func _go_help(m *ice.Message, key string) { - p := m.Cmd(cli.SYSTEM, GO, "doc", key).Append(cli.CMD_OUT) - if strings.TrimSpace(p) == "" { - return - } - m.PushSearch(ice.CMD, "help", nfs.FILE, key+".godoc", nfs.LINE, 1, mdb.TEXT, p) -} -func _go_find(m *ice.Message, key string) { - for _, p := range strings.Split(m.Cmdx(cli.SYSTEM, FIND, ".", "-name", key), ice.NL) { - if p == "" { - continue - } - m.PushSearch(ice.CMD, FIND, nfs.FILE, strings.TrimPrefix(p, ice.PWD), nfs.LINE, 1, mdb.TEXT, "") + if p := m.Cmd(cli.SYSTEM, GO, "doc", key).Append(cli.CMD_OUT); strings.TrimSpace(p) != "" { + m.PushSearch(nfs.FILE, key+".godoc", nfs.LINE, 1, mdb.TEXT, p) } } -func _go_grep(m *ice.Message, key string) { - msg := m.Spawn() - msg.Split(m.Cmd(cli.SYSTEM, GREP, "--exclude-dir=.git", "--exclude=.[a-z]*", "-rn", key, ice.PT).Append(cli.CMD_OUT), "file:line:text", ":", ice.NL) - msg.Table(func(index int, value map[string]string, head []string) { m.PushSearch(ice.CMD, GREP, value) }) +func _go_find(m *ice.Message, key string, dir string) { + m.Cmd(nfs.FIND, dir, key).Table(func(index int, value map[string]string, head []string) { + m.PushSearch(nfs.LINE, 1, value) + }) +} +func _go_grep(m *ice.Message, key string, dir string) { + m.Cmd(nfs.GREP, dir, key).Table(func(index int, value map[string]string, head []string) { + m.PushSearch(value) + }) } const ( - _TAGS = ".tags" - FIND = "find" - GREP = "grep" - MAIN = "main" + TAGS = ".tags" + MAIN = "main" ) const GO = "go" const MOD = "mod" @@ -86,13 +79,11 @@ func init() { m.Cmd(mdb.SEARCH, mdb.CREATE, GO, m.Prefix(GO)) m.Cmd(mdb.ENGINE, mdb.CREATE, GO, m.Prefix(GO)) - for _, k := range []string{GODOC, PROTO, SUM, MOD, GO} { + LoadPlug(m, GO, MOD, SUM, PROTO) + for _, k := range []string{GO, MOD, SUM, PROTO, GODOC} { m.Cmd(mdb.PLUGIN, mdb.CREATE, k, m.Prefix(k)) m.Cmd(mdb.RENDER, mdb.CREATE, k, m.Prefix(k)) } - LoadPlug(m, GO) - LoadPlug(m, MOD) - LoadPlug(m, PROTO) }}, GODOC: {Name: GODOC, Help: "文档", Action: ice.MergeAction(map[string]*ice.Action{ mdb.RENDER: {Hand: func(m *ice.Message, arg ...string) { @@ -100,53 +91,42 @@ func init() { m.Echo(m.Cmdx(cli.SYSTEM, GO, "doc", strings.TrimSuffix(arg[1], ice.PT+arg[0]))) }}, }, PlugAction())}, - PROTO: {Name: PROTO, Help: "协议", Action: ice.MergeAction(map[string]*ice.Action{}, PlugAction())}, - SUM: {Name: SUM, Help: "版本", Action: ice.MergeAction(map[string]*ice.Action{}, PlugAction())}, - MOD: {Name: MOD, Help: "模块", Action: ice.MergeAction(map[string]*ice.Action{}, PlugAction())}, + PROTO: {Name: PROTO, Help: "协议", Action: PlugAction()}, + SUM: {Name: SUM, Help: "版本", Action: PlugAction()}, + MOD: {Name: MOD, Help: "模块", Action: PlugAction()}, GO: {Name: GO, Help: "后端", Action: ice.MergeAction(map[string]*ice.Action{ - mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { - m.Option(cli.CMD_DIR, arg[2]) - if strings.HasSuffix(arg[1], "test.go") { - m.Cmdy(cli.SYSTEM, GO, "test", "-v", ice.PWD+arg[1]) - } else { - m.Cmdy(cli.SYSTEM, GO, "run", ice.PWD+arg[1]) - } - m.Set(ice.MSG_APPEND) - }}, mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) { if arg[0] == mdb.FOREACH { return } _go_tags(m, kit.Select(MAIN, arg, 1)) _go_help(m, kit.Select(MAIN, arg, 1)) - // _go_find(m, kit.Select(MAIN, arg, 1)) - // _go_grep(m, kit.Select(MAIN, arg, 1)) + // _go_find(m, kit.Select(MAIN, arg, 1), arg[2]) + // _go_grep(m, kit.Select(MAIN, arg, 1), arg[2]) + }}, + mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { + if m.Option(cli.CMD_DIR, arg[2]); strings.HasSuffix(arg[1], "_test.go") { + m.Cmdy(cli.SYSTEM, GO, "test", "-v", ice.PWD+arg[1]) + } else { + m.Cmdy(cli.SYSTEM, GO, "run", ice.PWD+arg[1]) + } }}, }, PlugAction())}, }, Configs: map[string]*ice.Config{ PROTO: {Name: PROTO, Help: "协议", Value: kit.Data(PLUG, kit.Dict( - PREFIX, kit.Dict("//", COMMENT), - PREPARE, kit.Dict( - KEYWORD, kit.Simple( - "syntax", "option", "package", "import", "service", "message", - ), - DATATYPE, kit.Simple( - "string", "int64", "int32", - ), + PREFIX, kit.Dict("//", COMMENT), PREPARE, kit.Dict( + KEYWORD, kit.Simple("syntax", "option", "package", "import", "service", "message"), + DATATYPE, kit.Simple("string", "int64", "int32"), ), KEYWORD, kit.Dict(), ))}, MOD: {Name: MOD, Help: "模块", Value: kit.Data(PLUG, kit.Dict( - PREFIX, kit.Dict("//", COMMENT), - PREPARE, kit.Dict( - KEYWORD, kit.Simple( - "go", "module", "require", "replace", "=>", - ), + PREFIX, kit.Dict("//", COMMENT), PREPARE, kit.Dict( + KEYWORD, kit.Simple("go", "module", "require", "replace", "=>"), ), KEYWORD, kit.Dict(), ))}, GO: {Name: GO, Help: "后端", Value: kit.Data(PLUG, kit.Dict( SPLIT, kit.Dict("space", "\t ", "operator", "{[(&.,:;!|<>)]}"), - PREFIX, kit.Dict("// ", COMMENT, "/*", COMMENT, "* ", COMMENT), - PREPARE, kit.Dict( + PREFIX, kit.Dict("// ", COMMENT, "/*", COMMENT, "* ", COMMENT), PREPARE, kit.Dict( KEYWORD, kit.Simple( "package", "import", "type", "struct", "interface", "const", "var", "func", "if", "else", "for", "range", "break", "continue", diff --git a/core/code/inner.go b/core/code/inner.go index a608b2f2..69e57c3b 100644 --- a/core/code/inner.go +++ b/core/code/inner.go @@ -13,46 +13,52 @@ import ( kit "shylinux.com/x/toolkits" ) +func _inner_list(m *ice.Message, ext, file, dir string, arg ...string) { + if !m.Right(dir, file) { + return // 没有权限 + } + if m.Conf(nfs.CAT, kit.Keym(ssh.SOURCE, ext)) == ice.TRUE { + m.Cmdy(nfs.CAT, path.Join(dir, file)) + } else { + _inner_show(m, ext, file, dir, arg...) + } +} func _inner_exec(m *ice.Message, ext, file, dir string, arg ...string) { if !m.Right(dir, file) { return // 没有权限 } - defer m.StatusTime() if m.Cmdy(mdb.ENGINE, ext, file, dir, arg); m.Result() != "" { return // 执行成功 } - if ls := kit.Simple(m.Confv(INNER, kit.Keym(EXEC, ext))); len(ls) > 0 { + if ls := kit.Simple(m.Configv(kit.Keys(EXEC, ext))); len(ls) > 0 { m.Cmdy(cli.SYSTEM, ls, file, ice.Option{cli.CMD_DIR, dir}) - m.Set(ice.MSG_APPEND) } } -func _inner_list(m *ice.Message, ext, file, dir string, arg ...string) { +func _inner_show(m *ice.Message, ext, file, dir string, arg ...string) { if !m.Right(dir, file) { return // 没有权限 } if m.Cmdy(mdb.RENDER, ext, file, dir, arg); m.Result() != "" { return // 解析成功 } - if m.Config(kit.Keys(ssh.SOURCE, ext)) == ice.TRUE { - m.Cmdy(nfs.CAT, path.Join(dir, file)) - } } -func LoadPlug(m *ice.Message, language string) { - m.Confm(language, kit.Keym(PLUG, PREPARE), func(key string, value interface{}) { - for _, v := range kit.Simple(value) { - m.Conf(language, kit.Keym(PLUG, KEYWORD, v), key) - } - }) +func LoadPlug(m *ice.Message, language ...string) { + for _, language := range language { + m.Conf(nfs.CAT, kit.Keym(nfs.SOURCE, language), ice.TRUE) + m.Confm(language, kit.Keym(PLUG, PREPARE), func(key string, value interface{}) { + for _, v := range kit.Simple(value) { + m.Conf(language, kit.Keym(PLUG, KEYWORD, v), key) + } + }) + } } func PlugAction(fields ...string) map[string]*ice.Action { return ice.SelectAction(map[string]*ice.Action{ - mdb.PLUGIN: {Hand: func(m *ice.Message, arg ...string) { - m.Debug("what %v", m.Config(PLUG)) - m.Echo(m.Config(PLUG)) - }}, + mdb.PLUGIN: {Hand: func(m *ice.Message, arg ...string) { m.Echo(m.Config(PLUG)) }}, mdb.RENDER: {Hand: func(m *ice.Message, arg ...string) { m.Cmdy(nfs.CAT, path.Join(arg[2], arg[1])) }}, + mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { m.Cmdy(nfs.CAT, path.Join(arg[2], arg[1])) }}, }, fields...) } @@ -71,18 +77,13 @@ const ( const ( PLUG = "plug" EXEC = "exec" + SHOW = "show" ) const INNER = "inner" func init() { Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ INNER: {Name: "inner path=src/@key file=main.go line=1 auto", Help: "源代码", Meta: kit.Dict(ice.DisplayLocal("")), Action: ice.MergeAction(map[string]*ice.Action{ - mdb.PLUGIN: {Name: "plugin", Help: "插件", Hand: func(m *ice.Message, arg ...string) { - if m.Cmdy(mdb.PLUGIN, arg); m.Result() == "" { - m.Echo(kit.Select("{}", m.Config(kit.Keys(PLUG, arg[0])))) - } - m.Set(ice.MSG_STATUS) - }}, mdb.SEARCH: {Name: "search", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { m.Option(nfs.DIR_ROOT, arg[2]) m.Option(cli.CMD_DIR, kit.Path(arg[2])) @@ -98,9 +99,18 @@ func init() { m.Cmdy(INNER, nfs.GREP, arg[1]) } }}, + mdb.PLUGIN: {Name: "plugin", Help: "插件", Hand: func(m *ice.Message, arg ...string) { + if m.Cmdy(mdb.PLUGIN, arg); m.Result() == "" { + m.Echo(kit.Select("{}", m.Config(kit.Keys(PLUG, arg[0])))) + } + m.Set(ice.MSG_STATUS) + }}, mdb.ENGINE: {Name: "engine", Help: "引擎", Hand: func(m *ice.Message, arg ...string) { _inner_exec(m, arg[0], arg[1], arg[2]) }}, + mdb.RENDER: {Name: "render", Help: "渲染", Hand: func(m *ice.Message, arg ...string) { + _inner_show(m, arg[0], arg[1], arg[2]) + }}, nfs.TAGS: {Name: "tags", Help: "索引", Hand: func(m *ice.Message, arg ...string) { }}, nfs.GREP: {Name: "grep", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { @@ -109,8 +119,7 @@ func init() { }}, cli.MAKE: {Name: "make", Help: "构建", Hand: func(m *ice.Message, arg ...string) { msg := m.Cmd(cli.SYSTEM, cli.MAKE, arg) - list := strings.Split(msg.Append(cli.CMD_ERR), ice.NL) - for _, line := range list { + for _, line := range strings.Split(msg.Append(cli.CMD_ERR), ice.NL) { if strings.Contains(line, ice.DF) { if ls := strings.SplitN(line, ice.DF, 4); len(ls) > 3 { m.Push(nfs.FILE, strings.TrimPrefix(ls[0], m.Option(nfs.PATH))) @@ -151,27 +160,13 @@ func init() { return } m.Option("exts", "inner/search.js?a=1,inner/favor.js") - arg[1] = kit.Split(arg[1])[0] - _inner_list(m, kit.Ext(arg[1]), arg[1], arg[0]) - if m.IsErrNotFound() { - m.SetResult() - m.Echo("") + if _inner_list(m, kit.Ext(arg[1]), arg[1], arg[0]); m.IsErrNotFound() { + m.SetResult("") } m.Set(ice.MSG_STATUS) }}, }, Configs: map[string]*ice.Config{ INNER: {Name: "inner", Help: "源代码", Value: kit.Data( - ssh.SOURCE, kit.Dict( - "s", ice.TRUE, "S", ice.TRUE, - "shy", ice.TRUE, "py", ice.TRUE, - "csv", ice.TRUE, "json", ice.TRUE, - "css", ice.TRUE, "html", ice.TRUE, - "txt", ice.TRUE, "url", ice.TRUE, - "log", ice.TRUE, "err", ice.TRUE, - - "md", ice.TRUE, "license", ice.TRUE, "makefile", ice.TRUE, "sql", ice.TRUE, - "ini", ice.TRUE, "conf", ice.TRUE, "toml", ice.TRUE, "yaml", ice.TRUE, "yml", ice.TRUE, - ), EXEC, kit.Dict( "py", []string{"python"}, "js", []string{"node"}, diff --git a/core/code/js.go b/core/code/js.go index 441ef7cc..6006f9f0 100644 --- a/core/code/js.go +++ b/core/code/js.go @@ -38,8 +38,8 @@ func init() { if arg[0] == mdb.FOREACH { return } - _go_find(m, kit.Select(MAIN, arg, 1)) - _go_grep(m, kit.Select(MAIN, arg, 1)) + _go_find(m, kit.Select(MAIN, arg, 1), arg[2]) + _go_grep(m, kit.Select(MAIN, arg, 1), arg[2]) }}, }, PlugAction())}, NODE: {Name: "node auto download", Help: "前端", Action: map[string]*ice.Action{ diff --git a/core/code/sh.go b/core/code/sh.go index b0f3ba30..97cc6a62 100644 --- a/core/code/sh.go +++ b/core/code/sh.go @@ -30,8 +30,8 @@ func init() { m.Option(cli.CMD_DIR, kit.Select(ice.SRC, arg, 2)) m.Cmdy(mdb.SEARCH, MAN1, arg[1:]) m.Cmdy(mdb.SEARCH, MAN8, arg[1:]) - _go_find(m, kit.Select(MAIN, arg, 1)) - _go_grep(m, kit.Select(MAIN, arg, 1)) + _go_find(m, kit.Select(MAIN, arg, 1), arg[2]) + _go_grep(m, kit.Select(MAIN, arg, 1), arg[2]) }}, MAN: {Hand: func(m *ice.Message, arg ...string) { m.Echo(_c_help(m, arg[0], arg[1])) diff --git a/core/code/shy.go b/core/code/shy.go index 211fe5ca..c82e21a1 100644 --- a/core/code/shy.go +++ b/core/code/shy.go @@ -5,6 +5,7 @@ import ( ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" kit "shylinux.com/x/toolkits" ) @@ -13,21 +14,24 @@ const SHY = "shy" func init() { Index.Register(&ice.Context{Name: SHY, Help: "脚本", Commands: map[string]*ice.Command{ ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - for _, cmd := range []string{mdb.PLUGIN, mdb.RENDER, mdb.ENGINE, mdb.SEARCH} { + for _, cmd := range []string{mdb.SEARCH, mdb.PLUGIN, mdb.RENDER, mdb.ENGINE} { m.Cmd(cmd, mdb.CREATE, SHY, m.Prefix(SHY)) } LoadPlug(m, SHY) }}, SHY: {Name: SHY, Help: "脚本", Action: ice.MergeAction(map[string]*ice.Action{ - mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { + mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) { + if arg[0] == SHY { + _go_find(m, kit.Select(MAIN, arg, 1), arg[2]) + _go_grep(m, kit.Select(MAIN, arg, 1), arg[2]) + } + }}, + mdb.RENDER: {Hand: func(m *ice.Message, arg ...string) { + m.ProcessCommand() m.Cmdy("web.wiki.word", path.Join(arg[2], arg[1])) }}, - mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) { - if arg[0] == mdb.FOREACH { - return - } - _go_find(m, kit.Select(MAIN, arg, 1)) - _go_grep(m, kit.Select(MAIN, arg, 1)) + mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(nfs.SOURCE, path.Join(arg[2], arg[1])) }}, }, PlugAction())}, }, Configs: map[string]*ice.Config{