Add NewTerminfoScreenFromTtyTerminfo to allow creating Screen using (#479)

custom terminfo as well as custom tty.
This commit is contained in:
Alexander Tumin 2021-09-26 19:17:45 +03:00 committed by GitHub
parent edd0f2f203
commit 2f3199b286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 7 deletions

View File

@ -46,19 +46,36 @@ func NewTerminfoScreen() (Screen, error) {
return NewTerminfoScreenFromTty(nil)
}
// NewTerminfoScreenFromTty returns a Screen using a custom Tty implementation.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
func NewTerminfoScreenFromTty(tty Tty) (Screen, error) {
ti, e := terminfo.LookupTerminfo(os.Getenv("TERM"))
// LookupTerminfo attempts to find a definition for the named $TERM falling
// back to attempting to parse the output from infocmp.
func LookupTerminfo(name string) (ti *terminfo.Terminfo, e error) {
ti, e = terminfo.LookupTerminfo(name)
if e != nil {
ti, e = loadDynamicTerminfo(os.Getenv("TERM"))
ti, e = loadDynamicTerminfo(name)
if e != nil {
return nil, e
}
terminfo.AddTerminfo(ti)
}
return
}
// NewTerminfoScreenFromTtyTerminfo returns a Screen using a custom Tty
// implementation and custom terminfo specification.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
// If passed terminfo is nil, then TERM environment variable is queried for
// terminal specification.
func NewTerminfoScreenFromTtyTerminfo(tty Tty, ti *terminfo.Terminfo) (s Screen, e error) {
if ti == nil {
ti, e = LookupTerminfo(os.Getenv("TERM"))
if e != nil {
return
}
}
t := &tScreen{ti: ti, tty: tty}
t.keyexist = make(map[Key]bool)
@ -77,6 +94,14 @@ func NewTerminfoScreenFromTty(tty Tty) (Screen, error) {
return t, nil
}
// NewTerminfoScreenFromTty returns a Screen using a custom Tty implementation.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
func NewTerminfoScreenFromTty(tty Tty) (Screen, error) {
return NewTerminfoScreenFromTtyTerminfo(tty, nil)
}
// tKeyCode represents a combination of a key code and modifiers.
type tKeyCode struct {
key Key