1
0
forked from x/icebergs
This commit is contained in:
shylinux 2020-01-06 16:02:31 +08:00
parent 3ae2c76660
commit 59dc668894
2 changed files with 291 additions and 0 deletions

183
core/code/auto.sh Executable file
View File

@ -0,0 +1,183 @@
#!/bin/sh
if [ "${ctx_dev}" = "" ] || [ "${ctx_dev}" = "-" ]; then
ctx_dev="http://localhost:9095"
fi
ctx_url=$ctx_dev"/code/zsh"
ctx_get=${ctx_get:="wget -q"}
ctx_curl=${ctx_curl:="curl"}
ctx_head=${ctx_head:="Content-Type: application/json"}
ctx_sid=${ctx_sid:=""}
ctx_silent=${ctx_silent:=""}
ctx_err=${ctx_err:="/dev/null"}
ctx_welcome=${ctx_welcome:="^_^ Welcome to Context world ^_^"}
ctx_goodbye=${ctx_goodbye:="^_^ Goodbye to Context world ^_^"}
ShyRight() {
[ "$1" = "" ] && return 1
[ "$1" = "0" ] && return 1
[ "$1" = "false" ] && return 1
[ "$1" = "true" ] && return 0
return 0
}
ShyEcho() {
ShyRight "$ctx_silent" || echo "$@"
}
ShyLog() {
echo "$@" > $ctx_err
}
ShyWord() {
echo "$*"|sed -e 's/\ /%20/g' -e 's/\n/\\n/g'
}
ShyForm() {
while [ $# -gt 1 ]; do
echo -n "`ShyWord "$1"`=`ShyWord "$2"`"
shift 2 && [ $# -gt 1 ] && echo -n "&"
done
}
ShyGet() {
local data=`ShyForm "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"`
${ctx_get} "${ctx_url}?${data}"
}
ShyLine() {
echo "$*"|sed -e 's/\"/\\\"/g' -e 's/\n/\\n/g'
}
ShyJSON() {
[ $# -eq 1 ] && echo \"`ShyLine "$1"`\" && return
echo -n "{"
while [ $# -gt 1 ]; do
echo -n \"`ShyLine "$1"`\"\:\"`ShyLine "$2"`\"
shift 2 && [ $# -gt 1 ] && echo -n ","
done
echo -n "}"
}
ShyPost() {
if [ "$SHELL" = "/bin/zsh" ]; then
ShyJSON "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"|read data
else
local data=`ShyJSON "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"`
fi
${ctx_curl} -s "${ctx_url}" -H "${ctx_head}" -d "${data}"
}
ShyDownload() {
${ctx_curl} -s "${ctx_url}" -F "cmd=download" -F "arg=$1" -F "sid=$ctx_sid"
}
ShyUpload() {
${ctx_curl} -s "${ctx_url}" -F "cmd=upload" -F "upload=@$1" -F "sid=$ctx_sid"
}
ShyBench() {
${ctx_curl} -s "${ctx_dev}/publish/boot.sh" | sh -s installs context
}
ShySend() {
local TEMP=`mktemp /tmp/tmp.XXXXXX` && "$@" > $TEMP
ShyRight "$ctx_silent" || cat $TEMP
${ctx_curl} -s "${ctx_url}" -F "cmd=sync" -F "arg=$1" -F "args=$*" -F "sub=@$TEMP"\
-F "SHELL=${SHELL}" -F "pwd=${PWD}" -F "sid=${ctx_sid}"
}
ShySends() {
local cmd=$1 && shift
local arg=$2 && shift
local TEMP=`mktemp /tmp/tmp.XXXXXX` && echo "$@" > $TEMP
ShyRight "$ctx_silent" || cat $TEMP
${ctx_curl} -s "${ctx_url}" -F "cmd=$cmd" -F "arg=$arg" -F "sub=@$TEMP" \
-F "SHELL=${SHELL}" -F "pwd=${PWD}" -F "sid=${ctx_sid}"
}
ShyRun() {
ctx_silent=false ShySend "$@"
}
Shy() {
local ctx_res=`ShyPost cmd "$1" arg "$2"`
case "$ctx_res" in
"PS1");;
*) [ -n "${ctx_res}" ] && ShyPost cmd "$1" arg "$2" res `sh -c ${ctx_res}`
esac
}
ShyLogout() {
echo ${ctx_goodbye} && [ "$ctx_sid" != "" ] && Shy logout
}
ShyLogin() {
HOST=`hostname` ctx_sid=`ShyPost cmd login share "${ctx_share}" pid "$$" pane "${TMUX_PANE}" hostname "${HOST}" username "${USER}"`
[ "$ctx_begin" = "" ] && ctx_begin=`history|tail -n1|awk '{print $1}'` && echo "begin: ${ctx_begin}"
echo "sid: ${ctx_sid}"
}
ShyFavor() {
[ "$READLINE_LINE" != "" ] && set $READLINE_LINE && READLINE_LINE=""
[ "$1" != "" ] && ctx_tab=$1 && shift; [ "$1" != "" ] && ctx_note=$1 && shift
[ "$1" != "" ] && ctx_word=$1 || ctx_word=`history|tail -n1|head -n1`
ShyPost cmd favor arg "${ctx_word}" tab "${ctx_tab}" note "${ctx_note}"
}
ShyFavors() {
[ "$READLINE_LINE" != "" ] && set $READLINE_LINE && READLINE_LINE=""
ShyPost cmd favor tab "$1" limit "$2"
}
ShySync() {
[ "$ctx_sid" = "" ] && ShyLogin
case "$1" in
"history")
ctx_end=`history|tail -n1|awk '{print $1}'`
ctx_begin=${ctx_begin:=$ctx_end}
ctx_count=`expr $ctx_end - $ctx_begin`
ShyEcho "sync $ctx_begin-$ctx_end count $ctx_count to $ctx_dev"
history|tail -n $ctx_count |while read line; do
ShySends history sub "$line"
done
ctx_begin=$ctx_end
;;
ps) ShySend ps -ef ;;
*) ShySend "$@"
esac
}
ShySyncs() {
case "$1" in
"base")
ShySync df &>/dev/null
ShySync ps &>/dev/null
ShySync env &>/dev/null
ShySync free &>/dev/null
ShySync history
;;
*)
esac
}
ShyHelp() {
ShyPost cmd help arg "$@"
}
ShyInit() {
[ "$ctx_begin" = "" ] && ctx_begin=`history|tail -n1|awk '{print $1}'`
case "${SHELL##*/}" in
"zsh")
PROMPT='%![%*]%c$ '
;;
*)
PS1="\!-$$-\t[\u@\h]\W\$ "
PS1="\e[32m\!\e[0m-$$-\e[31m$SPY_OWNER\e[0m@\e[33m$SPY_ROLE\e[0m[\e[32m\t\e[0m]\W\$ "
PS1="\!-$$-\t[\u@\h]\W\$ "
PS1="\!-$$-\u@\h[\t]\W\$ "
;;
esac
if bind &>/dev/null; then
bind -x '"\C-G\C-R":ShySyncs base'
bind -x '"\C-G\C-F":ShyFavor'
bind -x '"\C-Gf":ShyFavor'
bind -x '"\C-GF":ShyFavors'
elif bindkey &>/dev/null; then
bindkey -s '\C-G\C-R' 'ShySyncs base\n'
setopt nosharehistory
fi
echo "url: ${ctx_url}"
echo "pid: $$"
echo "begin: ${ctx_begin}"
echo "share: ${ctx_share}"
echo "pane: $TMUX_PANE"
}
ShyInit && trap ShyLogout EXIT

108
core/code/auto.vim Normal file
View File

@ -0,0 +1,108 @@
let ctx_url = (len($ctx_dev) > 1? $ctx_dev: "http://127.0.0.1:9095") . "/code/vim"
if !exists("g:ctx_sid") | let ctx_sid = "" | end
fun! ShySend(arg)
if has_key(a:arg, "sub") && a:arg["sub"] != ""
let temp = tempname()
call writefile(split(a:arg["sub"], "\n"), temp, "b")
let a:arg["sub"] = "@" . temp
endif
let a:arg["buf"] = bufname("%")
let a:arg["pwd"] = getcwd()
let a:arg["sid"] = g:ctx_sid
let args = ""
for k in keys(a:arg)
let args = args . " -F '" . k . "=" . a:arg[k] . "' "
endfor
return system("curl -s " . g:ctx_url . args . " 2>/dev/null")
endfun
fun! ShyLogout()
if g:ctx_sid == "" | return | endif
call ShySend({"cmd": "logout"})
endfun
fun! ShyLogin()
let g:ctx_sid = ShySend({"cmd": "login", "share": $ctx_share, "pid": getpid(), "pane": $TMUX_PANE, "hostname": hostname(), "username": $USER})
endfun
fun! ShyFavor()
if !exists("g:favor_tab") | let g:favor_tab = "" | endif
if !exists("g:favor_note") | let g:favor_note = "" | endif
let g:favor_tab = input("tab: ", g:favor_tab)
let g:favor_note = input("note: ", g:favor_note)
call ShySend({"cmd": "favor", "tab": g:favor_tab, "note": g:favor_note, "arg": getline("."), "line": getpos(".")[1], "col": getpos(".")[2]})
endfun
fun! ShyFavors()
let res = split(ShySend({"cmd": "favor", "tab": input("tab: ")}), "\n")
let page = "" | let note = ""
for i in range(0, len(res)-1, 2)
if res[i] != page
if note != "" | lexpr note | lopen | let note = "" | endif
execute exists(":TabooOpen")? "TabooOpen " . res[i]: "tabnew"
endif
let page = res[i] | let note .= res[i+1] . "\n"
endfor
if note != "" | lexpr note | lopen | let note = "" | endif
endfun
fun! ShySync(target)
if bufname("%") == "ControlP" | return | end
if a:target == "read" || a:target == "write"
call ShySend({"cmd": a:target, "arg": expand("<afile>")})
elseif a:target == "exec"
call ShySend({"cmd": a:target, "sub": getcmdline()})
elseif a:target == "insert"
call ShySend({"cmd": a:target, "sub": getreg("."), "row": line("."), "col": col(".")})
else
let cmd = {"bufs": "buffers", "regs": "registers", "marks": "marks", "tags": "tags", "fixs": "clist"}
call ShySend({"cmd": "sync", "arg": a:target, "sub": execute(cmd[a:target])})
endif
endfun
fun! ShyCheck(target)
if a:target == "cache"
call ShySync("bufs")
call ShySync("regs")
call ShySync("marks")
call ShySync("tags")
elseif a:target == "fixs"
let l = len(getqflist())
if l > 0
execute "copen " . (l > 10? 10: l + 1)
call ShySync("fixs")
else
cclose
end
end
endfun
fun! ShyTask()
call ShySend({"cmd": "tasklet", "arg": input("target: "), "sub": input("detail: ")})
endfun
fun! ShyGrep(word)
if !exists("g:grep_dir") | let g:grep_dir = "./" | endif
let g:grep_dir = input("dir: ", g:grep_dir, "file")
execute "grep -rn --exclude tags --exclude '*.tags' " . a:word . " " . g:grep_dir
endfun
fun! ShyTag(word)
execute "tag " . a:word
endfun
fun! ShyHelp()
echo ShySend({"cmd": "help"})
endfun
call ShyLogin()
autocmd VimLeave * call ShyLogout()
autocmd BufReadPost * call ShySync("bufs")
autocmd BufReadPost * call ShySync("read")
autocmd BufWritePre * call ShySync("write")
autocmd CmdlineLeave * call ShySync("exec")
autocmd QuickFixCmdPost * call ShyCheck("fixs")
autocmd InsertLeave * call ShySync("insert")
nnoremap <C-G><C-G> :call ShyGrep(expand("<cword>"))<CR>
nnoremap <C-G><C-R> :call ShyCheck("cache")<CR>
nnoremap <C-G><C-F> :call ShyFavor()<CR>
nnoremap <C-G>f :call ShyFavors()<CR>
nnoremap <C-G><C-T> :call ShyTask()<CR>