diff --git a/.gitignore b/.gitignore index 17df1834..4befdd1c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ *.swp +*.swo diff --git a/core/code/code.go b/core/code/code.go index 2073ab62..90e5003c 100644 --- a/core/code/code.go +++ b/core/code/code.go @@ -249,6 +249,12 @@ var Index = &ice.Context{Name: "code", Help: "编程中心", m.Push("_output", "result") } }}, + "/input/": {Name: "/input/", Help: "编辑器", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd("cli.input.match", arg[0]).Table(func(index int, value map[string]string, head []string) { + m.Echo("%s %s\n", value["code"], value["text"]) + m.Push("_output", "result") + }) + }}, "/vim": {Name: "/vim", Help: "编辑器", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if f, _, e := m.R.FormFile("sub"); e == nil { defer f.Close() diff --git a/misc/alpha/alpha.go b/misc/alpha/alpha.go new file mode 100644 index 00000000..2b871db1 --- /dev/null +++ b/misc/alpha/alpha.go @@ -0,0 +1,112 @@ +package alpha + +import ( + "bytes" + "encoding/csv" + "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + "github.com/shylinux/toolkits" + "math/rand" + "os" + "path" +) + +var Index = &ice.Context{Name: "alpha", Help: "英汉词典", + Caches: map[string]*ice.Cache{}, + Configs: map[string]*ice.Config{ + "alpha": {Name: "alpha", Help: "英汉词典", Value: kit.Data( + "store", "var/alpha/", "limit", "2000", "least", "1000", + )}, + }, + Commands: map[string]*ice.Command{ + ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(ice.CTX_CONFIG, "load", "alpha.json") + }}, + ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(ice.CTX_CONFIG, "save", "alpha.json", "cli.alpha.alpha") + }}, + + "alpha": {Name: "alpha [load|list]", Help: "英汉词典", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + count := kit.Int(m.Conf("alpha", "meta.count")) + if len(arg) == 0 { + arg = append(arg, "list", kit.Format(count-rand.Intn(count))) + } + + switch arg[0] { + case "load": + // 加载词库 + m.Cmd(ice.MDB_IMPORT, "cli.alpha.alpha", "", "list", + m.Cmd(ice.WEB_CACHE, "catch", "csv", arg[1]).Append("data")) + case "list": + // 词汇列表 + m.Option("cache.offend", kit.Select("0", arg, 1)) + m.Option("cache.limit", kit.Select("10", arg, 2)) + m.Grows("alpha", nil, "", "", func(index int, value map[string]interface{}) { + m.Push("", value, []string{"id", "word", "translation", "definition"}) + }) + } + }}, + "random": {Name: "random [count]", Help: "随机词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + count := kit.Int(m.Conf("alpha", "meta.count")) + for i := 0; i < kit.Int(kit.Select("10", arg, 0)); i++ { + m.Cmdy("alpha", "list", count-rand.Intn(count), 1) + } + }}, + "search": {Name: "search [word [method]]", Help: "查找词汇", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) == 0 { + // 收藏列表 + m.Cmdy(ice.WEB_FAVOR, "alpha.word") + return + } + + // 搜索方法 + method := "word" + if len(arg) > 1 { + method = arg[1] + } + switch method { + case "line": + case "word": + arg[0] = "," + arg[0] + "$" + } + + // 字段列表 + field := m.Confm("alpha", "meta.field") + if field == nil { + field = map[string]interface{}{} + head := []string{} + if f, e := os.Open(path.Join(m.Conf("alpha", "meta.store"), "cli.alpha.alpha..csv")); m.Assert(e) { + defer f.Close() + bio := csv.NewReader(f) + head, e = bio.Read() + } + for i, k := range head { + field[k] = i + } + m.Conf("alpha", "meta.field", field) + } + + // 搜索词汇 + bio := csv.NewReader(bytes.NewBufferString(m.Cmdx(ice.CLI_SYSTEM, "grep", "-rh", arg[0], m.Conf("alpha", "meta.store")))) + for i := 0; i < 100; i++ { + if line, e := bio.Read(); e != nil { + break + } else { + if method == "word" && i == 0 { + // 添加收藏 + m.Cmd(ice.WEB_FAVOR, "alpha.word", "alpha", + line[kit.Int(field["word"])], line[kit.Int(field["translation"])], + "id", line[kit.Int(field["id"])], "definition", line[kit.Int(field["definition"])], + ) + } + for _, k := range []string{"id", "word", "translation", "definition"} { + // 输出词汇 + m.Push(k, line[kit.Int(field[k])]) + } + } + } + }}, + }, +} + +func init() { cli.Index.Register(Index, nil) } diff --git a/misc/alpha/alpha.md b/misc/alpha/alpha.md new file mode 100644 index 00000000..36e3cd28 --- /dev/null +++ b/misc/alpha/alpha.md @@ -0,0 +1 @@ +# {{title "alpha"}} diff --git a/misc/input/input.go b/misc/input/input.go new file mode 100644 index 00000000..9ca3a5f6 --- /dev/null +++ b/misc/input/input.go @@ -0,0 +1,119 @@ +package input + +import ( + "bufio" + "bytes" + "encoding/csv" + "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + "github.com/shylinux/toolkits" + "math/rand" + "os" + "path" + "strings" +) + +var Index = &ice.Context{Name: "input", Help: "输入法", + Caches: map[string]*ice.Cache{}, + Configs: map[string]*ice.Config{ + "input": {Name: "input", Help: "输入法", Value: kit.Data( + "store", "var/input/", "limit", "2000", "least", "1000", + )}, + }, + Commands: map[string]*ice.Command{ + ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(ice.CTX_CONFIG, "load", "input.json") + }}, + ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(ice.CTX_CONFIG, "save", "input.json", "cli.input.input") + }}, + + "input": {Name: "input load|list", Help: "输入法", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + count := kit.Int(m.Conf("input", "meta.count")) + if len(arg) == 0 { + arg = append(arg, "list", kit.Format(count-rand.Intn(count))) + } + + switch arg[0] { + case "load": + // 加载词库 + if f, e := os.Open(arg[1]); m.Assert(e) { + bio := bufio.NewScanner(f) + for bio.Scan() { + if strings.HasPrefix(bio.Text(), "#") { + continue + } + line := kit.Split(bio.Text(), " \t") + m.Grow("input", nil, kit.Dict( + "text", line[0], "code", line[1], "weight", line[2], + )) + } + } + case "list": + // 词汇列表 + m.Option("cache.offend", kit.Select("0", arg, 1)) + m.Option("cache.limit", kit.Select("10", arg, 2)) + m.Grows("input", nil, "", "", func(index int, value map[string]interface{}) { + m.Push("", value, []string{"id", "code", "text", "weight"}) + }) + } + }}, + "match": {Name: "match [word [method]]", Help: "五笔字码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) == 0 { + // 收藏列表 + m.Cmdy(ice.WEB_FAVOR, "input.word") + return + } + + // 搜索方法 + method := "word" + if len(arg) > 1 { + method = arg[1] + } + switch method { + case "line": + case "word": + arg[0] = "^" + arg[0] + "," + } + + // 字段列表 + field := m.Confm("input", "meta.field") + if field == nil { + field = map[string]interface{}{} + head := []string{} + if f, e := os.Open(path.Join(m.Conf("input", "meta.store"), "input.csv")); m.Assert(e) { + defer f.Close() + + bio := csv.NewReader(f) + head, e = bio.Read() + } + for i, k := range head { + field[k] = i + } + m.Conf("input", "meta.field", field) + } + + // 搜索词汇 + bio := csv.NewReader(bytes.NewBufferString(m.Cmdx(ice.CLI_SYSTEM, "grep", "-rh", arg[0], m.Conf("input", "meta.store")))) + for i := 0; i < 100; i++ { + if line, e := bio.Read(); e != nil { + break + } else { + if method == "word" && i == 0 { + // 添加收藏 + m.Cmd(ice.WEB_FAVOR, "input.word", "input", + line[kit.Int(field["code"])], line[kit.Int(field["text"])], + "id", line[kit.Int(field["id"])], "weight", line[kit.Int(field["weight"])], + ) + } + for _, k := range []string{"id", "code", "text", "weight"} { + // 输出词汇 + m.Push(k, line[kit.Int(field[k])]) + } + } + } + }}, + }, +} + +func init() { cli.Index.Register(Index, nil) } diff --git a/misc/input/input.md b/misc/input/input.md new file mode 100644 index 00000000..a7505685 --- /dev/null +++ b/misc/input/input.md @@ -0,0 +1 @@ +# {{title "input"}} diff --git a/misc/input/input.vim b/misc/input/input.vim new file mode 100644 index 00000000..3110f36d --- /dev/null +++ b/misc/input/input.vim @@ -0,0 +1,45 @@ + +" 变量定义 +func! InputDefine(name, value) + if !exists("name") | exec "let " . a:name . " = \"" . a:value . "\"" | endif +endfunc + +" 输出日志 +call InputDefine("g:InputLog", "input.log") +fun! InputLog(txt) + call writefile([strftime("%Y-%m-%d %H:%M:%S ") . join(a:txt, "")], g:InputLog, "a") +endfun + +" 输入转换 +call InputDefine("g:InputTrans", "localhost:9020/code/input/") +fun! InputTrans(code) + let res = [] + for line in split(system("curl -s " . g:InputTrans . a:code), "\n") + let word = split(line, " ") + if len(word) > 1 | call extend(res, [word[1]]) | endif + endfor + let res = extend(res, [a:code]) + return l:res +endfun + +" 输入补全 +fun! InputComplete(firststart, base) + call InputLog(["complete", a:base, "(", col("."), ",", line("."), ")", getline(".")]) + if a:firststart + " locate the start of the word + let line = getline('.') + let start = col('.') - 1 + while start > 0 && line[start - 1] =~ '\a' + let start -= 1 + endwhile + return start + else + " find months matching with "a:base" + " retu + return InputTrans(a:base) + endif +endfun +set completefunc=InputComplete + +" autocmd InsertEnter * call ShySync("insert") +" autocmd InsertCharPre * call InputCheck() diff --git a/miss/go.mod b/miss/go.mod index 339a6890..6d5c03f9 100644 --- a/miss/go.mod +++ b/miss/go.mod @@ -2,7 +2,7 @@ module miss go 1.13 -require github.com/shylinux/icebergs v0.1.9 // indirect +require github.com/shylinux/icebergs v0.1.9 replace ( github.com/shylinux/icebergs => ../ diff --git a/miss/miss.md b/miss/miss.md index 3fc9dee0..97d59f1c 100644 --- a/miss/miss.md +++ b/miss/miss.md @@ -6,19 +6,19 @@ 命令化 组件化 个性化 `}} -{{table "图谱" ` +{{label "图谱" ` code wiki chat team mall linux nginx golang redis mysql zsh tmux docker git vim `}} -{{table "图谱" ` +{{label "图谱" ` ctx cli aaa web lex yac log gdb tcp nfs ssh mdb `}} -{{table "图谱" ` +{{label "图谱" ` spide serve space dream favor cache story share route proxy group label diff --git a/miss/src/main.go b/miss/src/main.go index afcf936f..6a6452db 100644 --- a/miss/src/main.go +++ b/miss/src/main.go @@ -7,6 +7,7 @@ import ( _ "github.com/shylinux/icebergs/misc" _ "github.com/shylinux/icebergs/misc/alpha" + _ "github.com/shylinux/icebergs/misc/input" _ "github.com/shylinux/icebergs/misc/lark" _ "github.com/shylinux/icebergs/misc/mp" _ "github.com/shylinux/icebergs/misc/pi"