github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/pkg/term/ascii.go (about)

     1  package term
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // ASCII list the possible supported ASCII key sequence
     9  var ASCII = []string{
    10  	"ctrl-@",
    11  	"ctrl-a",
    12  	"ctrl-b",
    13  	"ctrl-c",
    14  	"ctrl-d",
    15  	"ctrl-e",
    16  	"ctrl-f",
    17  	"ctrl-g",
    18  	"ctrl-h",
    19  	"ctrl-i",
    20  	"ctrl-j",
    21  	"ctrl-k",
    22  	"ctrl-l",
    23  	"ctrl-m",
    24  	"ctrl-n",
    25  	"ctrl-o",
    26  	"ctrl-p",
    27  	"ctrl-q",
    28  	"ctrl-r",
    29  	"ctrl-s",
    30  	"ctrl-t",
    31  	"ctrl-u",
    32  	"ctrl-v",
    33  	"ctrl-w",
    34  	"ctrl-x",
    35  	"ctrl-y",
    36  	"ctrl-z",
    37  	"ctrl-[",
    38  	"ctrl-\\",
    39  	"ctrl-]",
    40  	"ctrl-^",
    41  	"ctrl-_",
    42  }
    43  
    44  // ToBytes converts a string representing a suite of key-sequence to the corresponding ASCII code.
    45  func ToBytes(keys string) ([]byte, error) {
    46  	codes := []byte{}
    47  next:
    48  	for _, key := range strings.Split(keys, ",") {
    49  		if len(key) != 1 {
    50  			for code, ctrl := range ASCII {
    51  				if ctrl == key {
    52  					codes = append(codes, byte(code))
    53  					continue next
    54  				}
    55  			}
    56  			if key == "DEL" {
    57  				codes = append(codes, 127)
    58  			} else {
    59  				return nil, fmt.Errorf("Unknown character: '%s'", key)
    60  			}
    61  		} else {
    62  			codes = append(codes, byte(key[0]))
    63  		}
    64  	}
    65  	return codes, nil
    66  }