--- /dev/null
+#!/usr/bin/env python
+
+PAGERS = ["less", "more", "tail", "head", "grep"]
+
+# Usage:
+# * Put this file into ~/bin/colorwrap
+# * Add these aliases to your bash config
+# alias ls="colorwrap ls"
+# alias grep="colorwrap grep"
+
+
+import os, os.path, sys
+
+command = sys.argv[1]
+args = sys.argv[1:]
+pipe = os.readlink("/proc/%d/fd/1" % os.getpid())
+
+for d in filter(str.isdigit, os.listdir("/proc/")):
+ d = "/proc/" + d
+ fd = "%s/fd/0" % d
+ if not os.path.exists(fd):
+ continue
+ if os.readlink(fd) == pipe:
+ line = file("%s/cmdline" % d).read().split("\x00")
+ if line[0] == "python" and line[1] == sys.argv[0] or line[0] in PAGERS:
+ args.insert(1, "--color=always")
+ break
+else:
+ args.insert(1, "--color=auto")
+
+os.execvp(command, args)
+