From 232a1ca725943e7fc6a4832e133b79a99a2ae079 Mon Sep 17 00:00:00 2001 From: Carl Jackson Date: Sun, 23 Mar 2014 12:01:56 -0700 Subject: [PATCH] Replace terminal dependency with hand-waving In order to avoid a dependency on the go.crypto terminal package, let's try to do our own TTY sniffing. I think in practice this will work surprisingly well, even if it feels incredibly sketchy. --- web/middleware/terminal.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/web/middleware/terminal.go b/web/middleware/terminal.go index 813e83d..db02917 100644 --- a/web/middleware/terminal.go +++ b/web/middleware/terminal.go @@ -3,8 +3,7 @@ package middleware import ( "bytes" "fmt" - - "code.google.com/p/go.crypto/ssh/terminal" + "os" ) var ( @@ -30,7 +29,24 @@ var ( reset = []byte{'\033', '[', '0', 'm'} ) -var isTTY = terminal.IsTerminal(1) +var isTTY bool + +func init() { + // This is sort of cheating: if stdout is a character device, we assume + // that means it's a TTY. Unfortunately, there are many non-TTY + // character devices, but fortunately stdout is rarely set to any of + // them. + // + // We could solve this properly by pulling in a dependency on + // code.google.com/p/go.crypto/ssh/terminal, for instance, but as a + // heuristic for whether to print in color or in black-and-white, I'd + // really rather not. + fi, err := os.Stdout.Stat() + if err == nil { + m := os.ModeDevice | os.ModeCharDevice + isTTY = fi.Mode()&m == m + } +} // colorWrite func cW(buf *bytes.Buffer, color []byte, s string, args ...interface{}) {