Skip to content

Instantly share code, notes, and snippets.

@tiborvass
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiborvass/6d8e8662118dec4ca2ba to your computer and use it in GitHub Desktop.
Save tiborvass/6d8e8662118dec4ca2ba to your computer and use it in GitHub Desktop.
Hijacking Standard streams for Windows ANSI Emulation
package main
func Main() {
// everything that is currently in our main()
}
// +build !windows
package main
func main() {
Main()
}
// +build windows
func main() {
term.Windows(Main)
}
// +build windows
package term
var ch = make(chan struct{})
func init() {
stdoutR, stdoutW, err := os.Pipe()
if err != nil {
panic(err)
}
os.Stdout = stdoutW
go func() {
emulateAnsi(stdoutR) // should be emulateAnsi(stdinW, stdoutR, stderrR)
close(ch)
}()
}
func Windows(f func()) {
defer func() {
os.Stdout.Close()
<-ch
}()
f()
}
@sachin-jayant-joshi
Copy link

With respect to re-initializing the Stdin etc . The variables are of type File* which is a concrete type and not an interface.

I am having hard time reinitializing them in a polymorphic way.
Here are some of my unsuccessful attempts

http://play.golang.org/p/YmtxqGhorH
prog.go:32: cannot use f (type *MyFile) as type *File in argument to Hello

http://play.golang.org/p/TDxLBEYtE5

prog.go:22: cannot use wr (type *MyWriter) as type *os.File in assignment

http://play.golang.org/p/LtKvOFkbbi

prog.go:50: cannot use NewDerived1() (type *Derived1) as type *Base in assignment
prog.go:51: cannot use NewDerived2() (type *Derived2) as type *Base in assignment

http://play.golang.org/p/EHf4rtIEch

prog.go:47: cannot convert NewDerived1() (type *Derived1) to type PBASE
prog.go:48: cannot convert NewDerived2() (type *Derived2) to type PBASE
prog.go:53: arr[0].Foo undefined (type PBASE has no field or method Foo)
prog.go:54: arr[1].Foo undefined (type PBASE has no field or method Foo)
prog.go:55: arr[2].Foo undefined (type PBASE has no field or method Foo)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment