aboutsummaryrefslogtreecommitdiff
path: root/conf.d/00functions.fish
blob: d20c270e762d4f859c770c02b4819958d5efd5c4 (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
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 pp --description "Pretty print a message from a script" -a type -a msg

  if test -z "$msg"
    return
  end

  if test -z "$type"
    set type info
  end

  switch $type
    case error
      set -f mcolor red
      set -f pcolor red
      set -f prefix '!! '
    case warn
      set -f mcolor yellow
      set -f pcolor yellow
      set -f prefix '?? '
    case info
      set -f mcolor normal
      set -f pcolor green
      set -f prefix '==> '
    case info2
      set -f mcolor normal
      set -f pcolor blue
      set -f prefix '  -> '
    case ask
      set -f mcolor normal
      set -f pcolor blue
      set -f prefix ':: '
    case debug
      set -f mcolor cyan
      set -f pcolor cyan
      set -f prefix '** '
  end

  echo -n (set_color -o $pcolor)"$prefix"(set_color normal)
  echo $(set_color $mcolor)"$msg"(set_color normal)

end

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