Last active
August 29, 2015 14:16
-
-
Save tiborvass/6d8e8662118dec4ca2ba to your computer and use it in GitHub Desktop.
Hijacking Standard streams for Windows ANSI Emulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func Main() { | |
// everything that is currently in our main() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build !windows | |
package main | |
func main() { | |
Main() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build windows | |
func main() { | |
term.Windows(Main) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)