Skip to content

Instantly share code, notes, and snippets.

@cdecompilador
Created November 5, 2022 15:35
Show Gist options
  • Save cdecompilador/e806783033ddc05b162fac40b42cb12e to your computer and use it in GitHub Desktop.
Save cdecompilador/e806783033ddc05b162fac40b42cb12e to your computer and use it in GitHub Desktop.
wnv
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