File: util_test.go

package info (click to toggle)
golang-github-xo-terminfo 0.0~git20210125.ca9a967-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 256 kB
  • sloc: sh: 50; makefile: 6
file content (69 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download | duplicates (2)
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
package terminfo

import (
	"os"
	"path/filepath"
	"regexp"
	"sync"
	"testing"
)

var termNameCache = struct {
	names map[string]string
	sync.Mutex
}{}

var fileRE = regexp.MustCompile("^([0-9]+|[a-zA-Z])/")

func terms(t *testing.T) map[string]string {
	termNameCache.Lock()
	defer termNameCache.Unlock()

	if termNameCache.names == nil {
		termNameCache.names = make(map[string]string)
		for _, dir := range termDirs() {
			werr := filepath.Walk(dir, func(file string, fi os.FileInfo, err error) error {
				if err != nil {
					return err
				}
				if fi.IsDir() || !fileRE.MatchString(file[len(dir)+1:]) || fi.Mode()&os.ModeSymlink != 0 {
					return nil
				}

				term := filepath.Base(file)
				/*if term != "kterm" {
					return nil
				}*/

				if term == "xterm-old" {
					return nil
				}

				termNameCache.names[term] = file
				return nil
			})
			if werr != nil {
				t.Fatalf("could not walk directory, got: %v", werr)
			}
		}
	}

	return termNameCache.names
}

func termDirs() []string {
	var dirs []string
	for _, dir := range []string{"/lib/terminfo", "/usr/share/terminfo"} {
		fi, err := os.Stat(dir)
		if err != nil && os.IsNotExist(err) {
			continue
		} else if err != nil {
			panic(err)
		}
		if fi.IsDir() {
			dirs = append(dirs, dir)
		}
	}

	return dirs
}