File: shell.zsh

package info (click to toggle)
zplug 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: sh: 1,504; awk: 235; makefile: 26
file content (280 lines) | stat: -rw-r--r-- 5,602 bytes parent folder | download | duplicates (3)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
__zplug::utils::shell::remove_deadlinks()
{
    local link

    for link in "$@"
    do
        if [[ -L $link ]] && [[ ! -e $link ]]; then
            rm -f "$link"
        fi
    done
}

__zplug::utils::shell::search_commands()
{
    local -a args
    local    arg element cmd_name
    local    is_verbose=true

    while (( $# > 0 ))
    do
        arg="$1"
        case "$arg" in
            --verbose)
                is_verbose=true
                ;;
            --silent)
                is_verbose=false
                ;;
            -*|--*)
                return 1
                ;;
            *)
                args+=( "$arg" )
                ;;
        esac
        shift
    done

    for arg in "${args[@]}"
    do
        for element in "${(s.:.)arg}"
        do
            # Extract the first argument sparated by a space
            cmd_name="${element%% *}"

            # Check if cmd_name is available
            if (( $+commands[$cmd_name] )); then
                if $is_verbose; then
                    echo "$cmd_name"
                fi
                return 0
            else
                continue
            fi
        done
    done

    return 1
}

__zplug::utils::shell::glob2regexp()
{
    local -i i=0
    local    glob="$1" char

    printf "^"
    for ((; i < $#glob; i++))
    do
        char="${glob:$i:1}"
        case "$char" in
            \*)
                printf '.*'
                ;;
            .)
                printf '\.'
                ;;
            "{")
                printf '('
                ;;
            "}")
                printf ')'
                ;;
            ,)
                printf '|'
                ;;
            "?")
                printf '.'
                ;;
            \\)
                printf '\\\\'
                ;;
            *)
                printf "$char"
                ;;
        esac
    done
    printf "$\n"
}

__zplug::utils::shell::sudo()
{
    local pw="$ZPLUG_SUDO_PASSWORD"

    if [[ -z $pw ]]; then
        __zplug::log::write::error \
            "ZPLUG_SUDO_PASSWORD: is an invalid value\n"
        return 1
    fi

    sudo -k
    echo "$pw" \
        | sudo -S -p '' "$argv[@]"
}

__zplug::utils::shell::unansi()
{
    perl -pe 's/\e\[?.*?[\@-~]//g'
}

__zplug::utils::shell::cd()
{
    local    dir arg
    local -a dirs
    local    is_force=false

    while (( $# > 0 ))
    do
        arg="$1"
        case "$arg" in
            --force)
                is_force=true
                ;;
            -*|--*)
                return 1
                ;;
            "")
                ;;
            *)
                dirs+=( "$arg" )
                ;;
        esac
        shift
    done

    for dir in "$dirs[@]"
    do
        if $is_force; then
            [[ -d $dir ]] || mkdir -p "$dir"
        fi

        builtin cd "$dir" &>/dev/null
        return $status
    done

    return 1
}

__zplug::utils::shell::getopts()
{
    printf "%s\n" "$argv[@]" \
        | awk -f "$ZPLUG_ROOT/misc/contrib/getopts.awk"
}

__zplug::utils::shell::pipestatus()
{
    local _status="${pipestatus[*]-}"

    [[ ${_status//0 /} == 0 ]]
    return $status
}

__zplug::utils::shell::expand_glob()
{
    local    pattern="$1" file
    # Modifiers to use if $pattern does not include modifiers
    local    default_modifiers="${2:-(N)}"
    local -a matches

    # Modifiers not specified (by user)
    if [[ ! $pattern =~ '[^/]\([^)]*\)$' ]]; then
        pattern+="$default_modifiers"
    fi

    # Try expanding ~ and *
    matches=( ${~pattern} )

    # Use subshell for brace expansion
    if (( $#matches <= 1 )); then
        matches=( $( \
            zsh -c "$_ZPLUG_CONFIG_SUBSHELL; echo $pattern" \
            2> >(__zplug::log::capture::error) \
        ) )
    fi

    for file in "${matches[@]}"
    do
        [[ -e ${~file} ]] && echo ${~file}
    done
}

__zplug::utils::shell::zglob()
{
    (
    emulate -RL zsh
    setopt localoptions extendedglob
    local    f g match mbegin mend p_dir1 p_dir2
    local    MATCH MBEGIN MEND
    local    pat repl fpat
    local -a files targets
    local -A from to

    p_dir1=${~1:h}
    p_dir2=${~2:h}
    builtin cd $p_dir1
    pat=${1:t}
    repl=${2:t}
    shift 2

    if [[ $pat = (#b)(*)\((\*\*##/)\)(*) ]]; then
        fpat="$match[1]$match[2]$match[3]"
        setopt localoptions bareglobqual
        fpat="${fpat}(odon)"
    else
        fpat=$pat
    fi

    files=(${~fpat}(N))
    for f in $files[@]
    do
        if [[ $pat = (#b)(*)\(\*\*##/\)(*) ]]; then
            pat="$match[1](*/|)$match[2]"
        fi
        [[ -e $f && $f = (#b)${~pat} ]] || continue
        set -- "$match[@]"
        g=${(Xe)repl} 2>/dev/null
        from[$g]=$f
        to[$f]=$g
    done

    for f in $files[@]
    do
        [[ -z $to[$f] ]] && continue
        targets=($p_dir1/$f $p_dir2/$to[$f])
        print -r -- ${(q-)targets}
    done
    )
}

__zplug::utils::shell::eval()
{
    local cmd

    # Report stderr to error log
    eval "${=cmd}" 2> >(__zplug::log::capture::error) >/dev/null
    return $status
}

__zplug::utils::shell::json_escape()
{
    if (( $+commands[python] )) && python -c 'import json' 2> /dev/null; then
        python -c '
from __future__ import print_function
import json,sys
print(json.dumps(sys.stdin.read()))' \
    2> >(__zplug::log::capture::error)
    else
        echo "(Not available: python with json required)"
    fi
}

__zplug::utils::shell::is_atty()
{
    if [[ -t 0 && -t 1 ]]; then
        # terminal
        return 0
    else
        # pipeline
        return 1
    fi
}