From 2f3199b2869f64c79aafdcdcae0cf7db6dfaf05a Mon Sep 17 00:00:00 2001 From: Alexander Tumin Date: Sun, 26 Sep 2021 19:17:45 +0300 Subject: [PATCH] Add NewTerminfoScreenFromTtyTerminfo to allow creating Screen using (#479) custom terminfo as well as custom tty. --- tscreen.go | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/tscreen.go b/tscreen.go index a0820a4..11d84e0 100644 --- a/tscreen.go +++ b/tscreen.go @@ -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