From a7147aa6ca8b63a83cab06f75894bfc28b6a775b Mon Sep 17 00:00:00 2001 From: shaoying Date: Wed, 22 Apr 2020 03:26:58 +0800 Subject: [PATCH] add ish --- etc/conf/bashrc | 4 ++ etc/conf/ishrc | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ etc/conf/sh.vim | 6 +++ etc/conf/zshrc | 14 +----- 4 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 etc/conf/bashrc create mode 100644 etc/conf/ishrc create mode 100644 etc/conf/sh.vim diff --git a/etc/conf/bashrc b/etc/conf/bashrc new file mode 100644 index 00000000..7f30a03a --- /dev/null +++ b/etc/conf/bashrc @@ -0,0 +1,4 @@ +#!/bin/sh + +[ -f ~/.ishrc ] && source ~/.ishrc + diff --git a/etc/conf/ishrc b/etc/conf/ishrc new file mode 100644 index 00000000..6d1f0582 --- /dev/null +++ b/etc/conf/ishrc @@ -0,0 +1,120 @@ +#!/bin/sh + +ISH_LOG=${ISH_LOG:="/dev/null"} +ISH_PATH=${ISH_PATH:="$PWD/.ish/pluged"} +ISH_ROOT=${ISH_ROOT:="$HOME/.ish/pluged"} +ISH_HUB=${ISH_HUB:="github.com"} +ISH_FTP=${ISH_FTP:="https"} +ISH_INIT=${ISH_INIT:="init.sh"} +ISH_EXIT=${ISH_EXIT:="exit.sh"} +ISH_ORDER=${ISH_ORDER:=0} + +ISH_LOG=/dev/stderr +ish_log() { echo $* >$ISH_LOG; } + +require() { + # 解析参数 + local name=$1 init=$ISH_INIT + [ -z "$1" ] && echo $ISH_SCRIPT && return || shift + [ -z "$1" ] || init="$@" + ish_log $0 $name $init + + # 下载脚本 + local p="${name%%/*}" && p=${p%:} && case "$p" in + ${ISH_FTP}) + local pp=$ISH_PATH/$(_name $name) + if ! [ -f $pp/$init ]; then + mkdir -p $pp && wget $name/$init -O $pp/$init + fi + name=$(_name $name) + ;; + $ISH_HUB) [ -d "$ISH_PATH/$name/.git" ] || git clone https://$name $ISH_PATH/$name;; + esac + + # 加载脚本 + for p in $ISH_PATH $ISH_ROOT; do + [ -d "$p/$name" ] && for i in $init; do + ISH_MODULE=$(_name ish_${name}_) ISH_SCRIPT=$(_name ish_${name}__${i%%.*}) _load $p/$name/$i + done && break + done +} +module() { # 模块接口 + case "$1" in + get) _conf get $ISH_MODULE "$2" "$3";; + set) _conf set $ISH_MODULE "$2" "$3";; + *) + local mod=$1 fun=$2 && shift 2 + ISH_MODULE=ish_$(_name ${mod}_) _conf run ish_$(_name ${mod}_${fun}) "$@" + esac +} +script() { # 脚本接口 + case "$1" in + get) _conf get $ISH_SCRIPT "$2" "$3";; + set) _conf set $ISH_SCRIPT "$2" "$3";; + *) + local mod=$1 file=$2 fun=$3 && shift 3 + local name=ish_$(_name ${mod}__${file%%.*}_${fun}) + declare -f $name >/dev/null || require ${mod} ${file}.sh + ISH_MODULE=ish_$(_name ${mod}_) ISH_SCRIPT=ish_$(_name ${mod}__${file%%.*}) _conf run $name "$@" + esac +} +object() { # 对象接口 + case "$1" in + get) _conf get $ISH_OBJECT "$2" "$3";; + set) _conf set $ISH_OBJECT "$2" "$3";; + new) let ISH_ORDER=$ISH_ORDER+1 && echo ish_object_$ISH_ORDER;; + *) local fun=$1 && shift 1 && _conf run ${ISH_OBJECT}_${fun} "$@" + esac +} + +_name() { + local name="$*" + name=${name//\/\//\/} + name=${name//:/} + name=${name//./_} && name=${name//\//_} && name=${name//\ /_} + echo $name +} +_conf() { + case "$1" in + get) eval "[ -z \"\$${2}_$3\" ] && ${2}_$3=\"$4\"; echo \$${2}_$3";; + set) eval "${2}_$3=\"$4\"";; + run) local func=$2 && shift 2 + ish_log "run" $func + $func "$@" + esac +} +_load() { + [ -f "$1" ] || return + ish_log "source" "\e[32m$*\e[0m" + local back=$PWD pre=$1 + cd ${pre%/*} && ish_log "pwd" $PWD + source "$@" >/dev/null + cd $back +} +_plug() { + for p in $ISH_ROOT $ISH_PATH; do + local what=$p + [ -d "$what" ] && for hub in $what/*; do + case "${hub##*/}" in + $ISH_HUB) + for repos in $hub/*/*; do + require ${repos#$what/} $1 + done;; + *) require ${hub#$what/} $1 + esac + done + done +} +_init() { + ISH_OBJECT=$(object new) + _plug $ISH_INIT +} +_exit() { + ISH_OBJECT=$(object new) + _plug $ISH_EXIT +} +_init && trap _exit EXIT +ish() { + local key=$1 && shift && local mod=${key%/*} file=${key##*/} + file=${file//./\/} && script ${mod} ${file%/*} ${file##*/} $@ +} diff --git a/etc/conf/sh.vim b/etc/conf/sh.vim new file mode 100644 index 00000000..0f3cca0b --- /dev/null +++ b/etc/conf/sh.vim @@ -0,0 +1,6 @@ +highlight ishKey ctermfg=yellow +syntax match ishKey "^require" +syntax match ishKey "^module" +syntax match ishKey "^script" +syntax match ishKey "^object" + diff --git a/etc/conf/zshrc b/etc/conf/zshrc index 3c0b3839..c10e4b5f 100644 --- a/etc/conf/zshrc +++ b/etc/conf/zshrc @@ -89,15 +89,8 @@ source ~/.oh-my-zsh/plugins/z/z.sh export PROMPT=$LOCAL_PROMPT'%![%*]%c$ ' export EDITOR=vim -[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh [ -f ~/.zsh_local ] && source ~/.zsh_local - -alias RR="source ~/.zshrc" - -alias t='tmux' -alias d='docker' -alias g='git' -alias v='vim' +[ -f ~/.ishrc ] && source ~/.ishrc bindkey -e bindkey jk accept-line @@ -105,8 +98,3 @@ bindkey "^N" down-line-or-beginning-search bindkey "^P" up-line-or-beginning-search bindkey -M vicmd j down-line-or-beginning-search bindkey -M vicmd k up-line-or-beginning-search - -# preexec() { -# # bench web.code.counter nexec 1 >/dev/null -# } -#