Created
November 5, 2022 15:35
-
-
Save cdecompilador/e806783033ddc05b162fac40b42cb12e to your computer and use it in GitHub Desktop.
wnv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import os | |
# Check that a command exists on the path | |
def check_install(cmd_name): | |
try: | |
subprocess.run([cmd_name]) | |
except FileNotFoundError: | |
return False | |
return True | |
def main(): | |
# Check that `lsd` command exists on the path | |
if not check_install("lsd"): | |
print("error: `lsd` command not avaible on the path") | |
return 1 | |
# Spawn the `lsd` command and extract the output | |
stdout = subprocess.run( | |
["lsd", "-1", "--classic", "-R", "-Itarget", "-Ibuild"], | |
capture_output=True).stdout | |
# TODO: handle unix separator too | |
separator = b"\\" | |
# At first the father is the current file | |
father = b"." | |
# Iterate every output file of the command which may be a filename, | |
# a father or nothing (two newlines produce a b"") | |
for line in stdout.split(b"\n"): | |
# Skip empty lines | |
if line == b"": | |
continue | |
if line.startswith(b".\\"): | |
# Update the father | |
father = line[:-1] | |
else: | |
# Generate and print the path if its a file | |
path = (father + separator + line).decode("utf-8") | |
if os.path.isfile(path): | |
print(path) | |
return 0 | |
if __name__ == "__main__": | |
raise SystemExit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment