mirror of
https://shylinux.com/x/icebergs
synced 2025-04-25 09:08:06 +08:00
add alpha&input
This commit is contained in:
parent
92ad44769e
commit
8f8a590d7a
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
*.swp
|
||||
*.swo
|
||||
|
@ -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()
|
||||
|
112
misc/alpha/alpha.go
Normal file
112
misc/alpha/alpha.go
Normal file
@ -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) }
|
1
misc/alpha/alpha.md
Normal file
1
misc/alpha/alpha.md
Normal file
@ -0,0 +1 @@
|
||||
# {{title "alpha"}}
|
119
misc/input/input.go
Normal file
119
misc/input/input.go
Normal file
@ -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) }
|
1
misc/input/input.md
Normal file
1
misc/input/input.md
Normal file
@ -0,0 +1 @@
|
||||
# {{title "input"}}
|
45
misc/input/input.vim
Normal file
45
misc/input/input.vim
Normal file
@ -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()
|
@ -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 => ../
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user