Browse Source

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.
Carl Jackson 12 years ago
parent
commit
232a1ca725
1 changed files with 19 additions and 3 deletions
  1. +19
    -3
      web/middleware/terminal.go

+ 19
- 3
web/middleware/terminal.go View File

@ -3,8 +3,7 @@ package middleware
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"code.google.com/p/go.crypto/ssh/terminal"
"os"
) )
var ( var (
@ -30,7 +29,24 @@ var (
reset = []byte{'\033', '[', '0', 'm'} 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 // colorWrite
func cW(buf *bytes.Buffer, color []byte, s string, args ...interface{}) { func cW(buf *bytes.Buffer, color []byte, s string, args ...interface{}) {


Loading…
Cancel
Save