code: func (ar *ansiReader) Read(p []byte) (int, error) {
  	if len(p) == 0 {
  		return 0, nil
  	}
  
  	// Previously read bytes exist, read as much as we can and return
  	if len(ar.buffer) > 0 {
  		logger.Debugf("Reading previously cached bytes")
  
  		originalLength := len(ar.buffer)
  		copiedLength := copy(p, ar.buffer)
  
  		if copiedLength == originalLength {
  			ar.buffer = make([]byte, 0, len(p))
  		} else {
  			ar.buffer = ar.buffer[copiedLength:]
  		}
  
  		logger.Debugf("Read from cache p[%d]: % x", copiedLength, p)
  		return copiedLength, nil
  	}
  
  	// Read and translate key events
  	events, err := readInputEvents(ar.fd, len(p))
  	if err != nil {
  		return 0, err
  	} else if len(events) == 0 {
  		logger.Debug("No input events detected")
  		return 0, nil
  	}
  
  	keyBytes := translateKeyEvents(events, []byte(escapeSequence))
  
  	// Save excess bytes and right-size keyBytes
  	if len(keyBytes) > len(p) {
  		logger.Debugf("Received %d keyBytes, only room for %d bytes", len(keyBytes), len(p))
  		ar.buffer = keyBytes[len(p):]
  		keyBytes = keyBytes[:len(p)]
  	} else if len(keyBytes) == 0 {
  		logger.Debug("No key bytes returned from the translator")
  		return 0, nil
  	}
  
  	copiedLength := copy(p, keyBytes)
  	if copiedLength != len(keyBytes) {
  		return 0, errors.New("unexpected copy length encountered")
  	}
  
  	logger.Debugf("Read        p[%d]: % x", copiedLength, p)
  	logger.Debugf("Read keyBytes[%d]: % x", copiedLength, keyBytes)
  	return copiedLength, nil
  }
docs: // Read reads up to len(p) bytes of translated input events into p.