]> Sergey Matveev's repositories - schwabrak.git/blob - lib.zsh.rc
supercatlessness
[schwabrak.git] / lib.zsh.rc
1 setopt ERR_EXIT PIPE_FAIL EXTENDED_GLOB GLOB_STAR_SHORT
2
3 PERL=${PERL:-perl}
4 EDITOR=${EDITOR:-vi}
5
6 die() {
7     print $@ >&2
8     exit 1
9 }
10
11 [[ -d issues ]] || die You must run that command in directory with issues/
12
13 autoload -Uz relative
14
15 endash() {
16     $PERL -npe 's/(-+)/$1-/g ; s/ /-/g'
17 }
18
19 dedash() {
20     $PERL -npe 's/([^-])-([^-])/$1 $2/g ; s/-(-+)/$1/g'
21 }
22
23 # Usage: colourise ColourSpec:RegExp [ColourSpec:RegExp ...]
24 # It is intended to be a drop-in replacement for supercat utility.
25 # ColourSpec is comma delimited list of ANSI codes for colour setting.
26 # Look for zsh'es Functions/Misc/colors documentation about possible
27 # colour values. For example to set bold red on green background you use
28 # "bold,fg-red,bg-green" as a ColourSpec.
29 # RegExp is POSIX extended regular expression of the text you want to
30 # colourise.
31 colourise() {
32     if [[ -n $NO_COLOR ]] || [[ $# -eq 0 ]] ; then
33         cat
34         return
35     fi
36     (( ${+colour} )) || { autoload -Uz colors ; colors ; }
37     local lc=$'\e[' rc=m colr=""
38     local spec=(${(s/:/)1})
39     shift
40     for c (${(s/,/)${spec[1]}}) colr="${col}${lc}${colour[$c]}${rc}"
41     local re=${(j/:/)spec[2,-1]}
42     sed -E "s/${re}/${colr}&${reset_color}/g" | colourise $@
43 }
44
45 delim() {
46     {
47         local i
48         for i ({1..40}) print -n -- -
49         print
50     } | colourise magenta:".*"
51 }