From 0fdfe06c23e98d636ce18670b31a2cba0bb2a2c1 Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 24 Sep 2020 19:06:48 +0800 Subject: [PATCH] opt some --- base/gdb/gdb.go | 9 ++------- base/mdb/search.go | 4 ++++ logs.go | 1 - meta.go | 5 ++++- misc/alpha/alpha.go | 21 +++++++++++++++++---- type.go | 42 +++++++++++++++++++++++++----------------- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/base/gdb/gdb.go b/base/gdb/gdb.go index 0197291f..14bb960a 100644 --- a/base/gdb/gdb.go +++ b/base/gdb/gdb.go @@ -94,17 +94,12 @@ const ( BEGIN = "begin" END = "end" - START = "start" - STOP = "stop" OPEN = "open" CLOSE = "close" + START = "start" + STOP = "stop" - RELOAD = "reload" RESTART = "restart" - CHANGE = "change" - BUILD = "build" - PRUNE = "prune" - CLEAR = "clear" ) const ( diff --git a/base/mdb/search.go b/base/mdb/search.go index 9aceef39..d9a91bd1 100644 --- a/base/mdb/search.go +++ b/base/mdb/search.go @@ -35,6 +35,10 @@ func init() { m.Cmdy(kit.Keys(value[kit.MDB_TEXT], value[kit.MDB_NAME]), SEARCH, arg[0], arg[1], kit.Select("", arg, 2)) }) } + + m.Richs(SEARCH, nil, "alpha", func(key string, value map[string]interface{}) { + m.Cmdy(kit.Keys(value[kit.MDB_TEXT], value[kit.MDB_NAME]), SEARCH, arg[0], strings.ToLower(arg[1]), kit.Select("", arg, 2)) + }) }}, }}, nil) } diff --git a/logs.go b/logs.go index 304df1ea..e8eaf82b 100644 --- a/logs.go +++ b/logs.go @@ -13,7 +13,6 @@ var ErrNotLogin = "not login: " var ErrNotAuth = "not auth: " var ErrNotJoin = "not join: " var ErrNotFound = "not found: " -var ErrNotEnough = "not enough: " var ErrStart = "err start: " func (m *Message) log(level string, str string, arg ...interface{}) *Message { diff --git a/meta.go b/meta.go index 7d262606..d4bcf788 100644 --- a/meta.go +++ b/meta.go @@ -484,9 +484,12 @@ func (m *Message) Append(key string, arg ...interface{}) string { } func (m *Message) Appendv(key string, arg ...interface{}) []string { if key == MSG_APPEND { - m.meta[MSG_APPEND] = kit.Simple(arg) + if len(arg) > 0 { + m.meta[MSG_APPEND] = kit.Simple(arg) + } return m.meta[key] } + if key == "_index" { max := 0 for _, k := range m.meta[MSG_APPEND] { diff --git a/misc/alpha/alpha.go b/misc/alpha/alpha.go index 29fac0c4..de048c02 100644 --- a/misc/alpha/alpha.go +++ b/misc/alpha/alpha.go @@ -13,7 +13,7 @@ import ( "strings" ) -func _alpha_find(m *ice.Message, method, word string) { +func _alpha_find(m *ice.Message, method, word string) *ice.Message { // 搜索方法 switch word = strings.TrimSpace(word); method { case "line": @@ -31,6 +31,7 @@ func _alpha_find(m *ice.Message, method, word string) { m.Push(k, value[k]) } }) + return m } func _alpha_load(m *ice.Message, file, name string) { // 清空数据 @@ -66,16 +67,28 @@ var Index = &ice.Context{Name: ALPHA, Help: "英汉词典", )}, }, Commands: map[string]*ice.Command{ - ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Load() }}, - ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Save() }}, - ALPHA: {Name: "alpha method=word,line word auto", Help: "英汉", Action: map[string]*ice.Action{ mdb.IMPORT: {Name: "import file=usr/word-dict/ecdict name", Help: "加载词库", Hand: func(m *ice.Message, arg ...string) { _alpha_load(m, m.Option(kit.MDB_FILE), kit.Select(path.Base(m.Option(kit.MDB_FILE)), m.Option(kit.MDB_NAME))) }}, + mdb.SEARCH: {Name: "search", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { + _alpha_find(m.Spawn(), "word", arg[1]).Table(func(index int, value map[string]string, head []string) { + m.Push("file", "") + m.Push("line", arg[1]) + m.Push("text", value["definition"]+"\n"+value["translation"]) + }) + }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { _alpha_find(m, arg[0], arg[1]) }}, + + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Load() + m.Cmd(mdb.SEARCH, mdb.CREATE, ALPHA, ALPHA, c.Cap(ice.CTX_FOLLOW)) + }}, + ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Save() + }}, }, } diff --git a/type.go b/type.go index 72080501..0e9118df 100644 --- a/type.go +++ b/type.go @@ -544,11 +544,11 @@ func (m *Message) Search(key interface{}, cb interface{}) *Message { p := m.target.root if ctx, ok := names[key].(*Context); ok { p = ctx - } else if strings.Contains(key, ":") { - } else if key == "." { + p, key = m.target, "" + } else if key == ".." { if m.target.context != nil { - p = m.target.context + p, key = m.target.context, "" } } else if strings.Contains(key, ".") { list := strings.Split(key, ".") @@ -579,6 +579,14 @@ func (m *Message) Search(key interface{}, cb interface{}) *Message { // 遍历命令 switch cb := cb.(type) { + case func(key string, cmd *Command): + if key == "" { + for k, v := range p.Commands { + cb(k, v) + } + break + } + case func(p *Context, s *Context, key string, cmd *Command): if key == "" { for k, v := range p.Commands { @@ -639,22 +647,22 @@ func (m *Message) Cmd(arg ...interface{}) *Message { } func (m *Message) Confv(arg ...interface{}) (val interface{}) { m.Search(arg[0], func(p *Context, s *Context, key string, conf *Config) { - if len(arg) > 1 { - if len(arg) > 2 { - if arg[1] == nil { - // 写配置 - conf.Value = arg[2] - } else { - // 写修改项 - kit.Value(conf.Value, arg[1:]...) - } - } - // 读配置项 - val = kit.Value(conf.Value, arg[1]) - } else { - // 读配置 + if len(arg) == 1 { val = conf.Value + return // 读配置 } + + if len(arg) > 2 { + if arg[1] == nil || arg[1] == "" { + // 写配置 + conf.Value = arg[2] + } else { + // 写修改项 + kit.Value(conf.Value, arg[1:]...) + } + } + // 读配置项 + val = kit.Value(conf.Value, arg[1]) }) return }