Exemplo n.º 1
0
func Init(theme *ColorTheme, black bool, mouse bool) {
	{
		in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)
		if err != nil {
			panic("Failed to open /dev/tty")
		}
		_in = in
		// Break STDIN
		// syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))
	}

	C.setlocale(C.LC_ALL, C.CString(""))
	_screen = C.newterm(nil, C.stderr, C.stdin)
	if _screen == nil {
		fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))
		os.Exit(2)
	}
	C.set_term(_screen)
	if mouse {
		C.mousemask(C.ALL_MOUSE_EVENTS, nil)
	}
	C.noecho()
	C.raw() // stty dsusp undef

	if theme != nil {
		C.start_color()
		initPairs(theme, black)
		_color = attrColored
	} else {
		_color = attrMono
	}
}
Exemplo n.º 2
0
func Newterm(s string, out, in *os.File) (*Screen, error) {
	cs := C.CString(s)
	defer C.free(unsafe.Pointer(cs))
	oFile, iFile := C.fdopen(C.int(out.Fd()), C._wplus_), C.fdopen(C.int(in.Fd()), C._rplus_)
	screen := (*Screen)(C.newterm(cs, oFile, iFile))
	if screen == nil {
		return nil, CursesError{"Failed to create term"}
	}
	return screen, nil
}
Exemplo n.º 3
0
// NewTerm returns a new Screen, representing a physical terminal. If using
// this function to generate a new Screen you should not call Init().
// Unlike Init(), NewTerm does not call Refresh() to clear the screen so this
// will need to be done manually. When finished with a terminal, you must
// call End() in reverse order that each terminal was created in. After you
// are finished with the screen you must call Delete to free the memory
// allocated to it. This function is usually only useful for programs using
// multiple terminals or test for terminal capabilites. The argument termType
// is the type of terminal to be used ($TERM is used if value is "" which also
// has the same effect of using os.Getenv("TERM"))
func NewTerm(termType string, out, in *os.File) (*Screen, error) {
	var tt, wr, rd *C.char
	if termType == "" {
		tt, wr, rd = (*C.char)(nil), C.CString("w"), C.CString("r")
	} else {
		tt, wr, rd = C.CString(termType), C.CString("w"), C.CString("r")
		defer C.free(unsafe.Pointer(tt))
	}
	defer C.free(unsafe.Pointer(wr))
	defer C.free(unsafe.Pointer(rd))

	cout, cin := C.fdopen(C.int(out.Fd()), wr), C.fdopen(C.int(in.Fd()), rd)
	screen := C.newterm(tt, cout, cin)
	if screen == nil {
		return nil, errors.New("Failed to create new screen")
	}
	return &Screen{screen}, nil
}
Exemplo n.º 4
0
func Init(theme *ColorTheme, black bool, mouse bool) {
	{
		in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)
		if err != nil {
			panic("Failed to open /dev/tty")
		}
		_in = in
		// Break STDIN
		// syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))
	}

	C.setlocale(C.LC_ALL, C.CString(""))
	_screen = C.newterm(nil, C.stderr, C.stdin)
	C.set_term(_screen)
	if mouse {
		C.mousemask(C.ALL_MOUSE_EVENTS, nil)
	}
	C.cbreak()
	C.noecho()
	C.raw() // stty dsusp undef

	intChan := make(chan os.Signal, 1)
	signal.Notify(intChan, os.Interrupt, os.Kill)
	go func() {
		<-intChan
		Close()
		os.Exit(1)
	}()

	if theme != nil {
		C.start_color()
		initPairs(theme, black)
		_color = attrColored
	} else {
		_color = attrMono
	}
}