Skip to content

Commit

Permalink
Add NewTerminfoScreenFromTtyTerminfo to allow creating Screen using (#…
Browse files Browse the repository at this point in the history
…479)

custom terminfo as well as custom tty.
  • Loading branch information
iamtakingiteasy committed Sep 26, 2021
1 parent edd0f2f commit 2f3199b
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions tscreen.go
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 2f3199b

Please sign in to comment.