File: wi.py
   1 #!/usr/bin/python3
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 from os import walk
  27 from re import compile as compile_re, IGNORECASE, Pattern
  28 from sys import argv, exit, stderr, stdout
  29 
  30 
  31 info = '''
  32 wi [pattern] [folders...]
  33 
  34 Where Is looks at all file/folder names in the folders given, trying to
  35 case-insensitively match them to the regex-pattern given.
  36 
  37 All folders are searched into recursively; when no folders are given, the
  38 current one is searched into by default.
  39 '''
  40 
  41 # handle standard help cmd-line options, quitting right away in that case
  42 if len(argv) == 2 and argv[1] in ('-h', '--h', '-help', '--help'):
  43     print(info.strip())
  44     exit(0)
  45 
  46 
  47 
  48 def match_names(w, folder: str, expr: Pattern) -> None:
  49     if folder.endswith('/'):
  50         folder = folder[:-1]
  51 
  52     for (cur, subfolders, files) in walk(folder):
  53         for f in subfolders:
  54             s = f'{cur}/{f}/'
  55             if not expr.search(s) is None:
  56                 w.write(s)
  57                 w.write('\n')
  58                 w.flush()
  59         for f in files:
  60             s = f'{cur}/{f}'
  61             if not expr.search(s) is None:
  62                 w.write(s)
  63                 w.write('\n')
  64                 w.flush()
  65 
  66 
  67 try:
  68     if len(argv) < 2:
  69         expr = compile_re('.')
  70     else:
  71         expr = compile_re(argv[1], flags=IGNORECASE)
  72 
  73     # search all folders given
  74     for folder in argv[2:]:
  75         match_names(stdout, folder, expr)
  76 
  77     # when no folders are given, use the current one
  78     if len(argv) < 3:
  79         match_names(stdout, '.', expr)
  80 except BrokenPipeError:
  81     # quit quietly, instead of showing a confusing error message
  82     stderr.close()
  83 except KeyboardInterrupt:
  84     exit(2)
  85 except Exception as e:
  86     print(f'\x1b[31m{e}\x1b[0m', file=stderr)
  87     exit(1)