From 9c230bda83f3d32360cfbde286b68fccc85ab9ab Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 12 Nov 2019 00:55:25 +0800 Subject: [PATCH] add zsh.sync --- etc/conf/auto.sh | 38 ++++++++++-- src/contexts/ctx/type.go | 9 ++- src/examples/code/code.go | 118 ++++++++++++++++++++++++++++++++++---- src/plugin/zsh/index.shy | 26 ++++++++- 4 files changed, 173 insertions(+), 18 deletions(-) diff --git a/etc/conf/auto.sh b/etc/conf/auto.sh index bac87580..b092cadb 100644 --- a/etc/conf/auto.sh +++ b/etc/conf/auto.sh @@ -49,13 +49,30 @@ ShyDownload() { curl "${ctx_dev}/download/$1" } ShyUpdate() { - curl "${ctx_dev}/publish/$1" + curl "${ctx_dev}/publish/$1" > $1 } ShyUpload() { curl "${ctx_dev}/upload" -F "upload=@$1" } +ShyFuck() { + local TEMP=`mktemp /tmp/tmp.XXXXXX` && $@ > $TEMP + local data=`ShyForm cmd sync arg $1 SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"` + curl "${ctx_url}?$data" -F "sub=@$TEMP" +} ShySync() { case "$1" in + "df") + ShyFuck df + ;; + "ps") + ShyFuck ps + ;; + "env") + ShyFuck env + ;; + "free") + ShyFuck free + ;; "historys") ctx_end=`history|tail -n1|awk '{print $1}'` ctx_tail=`expr $ctx_end - $ctx_begin` @@ -79,6 +96,18 @@ ShySync() { esac } +ShySyncs() { + case "$1" in + "base") + ShySync df + ShySync env + ShySync free + ShySync historys + ;; + *) + esac + +} ShyHistory() { case "$SHELL" in "/bin/zsh") @@ -101,6 +130,7 @@ Shy() { ShyLogout() { echo ${ctx_goodbye} + sleep 1 Shy logout } ShyLogin() { @@ -116,12 +146,12 @@ ShyInit() { "/bin/zsh");; *) ctx_begin=`history|tail -n1|awk '{print $1}'` - bind -x '"\C-t":ShySync historys' + bind -x '"\C-t":ShySyncs base' # bind -x '"\C-gl":ShySync input' - PS1="\!-\t[\u@\h]\W\$ " + # PS1="\!-\t[\u@\h]\W\$ " + PS1="\!-\t\$ " ;; esac - } ShyInit && ShyLogin && trap ShyLogout EXIT diff --git a/src/contexts/ctx/type.go b/src/contexts/ctx/type.go index 9e52f15c..543f57dc 100644 --- a/src/contexts/ctx/type.go +++ b/src/contexts/ctx/type.go @@ -349,12 +349,15 @@ func (m *Message) Split(str string, arg ...string) *Message { } for _, l := range lines { + if len(l) == 0 { + continue + } if len(pos) > 0 { for i, v := range pos { - if len(l) < v { - m.Add("append", heads[i], "") - } else if i == len(pos)-1 { + if i == len(pos)-1 { m.Add("append", heads[i], strings.TrimSpace(l[v:])) + } else if len(l) < v || i+1 >= len(pos) || len(l) < pos[i+1] { + m.Add("append", heads[i], "") } else { m.Add("append", heads[i], strings.TrimSpace(l[v:pos[i+1]])) } diff --git a/src/examples/code/code.go b/src/examples/code/code.go index 2c4965c4..0f19252d 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -4,6 +4,8 @@ import ( "contexts/ctx" "contexts/web" "fmt" + "io/ioutil" + "net/http" "os" "os/exec" "path" @@ -59,6 +61,21 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "limit": "30", "least": "10", }}, + "free": map[string]interface{}{"meta": map[string]interface{}{ + "fields": "time sid type total used free shared buffer available", + "store": "var/tmp/vim/opens.csv", + "limit": "30", + "least": "10", + }}, + "df": map[string]interface{}{"meta": map[string]interface{}{ + "fields": "fs size used rest per pos", + }}, + "ps": map[string]interface{}{"meta": map[string]interface{}{ + "fields": "PID TIME COMMAND", + }}, + "env": map[string]interface{}{"meta": map[string]interface{}{ + "fields": "sid name value", + }}, }}, "vim": {Name: "vim", Help: "编辑器", Value: map[string]interface{}{ "editor": map[string]interface{}{"meta": map[string]interface{}{ @@ -109,7 +126,14 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", Commands: map[string]*ctx.Command{ "/zsh": {Name: "/zsh sid pwd cmd arg", Help: "终端", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { cmd = strings.TrimPrefix(cmd, "/") + if f, _, e := m.Optionv("request").(*http.Request).FormFile("sub"); e == nil { + defer f.Close() + if b, e := ioutil.ReadAll(f); e == nil { + m.Option("sub", string(b)) + } + } m.Log("info", "%v %v %v %v", cmd, m.Option("cmd"), m.Option("arg"), m.Option("sub")) + switch m.Option("cmd") { case "login": name := kit.Hashs(m.Option("pid"), m.Option("hostname"), m.Option("username")) @@ -149,7 +173,58 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "cmd": m.Option("arg"), "pwd": m.Option("pwd"), }) - return + case "sync": + m.Conf(cmd, []string{m.Option("arg"), "hash", m.Option("sid")}) + switch m.Option("arg") { + case "df": + m.Split(m.Option("sub"), " ", "6", "fs size used rest per pos").Table(func(index int, value map[string]string) { + if index > 0 { + m.Confv(cmd, []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + "fs": value["fs"], + "size": value["size"], + "used": value["used"], + "rest": value["rest"], + "per": value["per"], + "pos": value["pos"], + }) + } + }) + case "ps": + m.Split(m.Option("sub"), " ").Table(func(index int, value map[string]string) { + m.Confv(cmd, []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + "PID": value["PID"], + "TIME": value["TIME"], + "COMMAND": value["COMMAND"], + }) + }) + case "env": + m.Log("fuck", "what %v fuck", m.Option("sub")) + m.Split(strings.TrimPrefix(m.Option("sub"), "\n"), "=", "2", "name value").Table(func(index int, value map[string]string) { + m.Log("fuck", "what %v fuck", value) + m.Confv(cmd, []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + "name": value["name"], + "value": value["value"], + }) + }) + case "free": + sub := strings.Replace(m.Option("sub"), " ", "type", 1) + m.Split(sub, " ", "7", "type total used free shared buffer available").Table(func(index int, value map[string]string) { + if index > 0 { + m.Confv(cmd, []string{m.Option("arg"), "list", "-2"}, map[string]interface{}{ + "time": m.Time(), + "sid": m.Option("sid"), + "type": value["type"], + "total": value["total"], + "used": value["used"], + "free": value["free"], + "shared": value["shared"], + "buffer": value["buffer"], + "available": value["available"], + }) + } + }) + } + m.Set("append").Set("result") } return }}, @@ -197,13 +272,35 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", m.Grows(cmd, arg[0], func(meta map[string]interface{}, index int, value map[string]interface{}) { m.Push(fields, kit.Shortm(value, "times", "files", "sids")) }) - if m.Appends("times") { + if m.Appends("index") { + m.Sort("index", "int_r") + } else if m.Appends("time") { + m.Sort("time", "time_r") + } else if m.Appends("times") { m.Sort("times", "time_r") } - if m.Appends("time") { - m.Sort("time", "time_r") - } m.Table() + case "env", "ps", "df": + if len(arg) > 3 { + arg[3] = strings.Join(arg[3:], " ") + } + fields := strings.Split(kit.Select(m.Conf(cmd, arg[0]+".meta.fields"), arg, 3), " ") + m.Confm(cmd, []string{arg[0], "hash"}, func(key string, index int, value map[string]interface{}) { + if value["sid"] = key; len(arg) == 1 || arg[1] == "" || strings.HasPrefix(kit.Format(value[arg[1]]), arg[2]) { + m.Push(fields, kit.Shortm(value, "times", "files", "sids")) + } + }) + m.Table() + case "free": + if len(arg) > 3 { + arg[3] = strings.Join(arg[3:], " ") + } + m.Option("cache.limit", kit.Select("10", arg, 1)) + m.Option("cache.offset", kit.Select("0", arg, 2)) + fields := strings.Split(kit.Select(m.Conf(cmd, arg[0]+".meta.fields"), arg, 3), " ") + m.Grows(cmd, arg[0], func(meta map[string]interface{}, index int, value map[string]interface{}) { + m.Push(fields, kit.Shortm(value, "times", "files", "sids")) + }) case "init": m.Cmd("cli.system", m.Confv("package", "upadte")) @@ -749,7 +846,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", switch m.Option("arg") { case "bufs": m.Split(strings.TrimSpace(m.Option("sub")), " ", "5", "id tag name some line").Table(func(index int, value map[string]string) { - m.Conf("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + m.Confv("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ "buf": value["id"], "tag": value["tag"], "file": strings.TrimSuffix(strings.TrimPrefix(value["name"], "\""), "\""), @@ -758,23 +855,24 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", }) case "regs": m.Split(strings.TrimPrefix(m.Option("sub"), "\n--- Registers ---\n"), " ", "2", "name word").Table(func(index int, value map[string]string) { - m.Conf("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + m.Confv("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ "word": strings.Replace(strings.Replace(value["word"], "^I", "\t", -1), "^J", "\n", -1), "reg": strings.TrimPrefix(value["name"], "\""), }) }) case "marks": m.Split(strings.TrimPrefix(m.Option("sub"), "\n"), " ", "4").Table(func(index int, value map[string]string) { - m.Conf("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + m.Confv("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ "mark": value["mark"], "line": value["line"], "col": value["col"], "file": value["file/text"], }) + hello }) case "tags": m.Split(strings.TrimPrefix(m.Option("sub"), "\n"), " ", "6").Table(func(index int, value map[string]string) { - m.Conf("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + m.Confv("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ "tag": value["tag"], "line": value["line"], "file": value["in file/text"], @@ -786,7 +884,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", } m.Split(strings.TrimPrefix(m.Option("sub"), "\n"), " ", "3", "id file word").Table(func(index int, value map[string]string) { vs := strings.Split(kit.Format(value["file"]), ":") - m.Conf("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ + m.Confv("vim", []string{m.Option("arg"), "hash", m.Option("sid"), "-2"}, map[string]interface{}{ "fix": value["id"], "file": vs[0], "line": vs[1], diff --git a/src/plugin/zsh/index.shy b/src/plugin/zsh/index.shy index 13f12d1e..f8704f78 100644 --- a/src/plugin/zsh/index.shy +++ b/src/plugin/zsh/index.shy @@ -6,6 +6,30 @@ kit terminal "终端" private "web.code.zsh" "" terminal \ kit history "命令" private "web.code.zsh" "" history \ text "10" name limit view tiny \ text "0" name offset view tiny \ - text "times sids cmd pwd" name fields \ + text "times sids index cmd pwd" name fields \ + button "查看" action auto + +kit df "磁盘" private "web.code.zsh" "" df \ + text "fs" name limit view tiny \ + text "" name offset view tiny \ + text "sids fs size used rest per pos" name fields \ + button "查看" action auto + +kit ps "进程" private "web.code.zsh" "" ps \ + text "COMMAND" name limit view tiny \ + text "" name offset view tiny \ + text "sids PID TIME COMMAND" name fields \ + button "查看" action auto + +kit env "环境" private "web.code.zsh" "" env \ + text "name" name limit view tiny \ + text "" name offset view tiny \ + text "sids name value" name fields \ + button "查看" action auto + +kit free "内存" private "web.code.zsh" "" free \ + text "10" name limit view tiny \ + text "0" name offset view tiny \ + text "times sids type total used free" name fields \ button "查看" action auto