summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/xo/terminfo/load.go
diff options
context:
space:
mode:
authormjarkk <mkopenga@gmail.com>2021-07-27 15:00:37 +0200
committermjarkk <mkopenga@gmail.com>2021-07-30 15:14:46 +0200
commit79848087bccd5c87af1dbb44a39753aad1346f8b (patch)
tree07e4b6eb4b7ed5fdcbde8d697a214b647ddd0536 /vendor/github.com/xo/terminfo/load.go
parenta3b820fb5f20f4a24028ecbf285d54bbaa7b6974 (diff)
Switch to github.com/gookit/color for terminal colors
Diffstat (limited to 'vendor/github.com/xo/terminfo/load.go')
-rw-r--r--vendor/github.com/xo/terminfo/load.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/vendor/github.com/xo/terminfo/load.go b/vendor/github.com/xo/terminfo/load.go
new file mode 100644
index 000000000..9b0b94286
--- /dev/null
+++ b/vendor/github.com/xo/terminfo/load.go
@@ -0,0 +1,72 @@
+package terminfo
+
+import (
+ "os"
+ "os/user"
+ "path"
+ "strings"
+ "sync"
+)
+
+// termCache is the terminfo cache.
+var termCache = struct {
+ db map[string]*Terminfo
+ sync.RWMutex
+}{
+ db: make(map[string]*Terminfo),
+}
+
+// Load follows the behavior described in terminfo(5) to find correct the
+// terminfo file using the name, reads the file and then returns a Terminfo
+// struct that describes the file.
+func Load(name string) (*Terminfo, error) {
+ if name == "" {
+ return nil, ErrEmptyTermName
+ }
+
+ termCache.RLock()
+ ti, ok := termCache.db[name]
+ termCache.RUnlock()
+
+ if ok {
+ return ti, nil
+ }
+
+ var checkDirs []string
+
+ // check $TERMINFO
+ if dir := os.Getenv("TERMINFO"); dir != "" {
+ checkDirs = append(checkDirs, dir)
+ }
+
+ // check $HOME/.terminfo
+ u, err := user.Current()
+ if err != nil {
+ return nil, err
+ }
+ checkDirs = append(checkDirs, path.Join(u.HomeDir, ".terminfo"))
+
+ // check $TERMINFO_DIRS
+ if dirs := os.Getenv("TERMINFO_DIRS"); dirs != "" {
+ checkDirs = append(checkDirs, strings.Split(dirs, ":")...)
+ }
+
+ // check fallback directories
+ checkDirs = append(checkDirs, "/etc/terminfo", "/lib/terminfo", "/usr/share/terminfo")
+ for _, dir := range checkDirs {
+ ti, err = Open(dir, name)
+ if err != nil && err != ErrFileNotFound && !os.IsNotExist(err) {
+ return nil, err
+ } else if err == nil {
+ return ti, nil
+ }
+ }
+
+ return nil, ErrDatabaseDirectoryNotFound
+}
+
+// LoadFromEnv loads the terminal info based on the name contained in
+// environment variable TERM.
+func LoadFromEnv() (*Terminfo, error) {
+ return Load(os.Getenv("TERM"))
+}