115 lines
1.8 KiB
Go
115 lines
1.8 KiB
Go
|
package tty
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
)
|
||
|
|
||
|
// Window represents the size of a PTY window.
|
||
|
type Window struct {
|
||
|
Columns uint32
|
||
|
Rows uint32
|
||
|
Width uint32
|
||
|
Height uint32
|
||
|
}
|
||
|
|
||
|
// Pty represents a PTY request and configuration.
|
||
|
type Pty struct {
|
||
|
Term string
|
||
|
Columns uint32
|
||
|
Rows uint32
|
||
|
Width uint32
|
||
|
Height uint32
|
||
|
Modelist string
|
||
|
}
|
||
|
|
||
|
type Environ struct {
|
||
|
Key string
|
||
|
Value string
|
||
|
}
|
||
|
|
||
|
type TermInfo struct {
|
||
|
Term string
|
||
|
Cols uint32
|
||
|
Rows uint32
|
||
|
Modelist string
|
||
|
}
|
||
|
|
||
|
type CommandLine struct {
|
||
|
Command string
|
||
|
}
|
||
|
type ExitStatus struct {
|
||
|
Status uint32
|
||
|
}
|
||
|
|
||
|
func (i *TermInfo) ToWindow() (*Window) {
|
||
|
if i == nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return &Window{
|
||
|
Columns: i.Cols,
|
||
|
Rows: i.Rows,
|
||
|
Width: 8 * i.Cols,
|
||
|
Height: 8 * i.Rows,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (i *TermInfo) ToPty() (*Pty) {
|
||
|
if i == nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return &Pty{
|
||
|
Term: i.Term,
|
||
|
Columns: i.Cols,
|
||
|
Rows: i.Rows,
|
||
|
Width: 8 * i.Cols,
|
||
|
Height: 8 * i.Rows,
|
||
|
Modelist: i.Modelist,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (i *TermInfo) TermMap() (mode map[uint8]uint32) {
|
||
|
mode = make(map[uint8]uint32)
|
||
|
if i == nil {
|
||
|
return
|
||
|
}
|
||
|
reader := bytes.NewReader([]byte(i.Modelist))
|
||
|
buf := make([]byte, 4)
|
||
|
for {
|
||
|
if k, err := reader.ReadByte(); err != nil {
|
||
|
break
|
||
|
} else if read, err := reader.Read(buf); err != nil {
|
||
|
break
|
||
|
} else if read != 4 {
|
||
|
break
|
||
|
} else {
|
||
|
mode[uint8(k)] = uint32(buf[0])<<24 | uint32(buf[1])<<16 | uint32(buf[2])<<9 | uint32(buf[3])<<0
|
||
|
}
|
||
|
}
|
||
|
return mode
|
||
|
}
|
||
|
func (p *Pty) ToTermInfo() (*TermInfo) {
|
||
|
if p == nil {
|
||
|
return nil
|
||
|
}
|
||
|
return &TermInfo{
|
||
|
Term: p.Term,
|
||
|
Cols: p.Columns,
|
||
|
Rows: p.Rows,
|
||
|
Modelist: p.Modelist,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (p *Pty) ToWindow() (*Window) {
|
||
|
if p == nil {
|
||
|
return nil
|
||
|
}
|
||
|
return &Window{
|
||
|
Columns: p.Columns,
|
||
|
Rows: p.Rows,
|
||
|
Width: 8 * p.Columns,
|
||
|
Height: 8 * p.Rows,
|
||
|
}
|
||
|
}
|