summaryrefslogtreecommitdiff
path: root/zsh/.zsh/functions
blob: 411f27a2aa8cf029cfc2a5e0cf3180d0b30da381 (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
#!/bin/zsh

[[ -z ${ZSH_COLORS} ]] && ZSH_COLORS=${HOME}/.local/share/zsh/colors.zsh
[[ -f ${ZSH_COLORS} ]] && source ${ZSH_COLORS}

colorschemes=$(ls ${HOME}/.Xresources)

function colo {

    XRESOURCE=$1
    XRESOURCE_FILE=${HOME}/.Xresources/${XRESOURCE}

    # Change colors for current session
    if [[ ! -z "$TMUX" ]]; then
        printf '\x1bPtmux;'
        esc='\x1b\x1b'
    else
        esc='\x1b'
    fi

    /usr/bin/cpp ${XRESOURCE_FILE} | tr -d ' \t' | sed -n \
        -e "s/.*background:/${esc}]11;/p" \
        -e "s/.*foreground:/${esc}]10;/p" \
        -e "s/.*borderColor:/${esc}]708;/p" \
        -e "s/.*color\\([0-9][^:]*\\):/${esc}]4;\\1;/p" | tr \\n \\a

    # Change color for future sessions
    xrdb ${XRESOURCE_FILE}

    # Update termite colorscheme
    TERMITE_CONFIG=~/.config/termite/config
    while read -r line || [[ -n "$line" ]]; do
        case "$line" in
            *background* ) sed -i "s/^background.*$/background = ${line##* }/" ${TERMITE_CONFIG} ;;
            *foreground* ) sed -i "s/^foreground.*$/foreground = ${line##* }/" ${TERMITE_CONFIG} ;;
            *cursorColor* ) sed -i "s/^cursor.*$/cursor = ${line##* }/" ${TERMITE_CONFIG} ;;
            *color[0-9]* ) color=$(echo ${line/:*/} | sed 's/.*color\([0-9]\+\)/\1/');
                sed -i "s/^color${color}\>.*$/color${color} = ${line##* }/" ${TERMITE_CONFIG} ;;
            * ) ;;
        esac
    done < ${XRESOURCE_FILE}

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

    echo "export COLORSCHEME=${XRESOURCE%.*}" > ${ZSH_COLORS}
    echo "export BACKGROUND=${XRESOURCE#*.}" >> ${ZSH_COLORS}
    echo "export XRESOURCE=${XRESOURCE}" >> ${ZSH_COLORS}
    source ${ZSH_COLORS}
}

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