]> code.delx.au - monosys/commitdiff
Initial colorwrap version
authorJames Bunton <jamesbunton@fastmail.fm>
Tue, 5 Feb 2008 03:09:57 +0000 (14:09 +1100)
committerJames Bunton <jamesbunton@fastmail.fm>
Tue, 5 Feb 2008 03:09:57 +0000 (14:09 +1100)
scripts/linux_colorwrap [new file with mode: 0755]

diff --git a/scripts/linux_colorwrap b/scripts/linux_colorwrap
new file mode 100755 (executable)
index 0000000..44ad381
--- /dev/null
@@ -0,0 +1,32 @@
+#!/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)
+