File: find_file.bash

package info (click to toggle)
crazy-complete 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,404 kB
  • sloc: python: 7,949; sh: 4,636; makefile: 74
file content (51 lines) | stat: -rw-r--r-- 1,062 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
#!/bin/bash

_find_files() {
  local APPEND=false
  local DIRECTORY='.'
  local ONLY_DIRECTORIES=''
  local REGEX=''
  local REGEX_EXTENDED=''

  while getopts ":D:R:adE" opt; do
    case "$opt" in
      D) DIRECTORY="$OPTARG";;
      R) REGEX="$OPTARG";;
      a) APPEND=true;;
      d) ONLY_DIRECTORIES='-d';;
      E) REGEX_EXTENDED='-E';;
     \?) echo "Invalid option: $OPTARG" >&2;
         return 1;;
      :) echo "Option -$OPTARG requires an argument" >&2;
         return 1;;
     esac
  done

  local -a COMPREPLY_BACK=("${COMPREPLY[@]}")

  if [[ "$DIRECTORY" != '.' ]]; then
    if pushd "$DIRECTORY"; then
      _filedir $ONLY_DIRECTORIES
      popd
    else
      return 1
    fi
  else
    _filedir $ONLY_DIRECTORIES
  fi

  if [[ -n "$REGEX" ]]; then
    local -a COMPREPLY_NEW
    local FILE
    for FILE in ; do
      grep -q $REGEX_EXTENDED -- "$REGEX" <<< "$FILE" && COMPREPLY_NEW+=("$FILE")
    done
    COMPREPLY=("${COMPREPLY_NEW[@]}")
  fi

  if $APPEND; then
    COMPREPLY=("${COMPREPLY_BACK[@]}" "${COMPREPLY[@]}")
  fi
}

_find_files