lang/python/ PoorMansLs


The cygwin ls blocks trying to stat a link to a network share that isn't there. (On one laptop with a small SSD, I store my downloads on a network share, and when on the go that isn't accessible; there is a link in my home directory ~/dl so, if I want to ls inside my home directory, it hangs trying to stat ~/dl, so I wrote this to 'scratch this itch').

#!/usr/bin/env python3
from glob import glob
from colors import color
import os
import sys
args = sys.argv[1:]
files = set()
switches = set()
if len(args) == 0:
  args = ["*"]
fargs = []
for arg in args:
  if arg.startswith("-"):
    switches.add(arg[1:])
  else:
    fargs.append(arg)
if len(fargs) == 0:
  fargs = ["*"]
for farg in fargs:
  files.update(glob(farg))
for x in files:
  if os.path.islink(x):
    x = x+"@" if "f" in switches else x
    print(color(f"{x}",fg="cyan"))
  elif os.path.isdir(x):
    x = x+"/" if "f" in switches else x
    print(color(f"{x}",fg="blue"))
  elif os.access(x, os.X_OK):
    x = x+"*" if "f" in switches else x
    print(color(f"{x}",fg="green"))
  else:
    print(f"{x}")