summaryrefslogtreecommitdiff
path: root/zsh/.zsh/functions
blob: 86c1894caa9e7696f30d0c85b87db2fd2b613876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/zsh

if [[ -z ${ZSH_COLORS} ]]; then
    ZSH_COLORS=${HOME}/.local/share/zsh/colors.zsh
fi

colorschemes=$(echo ${HOME}/.Xresources/**/*.xresources | sed "s#${HOME}/.Xresources/##g" | sed 's#base16/base16-##g' | \
    sed 's/[^ ]\+\.256\.[^ ]\+//g' | sed 's/.xresources//g' | sed 's/[ ]\+/ /g' )

function colo {

    # Xresource file
    if [[ ! -z $1 ]]; then
        XRESOURCE=$1
    else
        XRESOURCE=$(echo ${colorschemes} | sed 's/ /\n/g' | sed '/^[ ]*$/d' | fzf)
        [[ -z ${XRESOURCE} ]] && return
    fi
    if [[ -f ${HOME}/.Xresources/${XRESOURCE}.xresources ]]; then
        XRESOURCE_FILE=${HOME}/.Xresources/${XRESOURCE}.xresources
    elif [[ -f ${HOME}/.Xresources/base16/base16-${XRESOURCE}.256.xresources ]]; then
        XRESOURCE_FILE=${HOME}/.Xresources/base16/base16-${XRESOURCE}.256.xresources
    else
        echo "Invalid colorscheme!"
        return
    fi

    # Change colors for current session
    /usr/bin/cpp ${XRESOURCE_FILE} | tr -d ' \t' | sed -n \
        -e 's/.*background:/\x1b]11;/p' \
        -e 's/.*foreground:/\x1b]10;/p' \
        -e 's/.*color\([0-9][^:]*\):/\x1b]4;\1;/p' | tr \\n \\a

    # Ensure border color is the same as background
    bg=$(/usr/bin/cpp ${XRESOURCE_FILE} | grep "background" | sed 's/[^ ]\+[ ]\+\([^ ]\+\)$/\1/')
    echo "\x1b]708;${bg}" | tr \\n \\a

    # Load Xresources file for future sessions
    xrdb ~/.Xresources/base.xresources
    xrdb -merge ${XRESOURCE_FILE}
    xrdb -merge <<< "URxvt.borderColor: ${bg}"

    # Change default environment variable for future sessions
    mkdir -p $(dirname ${ZSH_COLORS})

    COLORSCHEME=$(echo $(basename ${XRESOURCE_FILE}) | sed 's/\([^.]\+\)\..\+$/\1/g')
    BACKGROUND=$(echo ${XRESOURCE_FILE} | sed 's/^.\+\(dark\|light\).\+$/\1/')

    echo "export COLORSCHEME=${COLORSCHEME}" > ${ZSH_COLORS}
    echo "export BACKGROUND=${BACKGROUND}" >> ${ZSH_COLORS}
    echo "export XRESOURCE=${XRESOURCE}" >> ${ZSH_COLORS}
    source ${ZSH_COLORS}
}

# Source colors
if [[ -f ${ZSH_COLORS} ]]; then
    source ${ZSH_COLORS}
    colo ${XRESOURCE}
    # echo "sourced ${XRESOURCE}"
fi

# Completion for colorschemes (-M -> Case insensitive)
compctl -k "(${colorschemes})" -M 'm:{a-z}={A-Z}' colo