// Package terminfo implements reading terminfo files in pure go.
package terminfo //go:generate go run gen.go import ( ) // Error is a terminfo error. type Error string // Error satisfies the error interface. func ( Error) () string { return string() } const ( // ErrInvalidFileSize is the invalid file size error. ErrInvalidFileSize Error = "invalid file size" // ErrUnexpectedFileEnd is the unexpected file end error. ErrUnexpectedFileEnd Error = "unexpected file end" // ErrInvalidStringTable is the invalid string table error. ErrInvalidStringTable Error = "invalid string table" // ErrInvalidMagic is the invalid magic error. ErrInvalidMagic Error = "invalid magic" // ErrInvalidHeader is the invalid header error. ErrInvalidHeader Error = "invalid header" // ErrInvalidNames is the invalid names error. ErrInvalidNames Error = "invalid names" // ErrInvalidExtendedHeader is the invalid extended header error. ErrInvalidExtendedHeader Error = "invalid extended header" // ErrEmptyTermName is the empty term name error. ErrEmptyTermName Error = "empty term name" // ErrDatabaseDirectoryNotFound is the database directory not found error. ErrDatabaseDirectoryNotFound Error = "database directory not found" // ErrFileNotFound is the file not found error. ErrFileNotFound Error = "file not found" // ErrInvalidTermProgramVersion is the invalid TERM_PROGRAM_VERSION error. ErrInvalidTermProgramVersion Error = "invalid TERM_PROGRAM_VERSION" ) // Terminfo describes a terminal's capabilities. type Terminfo struct { // File is the original source file. File string // Names are the provided cap names. Names []string // Bools are the bool capabilities. Bools map[int]bool // BoolsM are the missing bool capabilities. BoolsM map[int]bool // Nums are the num capabilities. Nums map[int]int // NumsM are the missing num capabilities. NumsM map[int]bool // Strings are the string capabilities. Strings map[int][]byte // StringsM are the missing string capabilities. StringsM map[int]bool // ExtBools are the extended bool capabilities. ExtBools map[int]bool // ExtBoolsNames is the map of extended bool capabilities to their index. ExtBoolNames map[int][]byte // ExtNums are the extended num capabilities. ExtNums map[int]int // ExtNumsNames is the map of extended num capabilities to their index. ExtNumNames map[int][]byte // ExtStrings are the extended string capabilities. ExtStrings map[int][]byte // ExtStringsNames is the map of extended string capabilities to their index. ExtStringNames map[int][]byte } // Decode decodes the terminfo data contained in buf. func ( []byte) (*Terminfo, error) { var error // check max file length if len() >= maxFileLength { return nil, ErrInvalidFileSize } := &decoder{ buf: , n: len(), } // read header , := .readInts(6, 16) if != nil { return nil, } var int // check magic switch { case [fieldMagic] == magic: = 16 case [fieldMagic] == magicExtended: = 32 default: return nil, ErrInvalidMagic } // check header if hasInvalidCaps() { return nil, ErrInvalidHeader } // check remaining length if .n-.pos < capLength() { return nil, ErrUnexpectedFileEnd } // read names , := .readBytes([fieldNameSize]) if != nil { return nil, } // check name is terminated properly := findNull(, 0) if == -1 { return nil, ErrInvalidNames } = [:] // read bool caps , , := .readBools([fieldBoolCount]) if != nil { return nil, } // read num caps , , := .readNums([fieldNumCount], ) if != nil { return nil, } // read string caps , , := .readStrings([fieldStringCount], [fieldTableSize]) if != nil { return nil, } := &Terminfo{ Names: strings.Split(string(), "|"), Bools: , BoolsM: , Nums: , NumsM: , Strings: , StringsM: , } // at the end of file, so no extended caps if .pos >= .n { return , nil } // decode extended header , := .readInts(5, 16) if != nil { return nil, } // check extended offset field if hasInvalidExtOffset() { return nil, ErrInvalidExtendedHeader } // check extended cap lengths if .n-.pos != extCapLength(, ) { return nil, ErrInvalidExtendedHeader } // read extended bool caps .ExtBools, _, = .readBools([fieldExtBoolCount]) if != nil { return nil, } // read extended num caps .ExtNums, _, = .readNums([fieldExtNumCount], ) if != nil { return nil, } // read extended string data table indexes , := .readInts([fieldExtOffsetCount], 16) if != nil { return nil, } // read string data table , := .readBytes([fieldExtTableSize]) if != nil { return nil, } // precautionary check that exactly at end of file if .pos != .n { return nil, ErrUnexpectedFileEnd } var int // read extended string caps .ExtStrings, , = readStrings(, , [fieldExtStringCount]) if != nil { return nil, } , = [[fieldExtStringCount]:], [:] // read extended bool names .ExtBoolNames, _, = readStrings(, , [fieldExtBoolCount]) if != nil { return nil, } = [[fieldExtBoolCount]:] // read extended num names .ExtNumNames, _, = readStrings(, , [fieldExtNumCount]) if != nil { return nil, } = [[fieldExtNumCount]:] // read extended string names .ExtStringNames, _, = readStrings(, , [fieldExtStringCount]) if != nil { return nil, } // extIndexes = extIndexes[eh[fieldExtStringCount]:] return , nil } // Open reads the terminfo file name from the specified directory dir. func (, string) (*Terminfo, error) { var error var []byte var string for , := range []string{ path.Join(, [0:1], ), path.Join(, strconv.FormatUint(uint64([0]), 16), ), } { , = ioutil.ReadFile() if == nil { = break } } if == nil { return nil, ErrFileNotFound } // decode , := Decode() if != nil { return nil, } // save original file name .File = // add to cache termCache.Lock() for , := range .Names { termCache.db[] = } termCache.Unlock() return , nil } // boolCaps returns all bool and extended capabilities using f to format the // index key. func ( *Terminfo) ( func(int) string, bool) map[string]bool { := make(map[string]bool, len(.Bools)+len(.ExtBools)) if ! { for , := range .Bools { [()] = } } else { for , := range .ExtBools { [string(.ExtBoolNames[])] = } } return } // BoolCaps returns all bool capabilities. func ( *Terminfo) () map[string]bool { return .boolCaps(BoolCapName, false) } // BoolCapsShort returns all bool capabilities, using the short name as the // index. func ( *Terminfo) () map[string]bool { return .boolCaps(BoolCapNameShort, false) } // ExtBoolCaps returns all extended bool capabilities. func ( *Terminfo) () map[string]bool { return .boolCaps(BoolCapName, true) } // ExtBoolCapsShort returns all extended bool capabilities, using the short // name as the index. func ( *Terminfo) () map[string]bool { return .boolCaps(BoolCapNameShort, true) } // numCaps returns all num and extended capabilities using f to format the // index key. func ( *Terminfo) ( func(int) string, bool) map[string]int { := make(map[string]int, len(.Nums)+len(.ExtNums)) if ! { for , := range .Nums { [()] = } } else { for , := range .ExtNums { [string(.ExtNumNames[])] = } } return } // NumCaps returns all num capabilities. func ( *Terminfo) () map[string]int { return .numCaps(NumCapName, false) } // NumCapsShort returns all num capabilities, using the short name as the // index. func ( *Terminfo) () map[string]int { return .numCaps(NumCapNameShort, false) } // ExtNumCaps returns all extended num capabilities. func ( *Terminfo) () map[string]int { return .numCaps(NumCapName, true) } // ExtNumCapsShort returns all extended num capabilities, using the short // name as the index. func ( *Terminfo) () map[string]int { return .numCaps(NumCapNameShort, true) } // stringCaps returns all string and extended capabilities using f to format the // index key. func ( *Terminfo) ( func(int) string, bool) map[string][]byte { := make(map[string][]byte, len(.Strings)+len(.ExtStrings)) if ! { for , := range .Strings { [()] = } } else { for , := range .ExtStrings { [string(.ExtStringNames[])] = } } return } // StringCaps returns all string capabilities. func ( *Terminfo) () map[string][]byte { return .stringCaps(StringCapName, false) } // StringCapsShort returns all string capabilities, using the short name as the // index. func ( *Terminfo) () map[string][]byte { return .stringCaps(StringCapNameShort, false) } // ExtStringCaps returns all extended string capabilities. func ( *Terminfo) () map[string][]byte { return .stringCaps(StringCapName, true) } // ExtStringCapsShort returns all extended string capabilities, using the short // name as the index. func ( *Terminfo) () map[string][]byte { return .stringCaps(StringCapNameShort, true) } // Has determines if the bool cap i is present. func ( *Terminfo) ( int) bool { return .Bools[] } // Num returns the num cap i, or -1 if not present. func ( *Terminfo) ( int) int { , := .Nums[] if ! { return -1 } return } // Printf formats the string cap i, interpolating parameters v. func ( *Terminfo) ( int, ...interface{}) string { return Printf(.Strings[], ...) } // Fprintf prints the string cap i to writer w, interpolating parameters v. func ( *Terminfo) ( io.Writer, int, ...interface{}) { Fprintf(, .Strings[], ...) } // Color takes a foreground and background color and returns string that sets // them for this terminal. func ( *Terminfo) (, int, string) string { := int(.Nums[MaxColors]) // map bright colors to lower versions if the color table only holds 8. if == 8 { if > 7 && < 16 { -= 8 } if > 7 && < 16 { -= 8 } } var string if > && >= 0 { += .Printf(SetAForeground, ) } if > && >= 0 { += .Printf(SetABackground, ) } return + + .Printf(ExitAttributeMode) } // Goto returns a string suitable for addressing the cursor at the given // row and column. The origin 0, 0 is in the upper left corner of the screen. func ( *Terminfo) (, int) string { return Printf(.Strings[CursorAddress], , ) } // Puts emits the string to the writer, but expands inline padding indications // (of the form $<[delay]> where [delay] is msec) to a suitable number of // padding characters (usually null bytes) based upon the supplied baud. At // high baud rates, more padding characters will be inserted. /*func (ti *Terminfo) Puts(w io.Writer, s string, lines, baud int) (int, error) { var err error for { start := strings.Index(s, "$<") if start == -1 { // most strings don't need padding, which is good news! return io.WriteString(w, s) } end := strings.Index(s, ">") if end == -1 { // unterminated... just emit bytes unadulterated. return io.WriteString(w, "$<"+s) } var c int c, err = io.WriteString(w, s[:start]) if err != nil { return n + c, err } n += c s = s[start+2:] val := s[:end] s = s[end+1:] var ms int var dot, mandatory, asterisk bool unit := 1000 for _, ch := range val { switch { case ch >= '0' && ch <= '9': ms = (ms * 10) + int(ch-'0') if dot { unit *= 10 } case ch == '.' && !dot: dot = true case ch == '*' && !asterisk: ms *= lines asterisk = true case ch == '/': mandatory = true default: break } } z, pad := ((baud/8)/unit)*ms, ti.Strings[PadChar] b := make([]byte, len(pad)*z) for bp := copy(b, pad); bp < len(b); bp *= 2 { copy(b[bp:], b[:bp]) } if (!ti.Bools[XonXoff] && baud > int(ti.Nums[PaddingBaudRate])) || mandatory { c, err = w.Write(b) if err != nil { return n + c, err } n += c } } return n, nil }*/