-
Notifications
You must be signed in to change notification settings - Fork 51
/
screen.go
74 lines (65 loc) · 2.47 KB
/
screen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2011 Rob Thornton. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goncurses
// #include <stdio.h>
// #if defined(__MINGW32__) || defined(__MINGW64__)
// FILE *fdopen(int fildes, const char *mode) { return _fdopen(fildes, mode); }
// #endif
// #include <stdlib.h>
// #include <curses.h>
import "C"
import (
"errors"
"os"
"unsafe"
)
type Screen struct{ scrPtr *C.SCREEN }
// 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 capabilities. 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
}
// Set the screen to be the current, active screen
func (s *Screen) Set() (*Screen, error) {
screen := C.set_term(s.scrPtr)
if screen == nil {
return nil, errors.New("Failed to set screen")
}
return &Screen{screen}, nil
}
// Delete frees memory allocated to the screen. This function
func (s *Screen) Delete() {
C.delscreen(s.scrPtr)
}
// End is just a wrapper for the global End function. This helper function
// has been provided to help ensure that new terminals are closed in the
// proper, reverse order they were created. It makes the terminal active via
// set then called End so it is closed properly. You must make sure that
// Delete is called once done with the screen/terminal.
func (s *Screen) End() {
s.Set()
End()
}