Skip to content

Instantly share code, notes, and snippets.

@hchbaw
Created January 5, 2011 15:09
Show Gist options
  • Save hchbaw/766425 to your computer and use it in GitHub Desktop.
Save hchbaw/766425 to your computer and use it in GitHub Desktop.
# Zsh color cUBE utility.
# To fire, please source this file and type some texts on the command line.
# Then please type '<ESC>xzube<CR>', or just '<ESC>xzube-text<CR>'.
zube-loop () {
local -i i="$1"
local -i t="$2"
local -i k="$3"
local -i a="$4"
local r="$5"
local p="$6"
((k<=255)) || return
((i<=$#BUFFER)) || return
local s=${BUFFER[$i]-}
[[ -n "$s" ]] && { "$p" "$s" } && {
"$r" "$((i-1))" "$((i))" "$k"
((a==t)) && {zube-loop $((i+1)) $t $((k+1)) 0 $r $p; return $?}
((a!=t)) && {zube-loop $((i+1)) $t $((k)) $((a+1)) $r $p; return $?}
} || {zube-loop $((i+1)) $t $((k)) $a $r $p; return $?}
}
zube-hi-bg () { region_highlight+=("$1 $2 bg=$3") }
zube-hi-fg () { region_highlight+=("$1 $2 fg=$3") }
zube-able-p () { [[ "$1" != $'\012' ]] }
zube-raw () {
region_highlight=()
zube-loop 0 "$1" "${4:-0}" 0 "$2" "$3"
}
zube () { zube-raw 0 zube-hi-fg zube-able-p }; zle -N zube
zube-source-text-0 () {
local -i i=0
# 8 ANSI colors + high-intensities
#echo {000..007}; echo; echo {008..015}
# 6x6x6 color cube
i=16
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=17))}}; ((i++))
# grayscale ramp
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=11))}}; ((i++))
echo {${(l:3::0:)i}..${(l:3::0:)$((i+=11))}};
}
zube-source-text () { BUFFER="$(echo; zube-source-text-0)" }
zube-text-p () { [[ "$1" != " " ]] && [[ "$1" != $'\012' ]] }
zube-text () {
zube-source-text
zube-raw 2 zube-hi-fg zube-text-p 16
}
zle -N zube-text
[[ -n "${ZUBE-}" ]] || return
# A demonstration code. A Happy new year in Japanese.
zube-source () {
local c="$1"
local t="$2"
BUFFER="$(
local n=""
# eval to expand $c inside an 'l'.
eval "
echo \${(l:((16$t))::$c:)n}
[[ \"\${c-}\" == \" \" ]] || {
echo -n あけましておめでとうございます。
echo 今年もどうぞよろしくおねがいいたします。
echo -n あけましておめでとうございます。
echo 今年もどうぞよろしくおねがいいたします。
}
echo \${(l:((36$t))::$c:)n}
echo \${(l:((36$t))::$c:)n}
echo \${(l:((36$t))::$c:)n}
echo \${(l:((36$t))::$c:)n}
[[ \"\${c-}\" == \" \" ]] || {
echo -n あけましておめでとうございます。
echo 今年もどうぞよろしくおねがいいたします。
echo -n あけましておめでとうございます。
echo 今年もどうぞよろしくおねがいいたします。
}
[[ \"\${c-}\" == \" \" ]] && {
echo \${(l:((36$t))::$c:)n}
echo \${(l:((36$t))::$c:)n}
}
echo \${(l:((25$t))::$c:)n}
"
)"
}
zube~ () {
zube-source "M" "*2"
zube-raw 1 zube-hi-fg zube-able-p
zle -N zube zube~~
}
zube~~ () {
zube-source " " "*2"
zube-raw 1 zube-hi-bg zube-able-p
zle -N zube zube~~~
}
zube~~~-p () { [[ "$1" != " " ]] && [[ "$1" != $'\012' ]] }
zube~~~ () {
zube-raw 2 zube-hi-fg zube~~~-p
}
# XXX: redefined!
zle -N zube zube~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment