summaryrefslogtreecommitdiff
path: root/ar/.config/zsh/plugins.zsh
blob: f1ef0a9e565246b478bf2b7ae4818599f2ff6669 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/zsh

[[ "$XDG_SCRIPTS_HOME" == "$HOME/.local/bin" ]] || return
[[ "$ZDOTDIR" == "$HOME/.config/zsh" ]] || return
[[ "$ZPLUGINDIR" == "$XDG_SCRIPTS_HOME/zsh" ]] || return

### --- Plugins --- ###
plugins=(
    "Aloxaf/fzf-tab"
    # "jeffreytse/zsh-vi-mode"
    "kutsan/zsh-system-clipboard"
    # "MichaelAquilina/zsh-you-should-use"
    # "marlonrichert/zsh-autocomplete"
    "ohmyzsh/command-not-found"
    #"ohmyzsh/sudo"
    # "romkatv/powerlevel10k"
    "wfxr/forgit"
    "zdharma-continuum/fast-syntax-highlighting"
    "zsh-users/zsh-autosuggestions"
    "zsh-users/zsh-completions"
)

### --- Source Plugins --- ###
# Check plugins
zsh_check_plugins() {
    installed="true"
    for plugin in "${plugins[@]}"; do
        PLUGIN_NAME=$(echo "$plugin" | cut -d '/' -f 2)
        [ "$ZPLUGINDIR" = "ohmyzsh" ] && ZPLUGINDIR="$PLUGIN_NAME"
        PLUGIN_PATH="$ZPLUGINDIR/$PLUGIN_NAME"
        [ -d "$PLUGIN_PATH" ] && zsh_source_plugin "$PLUGIN_NAME/$PLUGIN_NAME" || { installed="false"; break; }
    done
    [ "$installed" = "true" ] || zsh_add_plugins "${plugins[@]}"
}

# Function to source plugin files
zsh_source_plugin() {
    for file in "$@"; do
        if [ -f "$ZPLUGINDIR/$file.plugin.zsh" ] && echo "$file" | grep -vq "command-not-found/command-not-found"; then
            . "$ZPLUGINDIR/$file.plugin.zsh"
        elif echo "$file" | grep -q "command-not-found/command-not-found"; then
            . "$ZPLUGINDIR/command-not-found/command-not-found.plugin.zsh"
        elif [ -z "$ZPLUGINDIR/$file.plugin.zsh" ] && [ -f "$ZPLUGINDIR/$file.zsh" ]; then
            . "$ZPLUGINDIR/$file.zsh"
        fi
        [ -f "$ZPLUGINDIR/$file.zsh-theme" ] && . "$ZPLUGINDIR/$file.zsh-theme" && . "${XDG_CONFIG_HOME:-${HOME}/.config}/shell/p10k"
    done
}

# Function to add plugins
zsh_add_plugins() {
    for plugin do
        PLUGIN_NAME=$(echo "$plugin" | cut -d '/' -f 2)
        PLUGIN_PATH="$ZPLUGINDIR/$PLUGIN_NAME"

        if [ -d "$PLUGIN_PATH" ]; then
            zsh_source_plugin "$PLUGIN_NAME/$PLUGIN_NAME"
        else
            ORG_NAME=$(echo "$plugin" | cut -d '/' -f 1)
            case "$ORG_NAME" in
                "ohmyzsh")
                    [ ! -d "$ZPLUGINDIR/oh-my-zsh" ] && git clone --depth=1 https://github.com/ohmyzsh/ohmyzsh.git "$ZPLUGINDIR/oh-my-zsh" >/dev/null 2>&1

                    OHMYZSH_PLUGIN_PATH="$ZPLUGINDIR/oh-my-zsh/plugins/$PLUGIN_NAME"
                    cp -r "$OHMYZSH_PLUGIN_PATH" "$ZPLUGINDIR/" >/dev/null 2>&1
                    rm -rf "$ZPLUGINDIR/oh-my-zsh" >/dev/null 2>&1
                    ;;
                *)
                    git clone "https://github.com/$plugin.git" "$PLUGIN_PATH" >/dev/null 2>&1
                    ;;
            esac
            rm -rf "$PLUGIN_PATH/.git"
            chmod +x "$PLUGIN_PATH"
        fi
    done
}

# Function to sync plugins
zsh_sync_plugins() {
    ACTIVE_PLUGINS=$(grep '^[[:space:]]*"[^"]\+"' ~/.config/zsh/plugins.zsh | sed 's|.*/\([^/"]*\)".*|\1|')

    for PLUGIN_DIR in "$ZPLUGINDIR"/*; do
        if [ -d "$PLUGIN_DIR" ]; then
            PLUGIN_NAME=$(basename "$PLUGIN_DIR")

            echo "$ACTIVE_PLUGINS" | grep -q "$PLUGIN_NAME" || {
                echo "Removing unused plugin: $PLUGIN_NAME"
                rm -rf "$PLUGIN_DIR"
            }
        fi
    done
}

# Function to update plugins
# Since .git folder in each plugin dir is removed,
# Delete all plugins and install them agian
# .git is searched in Neovim projects
alias zup=zsh_update_plugins
zsh_update_plugins() {
    [ -d "$ZPLUGINDIR" ] && rm -rf "$ZPLUGINDIR" && zsh_check_plugins "${plugins[@]}"
    zsh_sync_plugins
}

zsh_check_plugins "${plugins[@]}"
zsh_sync_plugins