aboutsummaryrefslogtreecommitdiff
path: root/conf.d/00functions.fish
blob: d1ad87b2bcf3b043443c9c6d20ef2dffef157056 (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
function alias_if --description "Alias if a program exists"
  if test (count $argv) -lt 2
    return
  end
  if command -sq $argv[1]
    if test (count $argv) -gt 2
      alias $argv[2] "$argv[1] $argv[3..-1]"
    else
      alias $argv[2] "$argv[1]"
    end
  end
end

function alias_if_not --description "Alias if a program doesn't exist"
  if test (count $argv) -lt 2
    return
  end
  if not command -sq $argv[1]
    if test (count $argv) -gt 2
      alias $argv[1] "$argv[2] $argv[3..-1]"
    else
      alias $argv[1] "$argv[2]"
    end
  end
end

function find_prog --description "Find one or more programs in \$PATH"
  for prog in $argv
    for search_path in $PATH
      if test -x "$search_path/$prog"
        echo -n "$prog"
        return
      end
    end
  end
end

function ask --description "Ask a question answered by yes or no"
  read -n 1 -f -P (set_color -o blue)"::"(set_color normal)" $argv[1] [y/N] " answer
  switch $answer
    case y Y
      return 0 # yes
    case '*'
      return 1 # no
  end
end