File: wi.py 1 #!/usr/bin/python3 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2024 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 # wi [pattern] [folders...] 27 # 28 # Where Is looks at all file/folder names in the folders given, trying to 29 # case-insensitively match them to the regex-pattern given. 30 # 31 # All folders are searched into recursively; when no folders are given, the 32 # current one is searched into by default. 33 34 35 from os import walk 36 from re import compile as compile_re, IGNORECASE, Pattern 37 from sys import argv, exit, stderr, stdin, stdout 38 39 40 # info is the help message shown when asked to 41 info = ''' 42 wi [pattern] [folders...] 43 44 Where Is looks at all file/folder names in the folders given, trying to 45 case-insensitively match them to the regex-pattern given. 46 47 All folders are searched into recursively; when no folders are given, the 48 current one is searched into by default. 49 '''.strip() 50 51 # handle standard help cmd-line options, quitting right away in that case 52 if len(argv) == 2 and argv[1] in ('-h', '--h', '-help', '--help'): 53 print(info, file=stderr) 54 exit(0) 55 56 57 58 def match_names(folder: str, expr: Pattern) -> None: 59 if folder.endswith('/'): 60 folder = folder[:-1] 61 62 for (cur, subfolders, files) in walk(folder): 63 for f in subfolders: 64 s = f'{cur}/{f}/' 65 if not expr.search(s) is None: 66 print(s) 67 for f in files: 68 s = f'{cur}/{f}' 69 if not expr.search(s) is None: 70 print(s) 71 72 73 try: 74 if len(argv) < 2: 75 expr = compile_re('.') 76 else: 77 expr = compile_re(argv[1], flags=IGNORECASE) 78 79 stdout.reconfigure(newline='\n', encoding='utf-8') 80 81 # search all folders given 82 for folder in argv[2:]: 83 match_names(folder, expr) 84 85 # when no folders are given, use the current one 86 if len(argv) < 3: 87 match_names('.', expr) 88 except (BrokenPipeError, KeyboardInterrupt): 89 # quit quietly, instead of showing a confusing error message 90 stderr.flush() 91 stderr.close() 92 except Exception as e: 93 print(f'\x1b[31m{e}\x1b[0m', file=stderr) 94 exit(1)