From 42af6720cb7511bc2c54d4e18da7a2f3ad7e68cd Mon Sep 17 00:00:00 2001 From: shaoying Date: Wed, 13 Nov 2019 21:21:11 +0800 Subject: [PATCH] add zsh.favor --- etc/conf/auto.sh | 193 +++++++++++++++++++++----------------- etc/conf/tmux.conf | 1 + src/contexts/ctx/ctx.go | 3 + src/contexts/web/web.go | 10 +- src/examples/code/code.go | 48 ++++++++-- src/plugin/zsh/index.shy | 18 ++++ 6 files changed, 171 insertions(+), 102 deletions(-) diff --git a/etc/conf/auto.sh b/etc/conf/auto.sh index 0e43d902..1dd38cf1 100644 --- a/etc/conf/auto.sh +++ b/etc/conf/auto.sh @@ -5,12 +5,30 @@ if [ "${ctx_dev}" = "" ] || [ "${ctx_dev}" = "-" ]; then fi ctx_url=$ctx_dev"/code/zsh" +ctx_get=${ctx_get:="wget -q"} +ctx_curl=${ctx_curl:="curl -s"} ctx_head=${ctx_head:="Content-Type: application/json"} ctx_sync=${ctx_sync:=""} ctx_sid=${ctx_sid:=""} +ctx_silent=${ctx_silent:=""} ctx_welcome=${ctx_welcome:="^_^ Welcome to Context world ^_^"} ctx_goodbye=${ctx_goodbye:="^_^ Goodbye to Context world ^_^"} +# ctx_ps1=${ctx_ps1:="\!-$$-\t[\u@\h]\W\$ "} +ctx_ps1=${ctx_ps1:="\!-$$-[\t]\W\$ "} +ctx_bind=${ctx_bind:="bind -x"} +ctx_null=${ctx_null:="false"} + +ShyRight() { + [ "$1" = "" ] && return 1 + [ "$1" = "0" ] && return 1 + [ "$1" = "false" ] && return 1 + [ "$1" = "true" ] && return 0 + return 0 +} +ShyEcho() { + ShyRight "$ctx_silent" || echo "$@" +} ShyWord() { echo "$*"|sed -e 's/\ /%20/g' -e 's/\n/\\n/g' @@ -23,7 +41,7 @@ ShyForm() { } ShyGet() { local data=`ShyForm "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"` - wget -q "${ctx_url}?${data}" + ${ctx_get} "${ctx_url}?${data}" } ShyLine() { echo "$*"|sed -e 's/\"/\\\"/g' -e 's/\n/\\n/g' @@ -43,82 +61,25 @@ ShyPost() { else local data=`ShyJSON "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"` fi - curl -s "${ctx_url}" -H "${ctx_head}" -d "${data}" + ${ctx_curl} "${ctx_url}" -H "${ctx_head}" -d "${data}" } ShyDownload() { - curl "${ctx_dev}/download/$1" + ${ctx_curl} "${ctx_dev}/download/$1" } ShyUpdate() { - curl "${ctx_dev}/publish/$1" > $1 + ${ctx_curl} "${ctx_dev}/publish/$1" > $1 } ShyUpload() { - curl "${ctx_dev}/upload" -F "upload=@$1" + ${ctx_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" +ShySend() { + local TEMP=`mktemp /tmp/tmp.XXXXXX` && "$@" > $TEMP + ShyRight "$ctx_silent" || cat $TEMP + ${ctx_curl} "${ctx_url}" -F "cmd=sync" -F "arg=$1" -F "args=$*" -F "sub=@$TEMP"\ + -F "SHELL=${SHELL}" -F "pwd=${PWD}" -F "sid=${ctx_sid}" } -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` - echo - echo "upload history $ctx_begin-$ctx_end count $ctx_tail to $ctx_dev" - history|tail -n $ctx_tail |while read line; do - line=`ShyLine $line` - ShyPost arg "$line" cmd historys FORMAT "$HISTTIMEFORMAT" - echo $line - done - ctx_begin=$ctx_end - ;; - "history") tail -n0 -f $HISTFILE | while true; do read line - line=`ShyLine $line` - ShyPost arg "$line" cmd history FORMAT "$HISTTIMEFORMAT" - echo $line - done;; - "input") - ShyGet arg "$READLINE_LINE" cmd "input" SHELL "$SHELL" - ;; - esac -} - -ShySyncs() { - case "$1" in - "base") - ShySync df - ShySync env - ShySync free - ShySync historys - ;; - *) - esac - -} -ShyHistory() { - case "$SHELL" in - "/bin/zsh") - ShySync history &>/dev/null & - ctx_sync=$! - ;; - *) ;; - esac -} -ShyRecord() { - script $1 +ShyRun() { + ctx_silent=false ShySend "$@" } Shy() { local ctx_res=`ShyPost cmd "$1" arg "$2"` @@ -129,32 +90,90 @@ Shy() { } ShyLogout() { - echo ${ctx_goodbye} - sleep 1 - Shy logout + echo ${ctx_goodbye} && sleep 1 && Shy logout } ShyLogin() { HOST=`hostname` ctx_sid=`ShyPost cmd login pid "$$" pane "${TMUX_PANE}" hostname "${HOST}" username "${USER}"` + echo "sid: ${ctx_sid:0:6}" +} +ShyFavor() { + [ "$1" != "" ] && ctx_tab=$1 + [ "$2" != "" ] && ctx_note=$2 + ShyPost cmd favor arg "`history|tail -n2|head -n1`" tab "${ctx_tab}" note "${ctx_note}" +} +ShySync() { + [ "$ctx_sid" = "" ] && ShyLogin + + case "$1" in + "historys") tail -n0 -f $HISTFILE | while true; do read line + line=`ShyLine $line` + ShyPost arg "$line" cmd history FORMAT "$HISTTIMEFORMAT" + echo $line + done + ;; + "history") + ctx_end=`history|tail -n1|awk '{print $1}'` + ctx_tail=`expr $ctx_end - $ctx_begin` + ShyEcho "sync $ctx_begin-$ctx_end count $ctx_tail to $ctx_dev" + history|tail -n $ctx_tail |while read line; do + line=`ShyLine $line` + ShyPost arg "$line" cmd historys FORMAT "$HISTTIMEFORMAT" + ShyEcho $line + done + ctx_begin=$ctx_end + ;; + *) ShySend "$@" + esac +} +ShySyncs() { + case "$1" in + "base") + ShySync df + ShySync env + ShySync free + ShySync history + ;; + *) + esac +} +ShyHistory() { + case "$SHELL" in + "/bin/zsh") + ShySync df + ShySync env + ShySync free + ShySync historys &>/dev/null & + ctx_sync=$! + ;; + *) ;; + esac +} +ShyRecord() { + script $1 +} +ShyHelp() { + ShyPost cmd help arg "$@" +} +ShyInit() { + [ "$ctx_begin" == "" ] && ctx_begin=`history|tail -n1|awk '{print $1}'` + + case "$SHELL" in + "/bin/zsh") + ctx_bind=${ctx_null} + ;; + *) + ;; + esac + + PS1=${ctx_ps1} + ${ctx_bind} '"\C-t\C-t":ShySyncs base' + echo ${ctx_welcome} echo "url: ${ctx_url}" - echo "sid: ${ctx_sid:0:6}" echo "pid: $$" echo "pane: $TMUX_PANE" echo "begin: ${ctx_begin}" } -ShyInit() { - ctx_begin=`cat $HISTFILE|wc -l` - case "$SHELL" in - "/bin/zsh");; - *) - ctx_begin=`history|tail -n1|awk '{print $1}'` - bind -x '"\C-t":ShySyncs base' - # bind -x '"\C-gl":ShySync input' - PS1="\!-\t[\u@\h]\W\$ " - # PS1="\!-\t\$ " - ;; - esac -} -ShyInit && ShyLogin && trap ShyLogout EXIT +ShyInit && trap ShyLogout EXIT diff --git a/etc/conf/tmux.conf b/etc/conf/tmux.conf index fd6ff00e..c692da3b 100644 --- a/etc/conf/tmux.conf +++ b/etc/conf/tmux.conf @@ -89,4 +89,5 @@ bind -t vi-edit C-f cursor-right bind -t vi-edit C-j enter # }}} +bind C-r send-keys "export ctx_dev=$ctx_self\ncurl -s \$ctx_dev/publish/auto.sh >auto.sh\nsource auto.sh\n" source-file ~/.tmux_local diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index ba7a3686..51016ec5 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -90,12 +90,15 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, "index": []interface{}{ "^_^ 欢迎来到云境世界 ^_^", "^_^ Welcome to Context world ^_^", + "", "V2.1: Miss You Forever", "Date: 2019.10.29 13:14:21", "From: 2017.11.01 00:08:21", + "", "Meet: shylinuxc@gmail.com", "More: https://shylinux.com", "More: https://github.com/shylinux/context", + "", }, }, Help: "帮助"}, "time": &Config{Name: "timer", Value: map[string]interface{}{ diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 64d66772..54e699a8 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -188,12 +188,7 @@ func (web *WEB) HandleCmd(m *ctx.Message, key string, cmd *ctx.Command) { } // 请求参数 - if r.ParseMultipartForm(int64(msg.Confi("serve", "form_size"))); r.MultipartForm != nil && len(r.MultipartForm.Value) > 0 { - for k, v := range r.MultipartForm.Value { - msg.Log("info", "%s: %v", k, v) - msg.Add("option", k, v) - } - } + r.ParseMultipartForm(int64(msg.Confi("serve", "form_size"))) if r.ParseForm(); len(r.PostForm) > 0 { for k, v := range r.PostForm { msg.Log("info", "%s: %v", k, v) @@ -978,7 +973,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } buf := bytes.NewBuffer(make([]byte, 0, 1024)) - fmt.Fprintf(buf, "create_time: %s\n", m.Time()) + fmt.Fprintf(buf, "create_time: %s\n", m.Time("2006-01-02 15:04")) fmt.Fprintf(buf, "create_user: %s\n", m.Option("username")) fmt.Fprintf(buf, "name: %s\n", h.Filename) fmt.Fprintf(buf, "type: %s\n", kind) @@ -1005,6 +1000,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } else { m.Echo("code: %s\n", code) m.Echo("hash: %s\n", name) + m.Echo("time: %s\n", m.Time("2006-01-02 15:04")) m.Echo("type: %s\n", kind) m.Echo("size: %s\n", kit.FmtSize(n)) } diff --git a/src/examples/code/code.go b/src/examples/code/code.go index a8f0ca92..eb1b9fea 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -135,6 +135,8 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", m.Log("info", "%v %v %v %v", cmd, m.Option("cmd"), m.Option("arg"), m.Option("sub")) switch m.Option("cmd") { + case "help": + m.Echo(strings.Join(kit.Trans(m.Confv("help", "index")), "\n")) case "login": name := kit.Hashs(m.Option("pid"), m.Option("hostname"), m.Option("username")) m.Conf(cmd, []string{"terminal", "hash", name}, map[string]interface{}{ @@ -173,6 +175,22 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "cmd": m.Option("arg"), "pwd": m.Option("pwd"), }) + case "favor": + m.Option("river", m.Conf(cmd, []string{"terminal", "hash", m.Option("sid"), "river"})) + table := m.Conf(cmd, []string{"terminal", "hash", m.Option("sid"), "table"}) + m.Log("info", "favor: %v table: %v", m.Option("river"), table) + + if m.Options("tab") { + m.Cmd("ssh.data", "insert", table, "tab", m.Option("tab"), + "note", m.Option("note"), "word", m.Option("arg"), + ) + return + } + m.Cmd("ssh.data", "show", table).Table(func(index int, value map[string]string) { + m.Echo("%v:%v\n%v\n", value["tab"], value["note"], value["word"]) + }).Set("append") + return + case "sync": m.Conf(cmd, []string{m.Option("arg"), "hash", m.Option("sid")}) switch m.Option("arg") { @@ -198,9 +216,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", }) }) 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"], @@ -209,7 +225,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", 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 { + if index == 1 { m.Confv(cmd, []string{m.Option("arg"), "list", "-2"}, map[string]interface{}{ "time": m.Time(), "sid": m.Option("sid"), @@ -251,6 +267,21 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", } fallthrough case "terminal": + if len(arg) > 2 && arg[2] != "" { + m.Conf(cmd, []string{arg[0], "hash", arg[1], "table"}, arg[2]) + m.Conf(cmd, []string{arg[0], "hash", arg[1], "river"}, m.Option("river")) + } + if len(arg) > 1 && arg[1] != "" { + m.Option("table.format", "table") + m.Confm(cmd, []string{arg[0], "hash", arg[1]}, func(key string, value string) { + m.Push(key, value) + }) + m.Sort("key") + break + } + if len(arg) > 3 { + arg[3] = strings.Join(arg[3:], " ") + } fields := strings.Split(kit.Select(m.Conf(cmd, arg[0]+".meta.fields"), arg, 1), " ") m.Confm(cmd, arg[0]+".hash", func(key string, value map[string]interface{}) { m.Push(fields, kit.Shortm(value, "times", "files", "sids")) @@ -272,12 +303,11 @@ 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("index") { - m.Sort("index", "int_r") - } else if m.Appends("time") { + if m.Appends("time") || m.Appends("times") { m.Sort("time", "time_r") - } else if m.Appends("times") { - m.Sort("times", "time_r") + } + if m.Appends("index") { + // m.Sort("index", "int_r") } m.Table() case "env", "ps", "df": @@ -814,6 +844,8 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", m.Log("info", "%v %v %v %v", cmd, m.Option("cmd"), m.Option("arg"), m.Option("sub")) switch m.Option("cmd") { + case "help": + m.Echo(strings.Join(kit.Trans(m.Confv("help", "index")), "\n")) case "login": name := kit.Hashs(m.Option("pid"), m.Option("hostname"), m.Option("username")) m.Conf(cmd, []string{"editor", "hash", name}, map[string]interface{}{ diff --git a/src/plugin/zsh/index.shy b/src/plugin/zsh/index.shy index f8704f78..210bc266 100644 --- a/src/plugin/zsh/index.shy +++ b/src/plugin/zsh/index.shy @@ -1,6 +1,24 @@ + +fun taps "便签" private \ + text "tap" name table imports plugin_vim_table action auto \ + text "" name index imports plugin_tap_id view tiny action auto \ + feature detail "修改" "复制" "下载" \ + exports vim_table table "" tap_id id "" vim_file file "" vim_line line "" vim_word word "" \ + button "记录" action auto + + if $2 == "modify" + copy skip ssh.data update $table $1 $3 $4 + else + copy ssh.data show + end +end + kit terminal "终端" private "web.code.zsh" "" terminal \ + text "" name sid imports plugin_zsh_sid action auto \ + text "" name table imports plugin_zsh_table \ text "times sids status pid pane hostname" name fields \ feature detail "prune" "复制" "下载" \ + exports zsh_sid sid \ button "查看" action auto kit history "命令" private "web.code.zsh" "" history \