amuz.es/src/infra/goutils@v0.1.3/tty/term.go (about)

     1  package tty
     2  
     3  import (
     4  	"bytes"
     5  )
     6  
     7  // Window represents the size of a PTY window.
     8  type Window struct {
     9  	Columns uint32
    10  	Rows    uint32
    11  	Width   uint32
    12  	Height  uint32
    13  }
    14  
    15  // Pty represents a PTY request and configuration.
    16  type Pty struct {
    17  	Term     string
    18  	Columns  uint32
    19  	Rows     uint32
    20  	Width    uint32
    21  	Height   uint32
    22  	Modelist string
    23  }
    24  
    25  type Environ struct {
    26  	Key   string
    27  	Value string
    28  }
    29  
    30  type TermInfo struct {
    31  	Term     string
    32  	Cols     uint32
    33  	Rows     uint32
    34  	Modelist string
    35  }
    36  
    37  type CommandLine struct {
    38  	Command string
    39  }
    40  type ExitStatus struct {
    41  	Status uint32
    42  }
    43  
    44  func (i *TermInfo) ToWindow() (*Window) {
    45  	if i == nil {
    46  		return nil
    47  	}
    48  
    49  	return &Window{
    50  		Columns: i.Cols,
    51  		Rows:    i.Rows,
    52  		Width:   8 * i.Cols,
    53  		Height:  8 * i.Rows,
    54  	}
    55  }
    56  
    57  func (i *TermInfo) ToPty() (*Pty) {
    58  	if i == nil {
    59  		return nil
    60  	}
    61  
    62  	return &Pty{
    63  		Term:     i.Term,
    64  		Columns:  i.Cols,
    65  		Rows:     i.Rows,
    66  		Width:    8 * i.Cols,
    67  		Height:   8 * i.Rows,
    68  		Modelist: i.Modelist,
    69  	}
    70  }
    71  
    72  func (i *TermInfo) TermMap() (mode map[uint8]uint32) {
    73  	mode = make(map[uint8]uint32)
    74  	if i == nil {
    75  		return
    76  	}
    77  	reader := bytes.NewReader([]byte(i.Modelist))
    78  	buf := make([]byte, 4)
    79  	for {
    80  		if k, err := reader.ReadByte(); err != nil {
    81  			break
    82  		} else if read, err := reader.Read(buf); err != nil {
    83  			break
    84  		} else if read != 4 {
    85  			break
    86  		} else {
    87  			mode[uint8(k)] = uint32(buf[0])<<24 | uint32(buf[1])<<16 | uint32(buf[2])<<9 | uint32(buf[3])<<0
    88  		}
    89  	}
    90  	return mode
    91  }
    92  func (p *Pty) ToTermInfo() (*TermInfo) {
    93  	if p == nil {
    94  		return nil
    95  	}
    96  	return &TermInfo{
    97  		Term:     p.Term,
    98  		Cols:     p.Columns,
    99  		Rows:     p.Rows,
   100  		Modelist: p.Modelist,
   101  	}
   102  }
   103  
   104  func (p *Pty) ToWindow() (*Window) {
   105  	if p == nil {
   106  		return nil
   107  	}
   108  	return &Window{
   109  		Columns: p.Columns,
   110  		Rows:    p.Rows,
   111  		Width:   8 * p.Columns,
   112  		Height:  8 * p.Rows,
   113  	}
   114  }